def download(): song_id = request.args.get('song_id') songName = request.args.get('songName') + '.mp3' if (None == song_id or '' == song_id): return 'error' params = getAllParams(song_id) resp = RequestUtil.post(song_url, params) #获取json中的url jsonObj = json.loads(resp) data = jsonObj['data'][0] if (None == data): return 'error' try: url = data['url'] r = requests.get(url) if r.status_code != 200: raise Exception('超时-----') response = make_response(r.content) response.headers['Content-Type'] = 'audio/x-mpeg;charset=utf-8' response.headers[ 'Content-Disposition'] = 'attachment; filename=' + songName.encode( ) return response except Exception as e: print '获取url参数异常' print str(e) return 'error'
def download_dm(self, cid, filepath): request = BilibiliApi.build_dm_api_request(cid) response = RequestUtil.do_request(request, load_json=False) self.before_response(response) SaveTool.saveText(response.raw_response.text, f'{filepath}.xml', encoding=response.raw_response.encoding)
def get_oid_info_by_page(self, page): request = BilibiliApi.build_oid_api_request(self.up_id, page, order=self.order, keyword=self.keyword) response = RequestUtil.do_request(request) self.before_response(response) return self._decode_oid_info(response.data)
def playList(): order = request.args.get('order') offset = request.args.get('offset') if (None == order or '' == order): order = 'hot' if (offset == None or '' == order): offset = 35 target_url = URL + "?order=" + order + "&offset=" + offset text = RequestUtil.get(target_url, {}) return text
def download_video(self, url, filename): final_filename = PathUtil.join_path(CONFIG['DOWNLOAD_PATH'], filename) if PathUtil.check_path(final_filename): print(f'{final_filename} exists, stop downloading') return final_filename print(f'{filename} download start') request = BilibiliApi.build_video_download_request(url) response = RequestUtil.do_request(request, load_json=False) self.before_response(response) self.save_video(response.raw_response, final_filename) return final_filename
def test_bilibili_sign_cid_api(self, cid): request = BilibiliApi.build_sign_cid_api_request(cid) response = RequestUtil.do_request(request) assert response.status_code == 200 data = response.json assert 'quality' in data assert 'format' in data if 'durl' in data: assert isinstance(data['durl'], list) elif 'dash' in data: assert isinstance(data['dash'], dict) else: assert False
def getSongUrl(): song_id = request.args.get('song_id') if (None == song_id or '' == song_id): return 'error' params = getAllParams(song_id) resp = RequestUtil.post(song_url, params) #获取json中的url #jsonObj = json.loads(json_string) jsonObj = json.loads(resp) data = jsonObj['data'][0] if (None == data): return 'error' try: url = data['url'] res = {'url': url, 'msg': 'success'} return json.dumps(res) except BaseException: print '获取url参数异常' return 'error'
def download_video(self, url, filename): if PathUtil.check_path(filename): PathUtil.remove(filename) retry_times = 2 while retry_times > 0: try: request = BilibiliApi.build_video_download_request(url) response = RequestUtil.do_request(request, load_json=False, stream=True) self.before_response(response) self.save_video(response.raw_response, filename) break except: print( f'{filename} download fail, retry times = {retry_times}, restart...' ) retry_times -= 1 if retry_times == 0: raise Exception('retry times exceed, stop downloading...') continue
def test_bilibili_archive_api(self, cid): request = BilibiliApi.build_dm_api_request(cid) response = RequestUtil.do_request(request, load_json=False) assert response.status_code == 200
def do_request_and_check_response(request): response = RequestUtil.do_request(request) assert response.status_code == 200 assert response.code == 0 assert isinstance(response.data, dict) return response.data
def get_sign_video_download_info(self, cid): request = BilibiliApi.build_sign_cid_api_request(cid) response = RequestUtil.do_request(request) self.before_response(response) return self._decode_video_download_info(response.json)
def get_video_info(self): request = BilibiliApi.build_aid_api_request(self.aid) response = RequestUtil.do_request(request) self.before_response(response) return self._decode_video_info(response.data)
def orderList(): id = request.args.get('id') target_url = order_url + id return RequestUtil.get(target_url, {})