Archived
添加了创建文件夹的代码
This commit is contained in:
3 files changed
+18
-67
No files matched your search
+18
-13
@@ -11,6 +11,7 @@ import hashlib
|
||||
import sys
|
||||
import requests
|
||||
import threading
|
||||
from bin.utils.logger import logger
|
||||
|
||||
|
||||
class MulThreadDownload(threading.Thread):
|
||||
@@ -30,12 +31,12 @@ class MulThreadDownload(threading.Thread):
|
||||
多线程下载
|
||||
:return:
|
||||
"""
|
||||
print(f'I: 线程: Thread-{self.name} 开始下载')
|
||||
logger.info(f'线程: Thread-{self.name} 开始下载')
|
||||
headers = {"Range": "bytes=%s-%s" % (self.startpos, self.endpos)}
|
||||
res = self.session.get(self.url, headers=headers)
|
||||
self.fd.seek(self.startpos)
|
||||
self.fd.write(res.content)
|
||||
print(f'I: 线程: Thread-{self.name} 结束下载')
|
||||
logger.info(f'线程: Thread-{self.name} 结束下载')
|
||||
|
||||
def run(self):
|
||||
"""
|
||||
@@ -62,21 +63,22 @@ class Check:
|
||||
data = fp.read()
|
||||
local_md5 = hashlib.md5(data).hexdigest()
|
||||
remote_md5 = self.session.get(self.remote_md5).json()['data'][0]
|
||||
print(f'I: 本地数据库md5: {local_md5}\nI: 远程数据库md5: {remote_md5}')
|
||||
logger.info(f'本地数据库md5: {local_md5}')
|
||||
logger.info(f'远程数据库md5: {remote_md5}')
|
||||
if local_md5 != remote_md5:
|
||||
print('E: 下载错误: 本地数据库md5和远程数据库md5检验不通过, 即将开始重新下载\nI: 本次下载将使用单线程下载')
|
||||
logger.error('下载错误: 本地数据库md5和远程数据库md5检验不通过, 即将开始重新下载\nI: 本次下载将使用单线程下载')
|
||||
single_download(self.assets_url)
|
||||
else:
|
||||
print('I: md5检验已通过')
|
||||
logger.info('md5检验已通过')
|
||||
|
||||
def download(self):
|
||||
"""
|
||||
开始下载
|
||||
:return:
|
||||
"""
|
||||
filesize = int(self.session.head(self.assets_url).headers['Content-Length'])
|
||||
filesize = int(self.session.get(self.assets_url).headers['Content-Length'])
|
||||
threaded_count = 3
|
||||
print(f'I: 数据库大小: {round(filesize / 1024 / 1024, 2)}Mb. 下载线程: {threaded_count}')
|
||||
logger.info(f'数据库大小: {round(filesize / 1024 / 1024, 2)}Mb. 下载线程: {threaded_count}')
|
||||
threading.BoundedSemaphore(threaded_count)
|
||||
step = filesize // threaded_count
|
||||
mtd_list = []
|
||||
@@ -113,24 +115,27 @@ def single_download(url):
|
||||
"""
|
||||
session = requests.Session()
|
||||
session.trust_env = False
|
||||
logger.info(f'正在使用单线程下载中')
|
||||
resp = session.get(url)
|
||||
print(f'I: 正在使用单线程下载中')
|
||||
with open('./bin/assets/theme.db', 'wb') as fp:
|
||||
fp.write(resp.content)
|
||||
print('I: 下载完成 正在检验文件md5')
|
||||
logger.info('下载完成 正在检验文件md5')
|
||||
with open('./bin/assets/theme.db', 'rb') as fp:
|
||||
data = fp.read()
|
||||
local_md5 = hashlib.md5(data).hexdigest()
|
||||
remote_md5 = session.get('https://themedatabases.vercel.app/md5').json()['data'][0]
|
||||
print(f'I: 本地数据库md5: {local_md5}\nI: 远程数据库md5: {remote_md5}')
|
||||
logger.info(f'本地数据库md5: {local_md5}')
|
||||
logger.info(f'远程数据库md5: {remote_md5}')
|
||||
if local_md5 != remote_md5:
|
||||
print('E: md5检验未通过请手动前往 https://themedatabases.vercel.app/assets 下载文件并放入./bin/assets文件夹内')
|
||||
logger.error('md5检验未通过请手动前往 https://themedatabases.vercel.app/assets 下载文件并放入./bin/assets文件夹内')
|
||||
sys.exit(-1)
|
||||
else:
|
||||
print('I: md5检验已通过')
|
||||
logger.info('md5检验已通过')
|
||||
|
||||
|
||||
if __name__ != '__main__':
|
||||
if os.path.exists('./app.py') and not os.path.exists('./bin/log'):
|
||||
os.mkdir('./bin/log')
|
||||
if not os.path.exists('./bin/assets/theme.db'):
|
||||
print('E: 没有检测到本地主题数据库即将开始下载')
|
||||
logger.error('没有检测到本地主题数据库即将开始下载')
|
||||
Check().download()
|
||||
Reference in New Issue
Block a user