添加下载用时输出

This commit is contained in:
MarkusJoe
2022-03-04 18:14:34 +08:00
parent 49c09419d9
commit b5401ac501
9 files changed
+129 -865

No files matched your search

+25 -11
View File
@@ -7,27 +7,28 @@
import os
import time
import requests
import threading
from bin.utils.logger import logger
class Threaded(threading.Thread):
def __init__(self, s, e, fp, id_, url):
def __init__(self, s, e, id_, url, name):
"""
初始化类
:param s: 开始点
:param e: 结束点
:param fp: 文件操作
:param id_: 线程id
:param url: 文件url
:param name: 文件名
"""
super().__init__()
super(Threaded, self).__init__()
self.start_ = s
self.end_ = e
self.fp = fp
self.id = id_
self.url = url
self.name = name
def download(self):
"""
@@ -37,8 +38,9 @@ class Threaded(threading.Thread):
"""
logger.info(f'线程: {self.id} 开始下载')
res = requests.get(self.url, headers={'Range': f'Bytes={self.start_}-{self.end_}'}).content
self.fp.seek(self.start_)
self.fp.write(res)
with open(self.name, 'r+b') as fp:
fp.seek(self.start_)
fp.write(res)
logger.info(f'线程: {self.id} 结束下载')
def run(self):
@@ -49,7 +51,20 @@ class Threaded(threading.Thread):
self.download()
def main(url: str, name: str, path: str = '.', workers: int = 8):
def cost(func):
def wrapper(*args, **kwargs):
logger.info('文件开始开始下载')
s = time.time()
execute = func(*args, **kwargs)
e = time.time()
logger.info(f'下载结束, 用时: {round(e - s, 2)} s')
return execute
return wrapper
@cost
def main(url: str, name: str, path: str = '.', workers: int = 4):
"""
主函数
:param url:
@@ -60,14 +75,13 @@ def main(url: str, name: str, path: str = '.', workers: int = 8):
"""
logger.info(f'本次下载使用线程数: {workers}')
file_size = int(requests.get(url).headers['Content-Length'])
if requests.get(url).status_code == '302':
if requests.get(url).status_code == 302:
url = requests.get(url).headers['Location']
logger.warning(f'下载地址已重定向到了: {url}')
logger.info(f'文件大小: {round(file_size / 1024 / 1024, 2)} Mb')
offset = int(file_size / workers)
start = 0
open(path + name, 'wb').close()
fp = open(path + name, 'r+b')
for i in range(workers):
if i == workers - 1:
end = file_size
@@ -75,7 +89,7 @@ def main(url: str, name: str, path: str = '.', workers: int = 8):
end = i * offset
else:
end = offset
threads = Threaded(start, end, fp, i, url)
threads = Threaded(start, end, i, url, path + name)
threads.start()
threads.join()
start = end + 1
@@ -84,4 +98,4 @@ def main(url: str, name: str, path: str = '.', workers: int = 8):
if __name__ != '__main__':
if not os.path.exists('./bin/db/data.db'):
logger.warning('数据库文件不存在, 即将开始下载')
main('https://themedatabase.vercel.app/assets', 'data.db', './bin/db/', 6)
main('https://themedatabase.vercel.app/assets', 'data.db', './bin/db/')