X-Bogus.js使用绝对路径

This commit is contained in:
jeffrey.deng 2023-09-01 16:32:04 +08:00
parent edf1a93aa5
commit 63fb38786a

View file

@ -113,6 +113,15 @@ class Scraper:
print('Error in get_url:', e) print('Error in get_url:', e)
return None return None
@staticmethod
def relpath(file):
""" Always locate to the correct relative path. """
from sys import _getframe
from pathlib import Path
frame = _getframe(1)
curr_file = Path(frame.f_code.co_filename)
return str(curr_file.parent.joinpath(file).resolve())
# 转换链接/convert url # 转换链接/convert url
@retry(stop=stop_after_attempt(4), wait=wait_fixed(7)) @retry(stop=stop_after_attempt(4), wait=wait_fixed(7))
async def convert_share_urls(self, url: str) -> Union[str, None]: async def convert_share_urls(self, url: str) -> Union[str, None]:
@ -218,7 +227,7 @@ class Scraper:
""" """
# 调用JavaScript函数 # 调用JavaScript函数
query = urllib.parse.urlparse(url).query query = urllib.parse.urlparse(url).query
xbogus = execjs.compile(open('./X-Bogus.js').read()).call('sign', query, self.headers['User-Agent']) xbogus = execjs.compile(open(self.relpath('./X-Bogus.js')).read()).call('sign', query, self.headers['User-Agent'])
print('生成的X-Bogus签名为: {}'.format(xbogus)) print('生成的X-Bogus签名为: {}'.format(xbogus))
new_url = url + "&X-Bogus=" + xbogus new_url = url + "&X-Bogus=" + xbogus
return new_url return new_url