Skip to content

Commit

Permalink
修复无法获取 download_url 的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
lemisky committed Mar 15, 2023
1 parent 7ededfe commit e638c76
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/aligo/core/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,8 @@
UNI_PARAMS = {'appName': 'aliyun_drive'}
UNI_HEADERS = {
'Referer': 'https://aliyundrive.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36',
'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/91.0.4472.114 Safari/537.36'),
# 没有此请求头,list file 获取不到 download_url 字段,url 不支持断点续传
'x-canary': 'client=web,app=adrive,version=v4.1.0',
}
29 changes: 16 additions & 13 deletions src/aligo/core/Download.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,21 @@ def _core_download_file(self, file_path: str, url: str) -> str:
'Referer': 'https://www.aliyundrive.com/',
}, stream=True) as resp:
total_size = int(resp.headers.get('content-length', 0))
if resp.headers.get('Accept-Ranges', None) != 'bytes':
raise ValueError(f'无效下载链接或链接已过期 {resp.url}')
progress_bar = tqdm(total=total_size + tmp_size, unit='B', unit_scale=True, colour='#31a8ff')
progress_bar.update(tmp_size)
with open(tmp_file, 'ab') as f:
for content in resp.iter_content(chunk_size=Download._DOWNLOAD_CHUNK_SIZE):
progress_bar.update(len(content))
f.write(content)
accept_range = resp.headers.get('Accept-Ranges', None)
if accept_range == 'bytes':
progress_bar = tqdm(total=total_size + tmp_size, unit='B', unit_scale=True, colour='#31a8ff')
progress_bar.update(tmp_size)
with open(tmp_file, 'ab') as f:
for content in resp.iter_content(chunk_size=Download._DOWNLOAD_CHUNK_SIZE):
progress_bar.update(len(content))
f.write(content)
else:
self._auth.log.warning(f'不支持断点续传 {file_path}')
progress_bar = tqdm(total=total_size, unit='B', unit_scale=True, colour='#31a8ff')
with open(tmp_file, 'wb') as f:
for content in resp.iter_content(chunk_size=Download._DOWNLOAD_CHUNK_SIZE):
progress_bar.update(len(content))
f.write(content)
os.renames(tmp_file, file_path)
finally:
if progress_bar:
Expand All @@ -126,10 +133,6 @@ def download_files(self, files: List[BaseFile], local_folder: str = '.') -> List
rt = []
for file in files:
file_path = os.path.join(local_folder, file.name)
try:
file_path = self._core_download_file(file_path, file.download_url or file.url)
except ValueError:
file = self._core_get_file(GetFileRequest(file_id=file.file_id, drive_id=file.drive_id))
file_path = self._core_download_file(file_path, file.download_url or file.url)
file_path = self._core_download_file(file_path, file.download_url or file.url)
rt.append(file_path)
return rt

0 comments on commit e638c76

Please sign in to comment.