From 5dfb81670d1e63d8337ba004274f27a99c6d9cc5 Mon Sep 17 00:00:00 2001 From: B1gM8c <89020353+B1gM8c@users.noreply.github.com> Date: Fri, 3 Mar 2023 16:25:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=9C=AC=E5=9C=B0=E7=94=9F?= =?UTF-8?q?=E6=88=90X-Bogus?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 由于使用第三方接口必然会产生网络延迟等问题,故支持本地生成X-Bogus 参考项目https://github.com/B1gM8c/tiktok --- TikTokUtils.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/TikTokUtils.py b/TikTokUtils.py index 349d00a..8bdf5f3 100644 --- a/TikTokUtils.py +++ b/TikTokUtils.py @@ -20,6 +20,8 @@ import requests from TikTokUrls import Urls import urllib.parse import execjs +import os +import sys class Utils(object): def __init__(self): @@ -51,6 +53,13 @@ class Utils(object): # 去除前后空格 return result + def resource_path(self,relative_path): + if getattr(sys, 'frozen', False): # 是否Bundle Resource + base_path = sys._MEIPASS + else: + base_path = os.path.dirname(os.path.abspath(__file__)) + return os.path.join(base_path, relative_path) + def getXbogus(self, url, headers=None): # getXbogus算法开源地址https://github.com/B1gM8c/tiktok urls = Urls() @@ -58,7 +67,7 @@ class Utils(object): user_agent = headers.get( 'User-Agent') if headers else "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" try: - xbogus = execjs.compile(open('./X-Bogus.js').read()).call('sign', query, user_agent) + xbogus = execjs.compile(open(self.resource_path(os.path.join("X-Bogus.js"))).read()).call('sign', query, user_agent) params = url + "&X-Bogus=" + xbogus except Exception as e: print('[ 错误 ]:X-Bogus算法异常')