コード例 #1
0
ファイル: subhd.py プロジェクト: awesome-archive/GetSubtitles
    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