3 from __future__ import unicode_literals
5 from .common import InfoExtractor
7 from ..compat import compat_urllib_parse
9 from ..utils import ExtractorError
20 class IqiyiIE(InfoExtractor):
23 _VALID_URL = r'http://(?:www\.)iqiyi.com/.+?\.html'
26 'url': 'http://www.iqiyi.com/v_19rrojlavg.html',
27 'md5': '2cb594dc2781e6c941a110d8f358118b',
29 'id': '9c1fb1b99d192b21c559e5a1a2cb3c73',
30 'title': '美国德州空中惊现奇异云团 酷似UFO',
35 def construct_video_urls(self, data, video_id, _uuid, bid):
44 def get_encode_code(l):
49 for i in range(c - 1, -1, -1):
50 a = do_xor(int(b[c - i - 1], 16), i)
55 mg = ')(*&^flash@#$%a'
56 tm = self._download_json(
57 'http://data.video.qiyi.com/t?tn=' + str(random.random()), video_id)['t']
58 t = str(int(math.floor(int(tm) / (600.0))))
59 return hashlib.md5((t + mg + x).encode('utf8')).hexdigest()
62 # getting all format will spend minutes for a big video.
64 bids = [int(i['bid']) for i in data['vp']['tkl'][0]['vs']
65 if 0 < int(i['bid']) <= 10]
69 for i in data['vp']['tkl'][0]['vs']:
70 if 0 < int(i['bid']) <= 10:
71 format_id = self.get_format(i['bid'])
77 video_urls_info = i['fs']
78 if not i['fs'][0]['l'].startswith('/'):
79 t = get_encode_code(i['fs'][0]['l'])
81 video_urls_info = i['flvs']
83 if int(i['bid']) != int(bid): # ignore missing match format
85 [('http://example.com/v.flv', ii['b']) for ii in video_urls_info])
86 video_urls_dict[format_id] = video_urls
89 for ii in video_urls_info:
91 if not vl.startswith('/'):
92 vl = get_encode_code(vl)
94 vl.split('/')[-1].split('.')[0])
96 base_url = data['vp']['du'].split('/')
97 base_url.insert(-1, key)
98 base_url = '/'.join(base_url)
101 'qyid': uuid.uuid4().hex,
106 'tn': str(int(time.time()))
108 api_video_url = base_url + vl + '?' + \
109 compat_urllib_parse.urlencode(param)
110 js = self._download_json(api_video_url, video_id)
113 (video_url, filesize))
115 video_urls_dict[format_id] = video_urls
116 return video_urls_dict
118 def get_format(self, bid):
127 return _dict.get(str(bid), None)
129 def get_bid(self, format_id):
139 return _dict.get(format_id, None)
141 def get_raw_data(self, tvid, video_id, enc_key, _uuid):
142 tm = str(int(time.time()))
145 'src': hashlib.md5(b'youtube-dl').hexdigest(),
151 (enc_key + tm + tvid).encode('utf8')).hexdigest(),
153 'tn': random.random(),
155 'authkey': hashlib.md5(
156 (tm + tvid).encode('utf8')).hexdigest()
159 api_url = 'http://cache.video.qiyi.com/vms' + '?' + \
160 compat_urllib_parse.urlencode(param)
161 raw_data = self._download_json(api_url, video_id)
164 def get_enc_key(self, swf_url, video_id):
165 req = self._request_webpage(
166 swf_url, video_id, note='download swf content')
168 cn = zlib.decompress(cn[8:])
169 pt = re.compile(b'MixerRemote\x08(?P<enc_key>.+?)\$&vv')
170 enc_key = self._search_regex(pt, cn, 'enc_key').decode('utf8')
173 def _real_extract(self, url):
174 webpage = self._download_webpage(
175 url, 'temp_id', note='download video page')
176 tvid = self._search_regex(
177 r'tvId ?= ?(\'|\")(?P<tvid>\d+)', webpage, 'tvid', flags=re.I, group='tvid')
178 video_id = self._search_regex(
179 r'videoId ?= ?(\'|\")(?P<video_id>[a-z\d]+)',
180 webpage, 'video_id', flags=re.I, group='video_id')
181 swf_url = self._search_regex(
182 r'(?P<swf>http://.+?MainPlayer.+?\.swf)', webpage, 'swf')
183 _uuid = uuid.uuid4().hex
185 enc_key = self.get_enc_key(swf_url, video_id)
187 raw_data = self.get_raw_data(tvid, video_id, enc_key, _uuid)
188 assert raw_data['code'] == 'A000000'
189 if not raw_data['data']['vp']['tkl']:
190 raise ExtractorError('No support iQiqy VIP video')
192 data = raw_data['data']
194 title = data['vi']['vn']
196 format = self._downloader.params.get('format', None)
197 bid = self.get_bid(format) if format else 'best'
199 raise ExtractorError('Can\'t get format.')
201 # generate video_urls_dict
202 video_urls_dict = self.construct_video_urls(
203 data, video_id, _uuid, bid)
207 for format_id in video_urls_dict:
208 video_urls = video_urls_dict[format_id]
209 for i, video_url_info in enumerate(video_urls):
210 if len(entries) < i + 1:
211 entries.append({'formats': []})
212 entries[i]['formats'].append(
214 'url': video_url_info[0],
215 'filesize': video_url_info[-1],
216 'format_id': format_id,
217 'preference': int(self.get_bid(format_id))
221 for i in range(len(entries)):
222 self._sort_formats(entries[i]['formats'])
225 'id': '_part%d' % (i + 1),
232 '_type': 'multi_video',
239 info['id'] = video_id
240 info['title'] = title