From 71a1825be5d935c3aa45ca81b12dbcabd3b95c52 Mon Sep 17 00:00:00 2001 From: MaxwellGoblin Date: Wed, 24 May 2017 02:16:32 +0800 Subject: [PATCH 1/2] [acfun]fix youku cloud source --- src/you_get/common.py | 23 +++++++++++++++ src/you_get/extractors/acfun.py | 52 ++++++++++++++++++++++++++------- 2 files changed, 65 insertions(+), 10 deletions(-) diff --git a/src/you_get/common.py b/src/you_get/common.py index 8d4d2d76..e98a80e9 100755 --- a/src/you_get/common.py +++ b/src/you_get/common.py @@ -138,6 +138,29 @@ if sys.stdout.isatty(): else: default_encoding = locale.getpreferredencoding().lower() +def rc4(key, data): +#all encryption algo should work on bytes + assert type(key)==type(data) and type(key) == type(b'') + state = list(range(256)) + j = 0 + for i in range(256): + j += state[i] + key[i % len(key)] + j &= 0xff + state[i], state[j] = state[j], state[i] + + i = 0 + j = 0 + out_list = [] + for char in data: + i += 1 + i &= 0xff + j += state[i] + j &= 0xff + state[i], state[j] = state[j], state[i] + prn = state[(state[i] + state[j]) & 0xff] + out_list.append(char ^ prn) + + return bytes(out_list) def maybe_print(*s): try: print(*s) except: pass diff --git a/src/you_get/extractors/acfun.py b/src/you_get/extractors/acfun.py index 6bb0dca4..57ca616b 100644 --- a/src/you_get/extractors/acfun.py +++ b/src/you_get/extractors/acfun.py @@ -10,11 +10,32 @@ from .sina import sina_download_by_vid from .tudou import tudou_download_by_iid from .youku import youku_download_by_vid, youku_open_download_by_vid -import json, re +import json +import re +import base64 def get_srt_json(id): url = 'http://danmu.aixifan.com/V2/%s' % id - return get_html(url) + return get_content(url) + +def youku_acfun_proxy(vid, sign): + url = 'http://aplay-vod.cn-beijing.aliyuncs.com/acfun/web?vid={}&ct=85&ev=2&sign={}'.format(vid, sign) + json_data = json.loads(get_content(url))['data'] + enc_text = base64.b64decode(json_data) + dec_text = rc4(b'2da3ca9e', enc_text).decode('utf8') + youku_json = json.loads(dec_text) + + yk_streams = {} + for stream in youku_json['stream']: + tp = stream['stream_type'] + yk_streams[tp] = [], stream['total_size'] + if stream.get('segs'): + for seg in stream['segs']: + yk_streams[tp][0].append(seg['url']) + else: + yk_streams[tp] = stream['m3u8'], stream['total_size'] + + return yk_streams def acfun_download_by_vid(vid, title, output_dir='.', merge=True, info_only=False, **kwargs): """str, str, str, bool, bool ->None @@ -26,7 +47,7 @@ def acfun_download_by_vid(vid, title, output_dir='.', merge=True, info_only=Fals """ #first call the main parasing API - info = json.loads(get_html('http://www.acfun.tv/video/getVideo.aspx?id=' + vid)) + info = json.loads(get_content('http://www.acfun.tv/video/getVideo.aspx?id=' + vid)) sourceType = info['sourceType'] @@ -47,12 +68,23 @@ def acfun_download_by_vid(vid, title, output_dir='.', merge=True, info_only=Fals letvcloud_download_by_vu(sourceId, '2d8c027396', title, output_dir=output_dir, merge=merge, info_only=info_only) elif sourceType == 'zhuzhan': #As in Jul.28.2016, Acfun is using embsig to anti hotlink so we need to pass this - embsig = info['encode'] - a = 'http://api.aixifan.com/plays/%s' % vid - s = json.loads(get_content(a, headers={'deviceType': '2'})) - if s['data']['source'] == "zhuzhan-youku": - sourceId = s['data']['sourceId'] - youku_open_download_by_vid(client_id='908a519d032263f8', vid=sourceId, title=title, output_dir=output_dir,merge=merge, info_only=info_only, embsig = embsig, **kwargs) +#In Mar. 2017 there is a dedicated ``acfun_proxy'' in youku cloud player +#old code removed + yk_streams = youku_acfun_proxy(info['sourceId'], info['encode']) + seq = ['mp4hd3', 'mp4hd2', 'mp4hd', 'flvhd'] + for t in seq: + if yk_streams.get(t): + preferred = yk_streams[t] + break +#total_size in the json could be incorrect(F.I. 0) + size = 0 + for url in preferred[0]: + _, _, seg_size = url_info(url) + size += seg_size +#fallback to flvhd is not quite possible + print_info(site_info, title, 'mp4', size) + if not info_only: + download_urls(preferred[0], title, 'mp4', size, output_dir=output_dir, merge=merge) else: raise NotImplementedError(sourceType) @@ -71,7 +103,7 @@ 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'http://[^\.]+.acfun.[^\.]+/\D/\D\D(\d+)', url) - html = get_html(url) + html = get_content(url) title = r1(r'data-title="([^"]+)"', html) title = unescape_html(title) From da1339b0b035be7a176605295215d9ad2103d715 Mon Sep 17 00:00:00 2001 From: MaxwellGoblin Date: Sat, 27 May 2017 20:14:52 +0800 Subject: [PATCH 2/2] [acfun]update interface --- src/you_get/extractors/acfun.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/you_get/extractors/acfun.py b/src/you_get/extractors/acfun.py index 57ca616b..95c66d80 100644 --- a/src/you_get/extractors/acfun.py +++ b/src/you_get/extractors/acfun.py @@ -19,10 +19,10 @@ def get_srt_json(id): return get_content(url) def youku_acfun_proxy(vid, sign): - url = 'http://aplay-vod.cn-beijing.aliyuncs.com/acfun/web?vid={}&ct=85&ev=2&sign={}'.format(vid, sign) + url = 'http://aplay-vod.cn-beijing.aliyuncs.com/acfun/web?vid={}&ct=85&ev=3&sign={}'.format(vid, sign) json_data = json.loads(get_content(url))['data'] enc_text = base64.b64decode(json_data) - dec_text = rc4(b'2da3ca9e', enc_text).decode('utf8') + dec_text = rc4(b'8bdc7e1a', enc_text).decode('utf8') youku_json = json.loads(dec_text) yk_streams = {}