Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 相册名称中含有空格导致下载文件出错;下载文件时加入 timeout #1

Merged
merged 2 commits into from
Dec 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions download.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

@logging_wrap
def download_media(url, dir, file_name):
result = False
s = "\rdownloading %s -> %05.2f%% "
chunk_size = 1024
with closing(requests.get(url, stream=True)) as r:
with requests.get(url, stream=True, timeout=30) as r:
extension = "jpg"
if "content-type" in r.headers:
content_type = r.headers["content-type"]
Expand All @@ -36,7 +37,9 @@ def download_media(url, dir, file_name):
current_len += len(data)
percent = 100 * current_len / total_len
print(s % (url, percent), end="")
print("\n%s is downloaded" % url)
result = True
print("\n%s is downloaded" % url)
return result


@logging_wrap
Expand Down Expand Up @@ -115,7 +118,7 @@ def download(self):

download_dir = temp[1]
id = temp[2]
download_media(url, download_dir, id)
fupdate.write("%s\n" % url)
fupdate.flush()
if download_media(url, download_dir, id):
fupdate.write("%s\n" % url)
fupdate.flush()
print("downloading done")
4 changes: 4 additions & 0 deletions tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def get_json_data_from_response(resp_text):


def purge_file_name(file_name):
temp = file_name
file_name = ""
for c in temp:
file_name += "_" if c.isspace() else c
escape_chars = "/\\:*?\"<>|"
for c in escape_chars:
file_name = file_name.replace(c, "%%%X" % ord(c))
Expand Down