3 from __future__ import unicode_literals
11 from .common import InfoExtractor
17 class YoukuIE(InfoExtractor):
20 http://(?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)|
22 (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|)
25 'url': 'http://v.youku.com/v_show/id_XNDgyMDQ2NTQw.html',
26 'md5': 'ffe3f2e435663dc2d1eea34faeff5b5b',
31 'id': 'XNDgyMDQ2NTQw_part00',
33 'title': 'youtube-dl test video "\'/\\ä↭𝕐'
38 nowTime = int(time.time() * 1000)
39 random1 = random.randint(1000,1998)
40 random2 = random.randint(1000,9999)
42 return "%d%d%d" %(nowTime,random1,random2)
44 def _get_file_ID_mix_string(self, seed):
46 source = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\:._-1234567890")
48 for i in range(len(source)):
49 seed = (seed * 211 + 30031) % 65536
50 index = math.floor(seed / 65536 * len(source))
51 mixed.append(source[int(index)])
52 source.remove(source[int(index)])
53 #return ''.join(mixed)
56 def _get_file_id(self, fileId, seed):
57 mixed = self._get_file_ID_mix_string(seed)
58 ids = fileId.split('*')
62 realId.append(mixed[int(ch)])
63 return ''.join(realId)
65 def _real_extract(self, url):
66 mobj = re.match(self._VALID_URL, url)
67 video_id = mobj.group('id')
69 info_url = 'http://v.youku.com/player/getPlayList/VideoIDS/' + video_id
71 config = self._download_json(info_url, video_id)
73 error_code = config['data'][0].get('error_code')
75 # -8 means blocked outside China.
76 error = config['data'][0].get('error') # Chinese and English, separated by newline.
77 raise ExtractorError(error or 'Server reported error %i' % error_code,
80 video_title = config['data'][0]['title']
81 seed = config['data'][0]['seed']
83 format = self._downloader.params.get('format', None)
84 supported_format = list(config['data'][0]['streamfileids'].keys())
86 # TODO proper format selection
87 if format is None or format == 'best':
88 if 'hd2' in supported_format:
93 elif format == 'worst':
100 fileid = config['data'][0]['streamfileids'][format]
101 keys = [s['k'] for s in config['data'][0]['segs'][format]]
102 # segs is usually a dictionary, but an empty *list* if an error occured.
105 sid = self._gen_sid()
106 fileid = self._get_file_id(fileid, seed)
108 #column 8,9 of fileid represent the segment number
109 #fileid[7:9] should be changed
110 for index, key in enumerate(keys):
111 temp_fileid = '%s%02X%s' % (fileid[0:8], index, fileid[10:])
112 download_url = 'http://k.youku.com/player/getFlvPath/sid/%s_%02X/st/flv/fileid/%s?k=%s' % (sid, index, temp_fileid, key)
115 'id': '%s_part%02d' % (video_id, index),
119 'title': video_title,
122 files_info.append(info)