download()增加是否显示信息选项

This commit is contained in:
g1879 2020-05-28 17:01:00 +08:00
parent f206df5bb8
commit e4c7b81fa3

View File

@ -109,8 +109,17 @@ class SessionPage(object):
self._url_available = True if self._response and self._response.status_code == 200 else False
return self._url_available
def download(self, file_url: str, goal_path: str = None, rename: str = None, **kwargs) -> tuple:
"""下载一个文件生成的response不写入self._response是临时的"""
def download(self, file_url: str, goal_path: str = None, rename: str = None, show_msg: bool = False,
**kwargs) -> tuple:
"""下载一个文件
生成的response不写入self._response是临时的
:param file_url: 文件url
:param goal_path: 存放路径url
:param rename: 重命名
:param kwargs: 连接参数
:param show_msg: 是否显示下载信息
:return: 元组bool和状态信息成功时信息为文件名
"""
goal_path = goal_path or OptionsManager().get_value('paths', 'global_tmp_path')
if not goal_path:
raise IOError('No path specified.')
@ -121,6 +130,7 @@ class SessionPage(object):
r = self._make_response(file_url, mode='get', **kwargs)
if not r:
if show_msg:
print('Invalid link')
return False, 'Invalid link'
# -------------------获取文件名-------------------
@ -135,6 +145,7 @@ class SessionPage(object):
# 避免和现有文件重名
file_full_name = avoid_duplicate_name(goal_path, file_full_name)
# 打印要下载的文件
if show_msg:
print_txt = file_full_name if file_name == file_full_name else f'{file_name} -> {file_full_name}'
print(print_txt)
# -------------------开始下载-------------------
@ -166,6 +177,7 @@ class SessionPage(object):
full_path.unlink()
r.close()
# -------------------显示并返回值-------------------
if show_msg:
print(info, '\n')
info = file_full_name if download_status else info
return download_status, info