Skip to content

Commit

Permalink
fix: 相册名称中含有空格导致下载文件出错;下载文件时加入 timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwpf committed Dec 24, 2018
1 parent 691cabd commit cdf62ff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
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

1 comment on commit cdf62ff

@wwwpf
Copy link
Owner Author

@wwwpf wwwpf commented on cdf62ff Dec 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#7 #6

Please sign in to comment.