YouTube: bite me

This commit is contained in:
Mort Yao 2013-08-17 15:11:03 +08:00
parent 66d82c9b51
commit 657c1e20c2

View File

@ -32,34 +32,30 @@ yt_codecs = [
{'itag': 17, 'container': '3GP', 'video_resolution': '144p', 'video_encoding': 'MPEG-4 Visual', 'video_profile': 'Simple', 'video_bitrate': '0.05', 'audio_encoding': 'AAC', 'audio_bitrate': '24'}, {'itag': 17, 'container': '3GP', 'video_resolution': '144p', 'video_encoding': 'MPEG-4 Visual', 'video_profile': 'Simple', 'video_bitrate': '0.05', 'audio_encoding': 'AAC', 'audio_bitrate': '24'},
] ]
# Signature decryption algorithm, reused code from youtube-dl def decipher(js, s):
def decrypt_signature(s): def tr_js(code):
if len(s) == 92: code = re.sub(r'function', r'def', code)
return s[25] + s[3:25] + s[0] + s[26:42] + s[79] + s[43:79] + s[91] + s[80:83] code = re.sub(r'\{', r':\n\t', code)
elif len(s) == 90: code = re.sub(r'\}', r'\n', code)
return s[25] + s[3:25] + s[2] + s[26:40] + s[77] + s[41:77] + s[89] + s[78:81] code = re.sub(r'var\s+', r'', code)
elif len(s) == 89: code = re.sub(r'(\w+).join\(""\)', r'"".join(\1)', code)
return s[84:78:-1] + s[87] + s[77:60:-1] + s[0] + s[59:3:-1] code = re.sub(r'(\w+).length', r'len(\1)', code)
elif len(s) == 88: code = re.sub(r'(\w+).reverse\(\)', r'\1[::-1]', code)
return s[48] + s[81:67:-1] + s[82] + s[66:62:-1] + s[85] + s[61:48:-1] + s[67] + s[47:12:-1] + s[3] + s[11:3:-1] + s[2] + s[12] code = re.sub(r'(\w+).slice\((\d+)\)', r'\1[\2:]', code)
elif len(s) == 87: code = re.sub(r'(\w+).split\(""\)', r'list(\1)', code)
return s[83:53:-1] + s[3] + s[52:40:-1] + s[86] + s[39:10:-1] + s[0] + s[9:3:-1] + s[53] return code
elif len(s) == 86:
return s[5:20] + s[2] + s[21:] f1 = match1(js, r'g.sig\|\|(\w+)\(g.s\)')
elif len(s) == 85: f1def = match1(js, r'(function %s\(\w+\)\{[^\{]+\})' % f1)
return s[2:8] + s[0] + s[9:21] + s[65] + s[22:65] + s[84] + s[66:82] + s[21] code = tr_js(f1def)
elif len(s) == 84: f2 = match1(f1def, r'(\w+)\(\w+,\d+\)')
return s[83:27:-1] + s[0] + s[26:5:-1] + s[2:0:-1] + s[27] if f2 is not None:
elif len(s) == 83: f2def = match1(js, r'(function %s\(\w+,\w+\)\{[^\{]+\})' % f2)
return s[81:64:-1] + s[82] + s[63:52:-1] + s[45] + s[51:45:-1] + s[1] + s[44:1:-1] + s[0] code = code + 'global %s\n' % f2 + tr_js(f2def)
elif len(s) == 82:
return s[36] + s[79:67:-1] + s[81] + s[66:40:-1] + s[33] + s[39:36:-1] + s[40] + s[35] + s[0] + s[67] + s[32:0:-1] + s[34] code = code + 'sig=%s(s)' % f1
elif len(s) == 81: exec(code, globals(), locals())
return s[56] + s[79:56:-1] + s[41] + s[55:41:-1] + s[80] + s[40:34:-1] + s[0] + s[33:29:-1] + s[34] + s[28:9:-1] + s[29] + s[8:0:-1] + s[9] return locals()['sig']
elif len(s) == 79:
return s[54] + s[77:54:-1] + s[39] + s[53:39:-1] + s[78] + s[38:34:-1] + s[0] + s[33:29:-1] + s[34] + s[28:9:-1] + s[29] + s[8:0:-1] + s[9]
else:
raise Exception('Unable to decrypt signature, key length %d not supported; retrying might work' % (len(s)))
def youtube_download_by_id(id, title=None, output_dir='.', merge=True, info_only=False): def youtube_download_by_id(id, title=None, output_dir='.', merge=True, info_only=False):
"""Downloads a YouTube video by its unique id. """Downloads a YouTube video by its unique id.
@ -79,6 +75,8 @@ def youtube_download_by_id(id, title=None, output_dir='.', merge=True, info_only
title = ytplayer_config['args']['title'] title = ytplayer_config['args']['title']
stream_list = ytplayer_config['args']['url_encoded_fmt_stream_map'].split(',') stream_list = ytplayer_config['args']['url_encoded_fmt_stream_map'].split(',')
html5player = ytplayer_config['assets']['js']
streams = { streams = {
parse.parse_qs(stream)['itag'][0] : parse.parse_qs(stream) parse.parse_qs(stream)['itag'][0] : parse.parse_qs(stream)
@ -95,7 +93,8 @@ def youtube_download_by_id(id, title=None, output_dir='.', merge=True, info_only
if 'sig' in download_stream: if 'sig' in download_stream:
sig = download_stream['sig'][0] sig = download_stream['sig'][0]
else: else:
sig = decrypt_signature(download_stream['s'][0]) js = get_content(html5player)
sig = decipher(js, download_stream['s'][0])
url = '%s&signature=%s' % (url, sig) url = '%s&signature=%s' % (url, sig)
type, ext, size = url_info(url) type, ext, size = url_info(url)