diff --git a/src/you_get/extractors/acfun.py b/src/you_get/extractors/acfun.py index b83c2859..9205b1b8 100644 --- a/src/you_get/extractors/acfun.py +++ b/src/you_get/extractors/acfun.py @@ -111,6 +111,18 @@ def acfun_download_by_vid(vid, title, output_dir='.', merge=True, info_only=Fals def acfun_download(url, output_dir='.', merge=True, info_only=False, **kwargs): assert re.match(r'https?://[^\.]*\.*acfun\.[^\.]+/(\D|bangumi)/\D\D(\d+)', url) + def getM3u8UrlFromCurrentVideoInfo(currentVideoInfo): + if 'playInfos' in currentVideoInfo: + return currentVideoInfo['playInfos'][0]['playUrls'][0] + elif 'ksPlayJson' in currentVideoInfo: + ksPlayJson = json.loads( currentVideoInfo['ksPlayJson'] ) + representation = ksPlayJson.get('adaptationSet')[0].get('representation') + reps = [] + for one in representation: + reps.append( (one['width']* one['height'], one['url'], one['backupUrl']) ) + return max(reps)[1] + + if re.match(r'https?://[^\.]*\.*acfun\.[^\.]+/\D/\D\D(\d+)', url): html = get_content(url, headers=fake_headers) json_text = match1(html, r"(?s)videoInfo\s*=\s*(\{.*?\});") @@ -122,37 +134,18 @@ def acfun_download(url, output_dir='.', merge=True, info_only=False, **kwargs): if len(video_list) > 1: title += " - " + [p.get('title') for p in video_list if p.get('id') == vid][0] currentVideoInfo = json_data.get('currentVideoInfo') - if 'playInfos' in currentVideoInfo: - m3u8_url = currentVideoInfo['playInfos'][0]['playUrls'][0] - elif 'ksPlayJson' in currentVideoInfo: - ksPlayJson = json.loads( currentVideoInfo['ksPlayJson'] ) - representation = ksPlayJson.get('adaptationSet').get('representation') - reps = [] - for one in representation: - reps.append( (one['width']* one['height'], one['url'], one['backupUrl']) ) - m3u8_url = max(reps)[1] - + m3u8_url = getM3u8UrlFromCurrentVideoInfo(currentVideoInfo) elif re.match("https?://[^\.]*\.*acfun\.[^\.]+/bangumi/aa(\d+)", url): html = get_content(url, headers=fake_headers) - tag_script = match1(html, r'') + tag_script = match1(html, r'') json_text = tag_script[tag_script.find('{') : tag_script.find('};') + 1] json_data = json.loads(json_text) title = json_data['bangumiTitle'] + " " + json_data['episodeName'] + " " + json_data['title'] vid = str(json_data['videoId']) up = "acfun" - play_info = get_content("https://www.acfun.cn/rest/pc-direct/play/playInfo/m3u8Auto?videoId=" + vid, headers=fake_headers) - play_url = json.loads(play_info)['playInfo']['streams'][0]['playUrls'][0] - m3u8_all_qualities_file = get_content(play_url) - m3u8_all_qualities_lines = m3u8_all_qualities_file.split('#EXT-X-STREAM-INF:')[1:] - highest_quality_line = m3u8_all_qualities_lines[0] - for line in m3u8_all_qualities_lines: - bandwith = int(match1(line, r'BANDWIDTH=(\d+)')) - if bandwith > int(match1(highest_quality_line, r'BANDWIDTH=(\d+)')): - highest_quality_line = line - #TODO: 应由用户指定清晰度 - m3u8_url = match1(highest_quality_line, r'\n([^#\n]+)$') - m3u8_url = play_url[:play_url.rfind("/")+1] + m3u8_url + currentVideoInfo = json_data.get('currentVideoInfo') + m3u8_url = getM3u8UrlFromCurrentVideoInfo(currentVideoInfo) else: raise NotImplemented diff --git a/tests/test.py b/tests/test.py index 00bd4cbb..33503b8d 100644 --- a/tests/test.py +++ b/tests/test.py @@ -39,6 +39,7 @@ class YouGetTests(unittest.TestCase): def test_acfun(self): acfun.download('https://www.acfun.cn/v/ac11701912', info_only=True) + acfun.download('https://www.acfun.cn/bangumi/aa6002986', info_only=True) def test_bilibil(self): bilibili.download(