压缩datalist传输大小

This commit is contained in:
leini 2023-09-11 23:55:35 +08:00
parent 0ed184824b
commit 2393a6bb82
1 changed files with 18 additions and 13 deletions

View File

@ -27,6 +27,7 @@ import os
import logging import logging
import json import json
import requests import requests
import zipfile
logging.basicConfig( logging.basicConfig(
format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s', format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s',
@ -68,24 +69,28 @@ def deal_download(datalist_item, url_prefix_str, datalist_path):
to deal datalist file download to deal datalist file download
""" """
logging.info('%s%s', 'start to download datalist ', datalist_item['name']) logging.info('%s%s', 'start to download datalist ', datalist_item['name'])
save_path = os.path.join(datalist_path, datalist_item['name']) save_path = datalist_path
if not os.path.exists(save_path): if not os.path.exists(save_path):
os.makedirs(save_path) os.makedirs(save_path)
logging.info('%s`%s`', 'Created directory ', save_path) logging.info('%s`%s`', 'Created directory ', save_path)
for filename in datalist_item['filelist']: zipfilename = datalist_item['zipfile']
tmp_url = url_prefix_str + datalist_item['name'] + '/' + filename tmp_url = url_prefix_str + zipfilename
save_filename = os.path.join(save_path, filename) save_filename = os.path.join(save_path, zipfilename)
rsp_listfile = requests.get(tmp_url) rsp = requests.get(tmp_url)
with open(save_filename, "wb") as file_pointer:
file_pointer.write(rsp.content)
if rsp.ok:
logging.info('%s `%s` %s', 'Download', zipfilename, 'complete')
else:
logging.error('%s%s%s%s%s', 'Can not download ', zipfilename,
' from ailemon\'s download server. ',
'http status ok is ', str(rsp.ok))
with open(save_filename, "wb") as file_pointer: f = zipfile.ZipFile(save_filename, 'r') # 压缩文件位置
file_pointer.write(rsp_listfile.content) f.extractall(save_path)
if rsp_listfile.ok: f.close()
logging.info('%s `%s` %s', 'Download', filename, 'complete') logging.info('%s `%s` %s', 'unzip', zipfilename, 'complete')
else:
logging.error('%s%s%s%s%s', 'Can not download ', filename,
' from ailemon\'s download server. ',
'http status ok is ', str(rsp_listfile.ok))
if num == len(body['datalist']): if num == len(body['datalist']):