From 0915574bc6832e22f1f45ee7d3e29e8a105f33da Mon Sep 17 00:00:00 2001 From: JoeanAmier Date: Fri, 9 Aug 2024 19:07:05 +0800 Subject: [PATCH] =?UTF-8?q?fix(download.py):=20=E4=BF=AE=E5=A4=8D=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8B=E8=BD=BD=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/application/download.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/source/application/download.py b/source/application/download.py index 15ea064..b947aab 100644 --- a/source/application/download.py +++ b/source/application/download.py @@ -1,8 +1,10 @@ from asyncio import gather from pathlib import Path +from typing import TYPE_CHECKING + from aiofiles import open from httpx import HTTPError -from typing import TYPE_CHECKING + from source.module import ERROR from source.module import Manager from source.module import logging @@ -126,8 +128,9 @@ class Download: @re_download async def __download(self, url: str, path: Path, name: str, format_: str, log, bar): + headers = self.headers.copy() try: - length, suffix = await self.__hand_file(url, format_, ) + length, suffix = await self.__head_file(url, headers, format_, ) except HTTPError as error: logging(log, str(error), ERROR) logging( @@ -139,9 +142,10 @@ class Download: return False temp = self.temp.joinpath(f"{name}.{suffix}") real = path.joinpath(f"{name}.{suffix}") - self.__update_headers_range(temp, ) + self.__update_headers_range(headers, temp, ) try: - async with self.client.stream("GET", url, headers=self.headers) as response: + # print(f"{url} Stream Headers:", headers) # 调试代码 + async with self.client.stream("GET", url, headers=headers, ) as response: response.raise_for_status() # self.__create_progress( # bar, @@ -183,14 +187,16 @@ class Download: def __extract_type(cls, content: str) -> str: return cls.CONTENT_TYPE_MAP.get(content, "") - async def __hand_file(self, + async def __head_file(self, url: str, + headers: dict[str, str], suffix: str, ) -> [int, str]: - response = await self.client.head(url, - headers=self.headers | { - "Range": "bytes=0-", - }, ) + # print(f"{url} Head Headers:", headers) # 调试代码 + response = await self.client.head( + url, + headers=headers, + ) response.raise_for_status() suffix = self.__extract_type( response.headers.get("Content-Type")) or suffix @@ -202,6 +208,10 @@ class Download: def __get_resume_byte_position(file: Path) -> int: return file.stat().st_size if file.is_file() else 0 - def __update_headers_range(self, file: Path) -> int: - self.headers["Range"] = f"bytes={(p := self.__get_resume_byte_position(file))}-" + def __update_headers_range( + self, + headers: dict[str, str], + file: Path, + ) -> int: + headers["Range"] = f"bytes={(p := self.__get_resume_byte_position(file))}-" return p