Skip to content

Commit

Permalink
fix: 修复部分文件无法下载的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
dataabc committed Dec 15, 2020
1 parent e0d70b3 commit 3ff0d28
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions weibo.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,18 @@ def download_one_file(self, url, file_path, type, weibo_id):
if not os.path.isfile(file_path):
s = requests.Session()
s.mount(url, HTTPAdapter(max_retries=5))
while True:
flag = True
while flag:
flag = False
downloaded = s.get(url,
headers=self.headers,
timeout=(5, 10),
verify=False)
if downloaded.content.endswith(b'\xff\xd9') or downloaded.content.endswith(b'\xaeB`\x82'):
break
headers=self.headers,
timeout=(5, 10),
verify=False)
if (url.endswith(('jpg', 'jpeg'))
and not downloaded.content.endswith(b'\xff\xd9')
) or (url.endswith('png') and
not downloaded.content.endswith(b'\xaeB`\x82')):
flag = True
with open(file_path, 'wb') as f:
f.write(downloaded.content)
except Exception as e:
Expand Down Expand Up @@ -607,7 +612,8 @@ def get_one_weibo(self, info):
weibo_info = info['mblog']
weibo_id = weibo_info['id']
retweeted_status = weibo_info.get('retweeted_status')
is_long = True if weibo_info.get('pic_num') > 9 else weibo_info.get('isLongText')
is_long = True if weibo_info.get(
'pic_num') > 9 else weibo_info.get('isLongText')
if retweeted_status and retweeted_status.get('id'): # 转发
retweet_id = retweeted_status.get('id')
is_long_retweet = retweeted_status.get('isLongText')
Expand Down

0 comments on commit 3ff0d28

Please sign in to comment.