Skip to content

Commit

Permalink
fix(xunlei): invalid device_id & refresh token (#510)
Browse files Browse the repository at this point in the history
- 修复 迅雷 device_id 参数提交问题
- 新增 迅雷刷新 token
  • Loading branch information
sgpublic authored May 9, 2024
1 parent c4c0aa6 commit e1c4d58
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions kubespider/download_provider/xunlei_download_provider/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import hashlib
import base64
from urllib.parse import quote

import execjs
import bencodepy

Expand All @@ -25,6 +27,7 @@ def __init__(self, name: str, config_reader: AbsConfigReader) -> None:
self.http_endpoint = ''
self._device_id = None
self._token_str = None
self._token_time = 0
self.js_ctx = execjs.compile('')
self.request_handler = get_request_controller(use_proxy=False)

Expand All @@ -50,14 +53,15 @@ def send_torrent_task(self, task: Task) -> TypeError:
file_info = self.list_files(token, magnet_url)
return self.send_task(token, file_info, magnet_url, task.path)

def device_id(self):
def device_id(self, url_encode: bool = False):
if self._device_id is None:
info_watch = "/webman/3rdparty/pan-xunlei-com/index.cgi/device/info/watch"
token = self.get_pan_token()
req = self.request_handler.post(self.http_endpoint + info_watch,
headers={'pan-auth': token}, timeout=30)
self._device_id = json.loads(req.text).get("target")
return self._device_id
self._device_id = str(json.loads(req.text).get("target"))
logging.info('Get xunlei device_id: %s', self._device_id)
return self._device_id if not url_encode else quote(self._device_id)

def send_magnet_task(self, task: Task) -> TypeError:
logging.info('Start magnet download:%s', task.url)
Expand Down Expand Up @@ -89,7 +93,6 @@ def load_config(self) -> TypeError:
self.js_ctx = execjs.compile(js_text)
except Exception as err:
logging.error("Cannot read .config/dependencies/xunlei_download_provider/get_token.js: %s", err)
# self.device_id = cfg.get('device_id', '')

def list_files(self, token: str, url: str) -> dict:
try:
Expand All @@ -116,9 +119,9 @@ def send_task(self, token: str, file_info: dict, url: str, path: str) -> TypeErr
"name": file_info['list']['resources'][0]['name'],
"file_name": file_info['list']['resources'][0]['name'],
"file_size": str(file_size),
"space": "device_id#" + self.device_id(),
"space": self.device_id(),
"params": {
"target": "device_id#" + self.device_id(),
"target": self.device_id(),
"url": url,
"total_file_count": str(file_info['list']['resources'][0]['file_count']),
"sub_file_index": str(self.get_file_index(file_info)),
Expand Down Expand Up @@ -154,7 +157,7 @@ def create_sub_path(self, token: str, dir_name: str, parent_id: str) -> TypeErro
data = {
"parent_id": parent_id,
"name": dir_name,
"space": "device_id#" + self.device_id(),
"space": self.device_id(),
"kind": "drive#folder"
}
rep = self.request_handler.post(self.http_endpoint + path, headers={'pan-auth': token}, timeout=30,
Expand All @@ -175,9 +178,10 @@ def get_path_id(self, token: str, path: str) -> str:
while 1:
if len(dir_list) == cnt:
return parent_id
file_path = '/webman/3rdparty/pan-xunlei-com/index.cgi/drive/v1/files?space=device_id%23' + \
self.device_id() + '&limit=200&filters=%7B%22kind%22%3A%7B%22eq%22%3A%22drive%23folder%22%7D%7D&page_token=&' + \
'pan_auth=' + token + '&device_space=&parent_id=' + parent_id
file_path = ('/webman/3rdparty/pan-xunlei-com/index.cgi/drive/v1/files?space=' +
self.device_id(url_encode=True) + '&limit=200&' +
'filters=%7B%22kind%22%3A%7B%22eq%22%3A%22drive%23folder%22%7D%7D&page_token=&' +
'pan_auth=' + token + '&device_space=&parent_id=' + parent_id)
rep = self.request_handler.get(self.http_endpoint + file_path, headers={'pan-auth': token}, timeout=30)
if rep.status_code != 200:
raise Exception('Get files id error:' + rep.text)
Expand Down Expand Up @@ -225,13 +229,15 @@ def get_file_index(self, file_info: dict) -> str:
def get_pan_token(self) -> str:
server_version = self.get_server_version()
if check_version_at_lest(server_version, "3.21.0"):
if self._token_str is not None:
if self._token_str is not None and self._token_time + 600 > int(time.time()):
return self._token_str
resp = self.request_handler.get(self.http_endpoint + '/webman/3rdparty/pan-xunlei-com/index.cgi/',
timeout=30)
uiauth = r'function uiauth\(value\){ return "(.*)" }'
for script in re.findall(uiauth, resp.text):
self._token_str = script
self._token_time = int(time.time())
logging.info('Get xunlei token from html:%s', self._token_str)
return self._token_str
logging.error('Get xunlei token from html error')
return ""
Expand Down

0 comments on commit e1c4d58

Please sign in to comment.