Skip to content

The only downloader you need. 下载器的集大成者。

文件获取优化,反爬优化 (#22)

XiaoYouChR94e161d5eac7
diff --git a/app/common/download_task.py b/app/common/download_task.py
index 13d730da..88652063 100644
--- a/app/common/download_task.py
+++ b/app/common/download_task.py
@@ -17,7 +17,16 @@ from app.common.config import cfg
from app.common.methods import getProxy, getReadableSize
Headers = {
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.64"}
+ "accept-encoding": "gzip, deflate, br",
+ "accept-language": "zh-CN,zh;q=0.9",
+ "cache-control": "max-age=0",
+ "cookie": "down_ip=1",
+ "sec-fetch-dest": "document",
+ "sec-fetch-mode": "navigate",
+ "sec-fetch-site": "none",
+ "sec-fetch-user": "?1",
+ "upgrade-insecure-requests": "1",
+ "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.64"}
urlRe = re.compile(r"^" +
"((?:https?|ftp)://)" +
@@ -160,7 +169,7 @@ class DownloadTask(QThread):
def __getLinkInfo(self):
try:
- response = httpx.head(self.url, follow_redirects=True)
+ response = httpx.head(self.url, headers=Headers, verify=False, proxy=getProxy(), follow_redirects=True)
response.raise_for_status() # 如果状态码不是 2xx,抛出异常
head = response.headers
@@ -175,6 +184,8 @@ class DownloadTask(QThread):
self.fileSize = int(head["content-length"])
self.ableToParallelDownload = True
+ print(head)
+
# 获取文件名
if not self.fileName:
try:
@@ -223,16 +234,19 @@ class DownloadTask(QThread):
logger.debug(f"方法2获取文件名成功, 文件名:{self.fileName}")
except (KeyError, IndexError) as e:
- # 处理没有文件名的情况
+
logger.info(f"方法2获取文件名失败, KeyError or IndexError:{e}")
self.fileName = urlparse(self.url).path.split('/')[-1]
- logger.debug(f"方法3获取文件名成功, 文件名:{self.fileName}")
- except Exception as e:
- # 什么都 Get 不到的情况
- logger.info(f"获取文件名失败, Exception:{e}")
- content_type = head["content-type"].split('/')[-1]
- self.fileName = f"downloaded_file{int(time())}.{content_type}"
- logger.debug(f"方法4获取文件名成功, 文件名:{self.fileName}")
+
+ if self.fileName:
+ logger.debug(f"方法3获取文件名成功, 文件名:{self.fileName}")
+ else:
+ logger.debug("方法3获取文件名失败, 文件名为空")
+ # 什么都 Get 不到的情况
+ logger.info(f"获取文件名失败, 错误:{e}")
+ content_type = head["content-type"].split('/')[-1]
+ self.fileName = f"downloaded_file{int(time())}.{content_type}"
+ logger.debug(f"方法4获取文件名成功, 文件名:{self.fileName}")
# 获取文件路径
if not self.filePath and Path(self.filePath).is_dir() == False:
@@ -283,8 +297,7 @@ class DownloadTask(QThread):
finished = False
while not finished:
try:
- download_headers = {"Range": f"bytes={worker.process}-{worker.endPos}",
- "User-Agent": Headers["User-Agent"]}
+ download_headers = Headers.update({"Range": f"bytes={worker.process}-{worker.endPos}"}) # 添加范围
async with worker.client.stream(url=self.url, headers=download_headers, timeout=30,
method="GET") as res:
diff --git a/app/components/task_card.py b/app/components/task_card.py
index 824cc1f4..a5ba8d4e 100644
--- a/app/components/task_card.py
+++ b/app/components/task_card.py
@@ -230,24 +230,28 @@ class TaskCard(CardWidget, Ui_TaskCard):
except Exception as e:
raise e
- # 删除记录文件
- with open("{}/Ghost Downloader 记录文件".format(cfg.appPath), "r", encoding="utf-8") as f:
- _ = f.read()
-
- _ = _.replace(str({"url": self.url, "fileName": self.fileName, "filePath": str(self.filePath),
- "blockNum": self.maxBlockNum, "status": self.status}) + "\n", "")
-
- with open("{}/Ghost Downloader 记录文件".format(cfg.appPath), "w", encoding="utf-8") as f:
- f.write(_)
-
except Exception as e:
logger.warning(f"Task:{self.fileName}, 删除时遇到错误: {e}")
finally:
- self.status = "canceled"
- # Remove Widget
- self.parent().parent().parent().expandLayout.removeWidget(self)
- self.hide()
+
+ try:
+ # 删除记录文件
+ with open("{}/Ghost Downloader 记录文件".format(cfg.appPath), "r", encoding="utf-8") as f:
+ _ = f.read()
+
+ _ = _.replace(str({"url": self.url, "fileName": self.fileName, "filePath": str(self.filePath),
+ "blockNum": self.maxBlockNum, "status": self.status}) + "\n", "")
+
+ with open("{}/Ghost Downloader 记录文件".format(cfg.appPath), "w", encoding="utf-8") as f:
+ f.write(_)
+
+ finally:
+
+ self.status = "canceled"
+ # Remove Widget
+ self.parent().parent().parent().expandLayout.removeWidget(self)
+ self.hide()
def __changeInfo(self, content: list):
# 理论来说 worker 直增不减 所以ProgressBar不用考虑线程减少的问题