mirror of
https://github.com/JoeanAmier/XHS-Downloader.git
synced 2026-03-22 06:57:16 +08:00
fix(download.py): 修复文件下载失败的问题
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
from asyncio import gather
|
from asyncio import gather
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from aiofiles import open
|
from aiofiles import open
|
||||||
from httpx import HTTPError
|
from httpx import HTTPError
|
||||||
from typing import TYPE_CHECKING
|
|
||||||
from source.module import ERROR
|
from source.module import ERROR
|
||||||
from source.module import Manager
|
from source.module import Manager
|
||||||
from source.module import logging
|
from source.module import logging
|
||||||
@@ -126,8 +128,9 @@ class Download:
|
|||||||
|
|
||||||
@re_download
|
@re_download
|
||||||
async def __download(self, url: str, path: Path, name: str, format_: str, log, bar):
|
async def __download(self, url: str, path: Path, name: str, format_: str, log, bar):
|
||||||
|
headers = self.headers.copy()
|
||||||
try:
|
try:
|
||||||
length, suffix = await self.__hand_file(url, format_, )
|
length, suffix = await self.__head_file(url, headers, format_, )
|
||||||
except HTTPError as error:
|
except HTTPError as error:
|
||||||
logging(log, str(error), ERROR)
|
logging(log, str(error), ERROR)
|
||||||
logging(
|
logging(
|
||||||
@@ -139,9 +142,10 @@ class Download:
|
|||||||
return False
|
return False
|
||||||
temp = self.temp.joinpath(f"{name}.{suffix}")
|
temp = self.temp.joinpath(f"{name}.{suffix}")
|
||||||
real = path.joinpath(f"{name}.{suffix}")
|
real = path.joinpath(f"{name}.{suffix}")
|
||||||
self.__update_headers_range(temp, )
|
self.__update_headers_range(headers, temp, )
|
||||||
try:
|
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()
|
response.raise_for_status()
|
||||||
# self.__create_progress(
|
# self.__create_progress(
|
||||||
# bar,
|
# bar,
|
||||||
@@ -183,14 +187,16 @@ class Download:
|
|||||||
def __extract_type(cls, content: str) -> str:
|
def __extract_type(cls, content: str) -> str:
|
||||||
return cls.CONTENT_TYPE_MAP.get(content, "")
|
return cls.CONTENT_TYPE_MAP.get(content, "")
|
||||||
|
|
||||||
async def __hand_file(self,
|
async def __head_file(self,
|
||||||
url: str,
|
url: str,
|
||||||
|
headers: dict[str, str],
|
||||||
suffix: str,
|
suffix: str,
|
||||||
) -> [int, str]:
|
) -> [int, str]:
|
||||||
response = await self.client.head(url,
|
# print(f"{url} Head Headers:", headers) # 调试代码
|
||||||
headers=self.headers | {
|
response = await self.client.head(
|
||||||
"Range": "bytes=0-",
|
url,
|
||||||
}, )
|
headers=headers,
|
||||||
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
suffix = self.__extract_type(
|
suffix = self.__extract_type(
|
||||||
response.headers.get("Content-Type")) or suffix
|
response.headers.get("Content-Type")) or suffix
|
||||||
@@ -202,6 +208,10 @@ class Download:
|
|||||||
def __get_resume_byte_position(file: Path) -> int:
|
def __get_resume_byte_position(file: Path) -> int:
|
||||||
return file.stat().st_size if file.is_file() else 0
|
return file.stat().st_size if file.is_file() else 0
|
||||||
|
|
||||||
def __update_headers_range(self, file: Path) -> int:
|
def __update_headers_range(
|
||||||
self.headers["Range"] = f"bytes={(p := self.__get_resume_byte_position(file))}-"
|
self,
|
||||||
|
headers: dict[str, str],
|
||||||
|
file: Path,
|
||||||
|
) -> int:
|
||||||
|
headers["Range"] = f"bytes={(p := self.__get_resume_byte_position(file))}-"
|
||||||
return p
|
return p
|
||||||
|
|||||||
Reference in New Issue
Block a user