From 796dc6d800953190105a0358f8918a6bd92aebf8 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sat, 21 Sep 2024 22:06:40 +0800 Subject: [PATCH] fix aliyun download --- app/modules/filemanager/storages/alipan.py | 37 ++++++++++++++++++++-- app/modules/filemanager/storages/u115.py | 2 ++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/app/modules/filemanager/storages/alipan.py b/app/modules/filemanager/storages/alipan.py index 457c8954..2689c34b 100644 --- a/app/modules/filemanager/storages/alipan.py +++ b/app/modules/filemanager/storages/alipan.py @@ -67,6 +67,8 @@ class AliPan(StorageBase): upload_file_complete_url = "https://api.aliyundrive.com/v2/file/complete" # 查询存储详情 storage_info_url = "https://api.aliyundrive.com/adrive/v1/user/driveCapacityDetails" + # 播放地址 + play_info_url = 'https://api.aliyundrive.com/v2/file/get_video_preview_play_info' def __handle_error(self, res: Response, apiname: str, action: bool = True): """ @@ -601,15 +603,44 @@ class AliPan(StorageBase): if not params: return None headers = self.__get_headers(params) + + def __get_play_url(): + """ + 获取播放地址 + """ + play_res = RequestUtils(headers=headers, timeout=10).post_res(self.play_info_url, json={ + "drive_id": fileitem.drive_id, + "file_id": fileitem.fileid + }) + if play_res: + play_dict = {} + play_info = play_res.json() + if play_info.get('video_preview_play_info'): + for i in play_info['video_preview_play_info'].get('live_transcoding_task_list') or []: + if i.get('url'): + try: + play_dict[i['template_id']] = i['url'] + except KeyError: + pass + if play_dict: + return list(play_dict.values())[-1] + return None + res = RequestUtils(headers=headers, timeout=10).post_res(self.download_url, json={ "drive_id": fileitem.drive_id, "file_id": fileitem.fileid }) if res: - download_url = res.json().get("url") + result = res.json() + download_url = result.get("url") if not download_url: - logger.warn(f"{fileitem.path} 未获取到下载链接") - return None + download_url = __get_play_url() + if not download_url: + if result.get("internal_url"): + download_url = result.get("internal_url") + if not download_url: + logger.warn(f"{fileitem.path} 未获取到下载链接") + return None res = RequestUtils(headers={ "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Referer": "https://www.alipan.com/", diff --git a/app/modules/filemanager/storages/u115.py b/app/modules/filemanager/storages/u115.py index 89be5142..8030165d 100644 --- a/app/modules/filemanager/storages/u115.py +++ b/app/modules/filemanager/storages/u115.py @@ -307,6 +307,8 @@ class U115Pan(StorageBase, metaclass=Singleton): with open(path, "wb") as f: f.write(res.content) return path + else: + logger.warn(f"{fileitem.path} 未获取到下载链接") except Exception as e: logger.error(f"115下载失败:{str(e)}") return None