def download_file(self, file_name, sub_url): """ 传入字幕页面链接, 字幕包标题, 返回压缩包类型,压缩包字节数据 """ s = requests.session() r = s.get(sub_url, headers=self.headers) bs_obj = BeautifulSoup(r.text, 'html.parser') a = bs_obj.find('div', {'class': 'subtitle-links'}).a download_link = a.attrs['href'] 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 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
def download_file(self, file_name, download_link): try: with closing(requests.get(download_link, stream=True)) as response: filename = response.headers['Content-Disposition'] 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)) except requests.Timeout: return None, None, 'false' if '.rar' in filename: datatype = '.rar' elif '.zip' in filename: datatype = '.zip' elif '.7z' in filename: datatype = '.7z' else: datatype = 'Unknown' with open('test.zip', 'wb') as f: f.write(sub_data_bytes) return datatype, sub_data_bytes
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'
def download_file(self, file_name, sub_url): """ 传入字幕页面链接, 字幕包标题, 返回压缩包类型,压缩包字节数据 """ sid = sub_url.split('/')[-1] while True: r = requests.post('http://subhd.com/ajax/down_ajax', data={'sub_id': sid}, headers=self.headers) message = r.content.decode('unicode-escape') if '下载频率过高,请等候一分钟' in message: bar = ProgressBar('├ 下载频率过高,请等候一分钟', count_time=60) bar.count_down() else: break download_link = re.search('http:.*(?=")', r.content.decode('unicode-escape')).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('├ 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 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