Skip to content

Commit

Permalink
Merge branch 'fix-acfun' of https://github.com/MMMartt/you-get into M…
Browse files Browse the repository at this point in the history
…MMartt-fix-acfun
  • Loading branch information
soimort committed Sep 20, 2020
2 parents 4abc4bd + c14605f commit e0615e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
39 changes: 16 additions & 23 deletions src/you_get/extractors/acfun.py
Original file line number Diff line number Diff line change
Expand Up @@ -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*(\{.*?\});")
Expand All @@ -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'<script>window\.pageInfo([^<]+)</script>')
tag_script = match1(html, r'<script>\s*window\.pageInfo([^<]+)</script>')
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
Expand Down
1 change: 1 addition & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def test_youtube(self):

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(
Expand Down

0 comments on commit e0615e3

Please sign in to comment.