部分番剧响应中 headers 不存在 content-length 导致报错
Closed this issue · 1 comments
ImYrS commented
问题
部分番剧下载时会报 KeyError: 'content-length'
,即为 headers 中未找到 content-length 无法继续下载。
在 H264/H265 均会发生,在同一部番剧中会出现有的会报错,有的可以正常下载。
复现步骤
以非人哉第二季为例 https://www.bilibili.com/bangumi/play/ep814370
4/5/6 话可以正常下载,其余 content-length 均缺失。
完整报错如下
Exception in thread Thread-5 (startDownloadThread):
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 975, in run
self._target(*self._args, **self._kwargs)
File "/Users/imyrs/Codes/Bili23-Downloader/gui/download.py", line 716, in startDownloadThread
self.downloader.start(info_list)
File "/Users/imyrs/Codes/Bili23-Downloader/utils/download.py", line 88, in start
self.add_url(entry)
File "/Users/imyrs/Codes/Bili23-Downloader/utils/download.py", line 67, in add_url
file_size = self.get_total_size(info["url"], info["referer_url"], path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/imyrs/Codes/Bili23-Downloader/utils/download.py", line 236, in get_total_size
total_size = int(req.headers["Content-Length"])
~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
File "/Users/imyrs/Codes/Bili23-Downloader/.venv/lib/python3.11/site-packages/requests/structures.py", line 52, in __getitem__
return self._store[key.lower()][1]
~~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 'content-length'
尝试
首先定位到报错位置
...
def get_total_size(self, url: str, referer_url: str, path: str) -> int:
req = self.session.head(url, headers = get_header(referer_url))
total_size = int(req.headers["Content-Length"])
...
此处简单改为提供默认值 total_size = int(req.headers.get("Content-Length", 0))
后可以开始下载,但可能因为默认分片下载,导致后面音频和视频轨道拼接时还是会报错。尝试改了下 seek 方法但也有其他问题。
时间有限未能仔细研究完整的下载逻辑,所以先发了个 issue。
ScottSloan commented
此问题已在 dev 分支中修复,随 1.51 版本进行更新,如有需要请切换至 dev 分支,感谢反馈