|
@@ -36,34 +36,49 @@ class AttachmentDownloader(AliYunService):
|
|
|
return "{}.{}".format(fid, filetype)
|
|
|
|
|
|
@staticmethod
|
|
|
- def _file_size(file: str):
|
|
|
- _kb = float(getsize(file)) / 1024
|
|
|
- if _kb >= 1024:
|
|
|
- _M = _kb / 1024
|
|
|
+ def _calc_size(size: float):
|
|
|
+ if size >= 1024:
|
|
|
+ _M = size / 1024
|
|
|
if _M >= 1024:
|
|
|
_G = _M / 1024
|
|
|
return "{:.1f} G".format(_G)
|
|
|
else:
|
|
|
return "{:.1f} M".format(_M)
|
|
|
else:
|
|
|
- return "{:.1f} kb".format(_kb)
|
|
|
+ return "{:.1f} kb".format(size)
|
|
|
|
|
|
- @staticmethod
|
|
|
- def get_stream(file, response):
|
|
|
+ def _file_size(self, file):
|
|
|
+ _kb = float(getsize(file)) / 1024
|
|
|
+ return self._calc_size(_kb)
|
|
|
+
|
|
|
+ def stream_download(self, file_path, response):
|
|
|
stream = io.BytesIO()
|
|
|
- chunk_size = 1024 # 单次请求最大值
|
|
|
- with open(file, 'wb') as f:
|
|
|
+ if 'content-length' in response.headers:
|
|
|
+ content_size = int(response.headers['content-length']) # 内容体总大小
|
|
|
+ else:
|
|
|
+ content_size = len(response.content)
|
|
|
+ size = self._calc_size(float(content_size) / 1024)
|
|
|
+ logger.info(f'>>> 下载附件:{file_path} - 大小:{size}')
|
|
|
+
|
|
|
+ chunk_size = 1024 # 单次请求数据块最大值
|
|
|
+ data_chunk_size = 0 # 下载数据块
|
|
|
+ with open(file_path, 'wb') as f:
|
|
|
for data in response.iter_content(chunk_size=chunk_size):
|
|
|
stream.write(data)
|
|
|
f.write(data)
|
|
|
+ data_chunk_size += len(data)
|
|
|
+ percent = (data_chunk_size / content_size) * 100
|
|
|
+ print("\r文件下载进度:%d%%(%d/%d) - %s" % (
|
|
|
+ percent, data_chunk_size, content_size, file_path), end=" ")
|
|
|
return stream.getvalue()
|
|
|
|
|
|
- def _download(
|
|
|
+ def _fetch_file(
|
|
|
self,
|
|
|
+ method: str,
|
|
|
url: str,
|
|
|
file: str,
|
|
|
- enable_proxy=False,
|
|
|
- allow_show_exception=False,
|
|
|
+ enable_proxy: bool,
|
|
|
+ allow_show_exception: bool,
|
|
|
**kwargs
|
|
|
):
|
|
|
request_params = {}
|
|
@@ -76,20 +91,11 @@ class AttachmentDownloader(AliYunService):
|
|
|
retries = 0
|
|
|
while retries < 3:
|
|
|
try:
|
|
|
- with requests.get(url, **request_params) as req:
|
|
|
- for req_kw in req_keywords:
|
|
|
- if req_kw in req.text:
|
|
|
- with requests.post(url, **request_params) as req:
|
|
|
- if req.status_code == 200:
|
|
|
- return self.get_stream(file, req)
|
|
|
- else:
|
|
|
- retries += 1
|
|
|
+ with requests.request(method, url, **request_params) as req:
|
|
|
+ if req.status_code == 200:
|
|
|
+ return self.stream_download(file, req)
|
|
|
else:
|
|
|
- if req.status_code == 200:
|
|
|
- return self.get_stream(file, req)
|
|
|
- else:
|
|
|
- retries += 1
|
|
|
-
|
|
|
+ retries += 1
|
|
|
except requests.RequestException:
|
|
|
if allow_show_exception:
|
|
|
traceback.print_exc()
|
|
@@ -110,19 +116,17 @@ class AttachmentDownloader(AliYunService):
|
|
|
):
|
|
|
if not file_name or not file_type or not download_url:
|
|
|
raise AttachmentError
|
|
|
-
|
|
|
+ request_method = kwargs.pop('method', 'get')
|
|
|
file_type = file_type.strip()
|
|
|
-
|
|
|
- file_name = clean_file_name(file_name,file_type)
|
|
|
-
|
|
|
+ file_name = clean_file_name(file_name, file_type)
|
|
|
download_url = judge_file_url(download_url)
|
|
|
|
|
|
for app_param in modify_file_url_list:
|
|
|
download_url = app_param(download_url)
|
|
|
|
|
|
local_tmp_file = self._create_file(file_name, file_type)
|
|
|
-
|
|
|
- file_stream = self._download(
|
|
|
+ file_stream = self._fetch_file(
|
|
|
+ request_method,
|
|
|
download_url,
|
|
|
local_tmp_file,
|
|
|
enable_proxy,
|
|
@@ -154,4 +158,3 @@ class AttachmentDownloader(AliYunService):
|
|
|
return result
|
|
|
else:
|
|
|
return {}
|
|
|
-
|