def download_file(self, file_name, sub_url):
""" ????????? ?????? ??????????????? """
sid = sub_url.split('/')[-1]
r = requests.post('http://subhd.com/ajax/down_ajax',
data={'sub_id': sid},
headers=self.headers)
content = r.content.decode('unicode-escape')
if json.loads(content)['success'] is False:
return None, None, 'false'
res = re.search('http:.*(?=")', r.content.decode('unicode-escape'))
download_link = res.group(0).replace('\\/', '/')
try:
with closing(requests.get(download_link, stream=True)) as response:
chunk_size = 1024 # ???????
# ??????
content_size = int(response.headers['content-length'])
bar = ProgressBar(prefix + ' Get',
file_name.strip(), content_size)
sub_data_bytes = b''
for data in response.iter_content(chunk_size=chunk_size):
sub_data_bytes += data
bar.refresh(len(sub_data_bytes))
# sub_data_bytes = requests.get(download_link, timeout=10).content
except requests.Timeout:
return None, None, 'false'
if 'rar' in download_link:
datatype = '.rar'
elif 'zip' in download_link:
datatype = '.zip'
elif '7z' in download_link:
datatype = '.7z'
else:
datatype = 'Unknown'
return datatype, sub_data_bytes, 'success'
评论列表
文章目录