2 from __future__ import unicode_literals
12 from .common import InfoExtractor
13 from ..compat import compat_urllib_parse
14 from ..utils import ExtractorError
17 class IqiyiIE(InfoExtractor):
20 _VALID_URL = r'http://(?:www\.)iqiyi.com/.+?\.html'
23 'url': 'http://www.iqiyi.com/v_19rrojlavg.html',
24 'md5': '2cb594dc2781e6c941a110d8f358118b',
26 'id': '9c1fb1b99d192b21c559e5a1a2cb3c73',
27 'title': '美国德州空中惊现奇异云团 酷似UFO',
32 def construct_video_urls(self, data, video_id, _uuid):
41 def get_encode_code(l):
46 for i in range(c - 1, -1, -1):
47 a = do_xor(int(b[c - i - 1], 16), i)
52 mg = ')(*&^flash@#$%a'
53 tm = self._download_json(
54 'http://data.video.qiyi.com/t?tn=' + str(random.random()), video_id)['t']
55 t = str(int(math.floor(int(tm) / (600.0))))
56 return hashlib.md5((t + mg + x).encode('utf8')).hexdigest()
59 for i in data['vp']['tkl'][0]['vs']:
60 if 0 < int(i['bid']) <= 10:
61 format_id = self.get_format(i['bid'])
67 video_urls_info = i['fs']
68 if not i['fs'][0]['l'].startswith('/'):
69 t = get_encode_code(i['fs'][0]['l'])
71 video_urls_info = i['flvs']
73 for ii in video_urls_info:
75 if not vl.startswith('/'):
76 vl = get_encode_code(vl)
78 vl.split('/')[-1].split('.')[0])
80 base_url = data['vp']['du'].split('/')
81 base_url.insert(-1, key)
82 base_url = '/'.join(base_url)
85 'qyid': uuid.uuid4().hex,
90 'tn': str(int(time.time()))
92 api_video_url = base_url + vl + '?' + \
93 compat_urllib_parse.urlencode(param)
94 js = self._download_json(api_video_url, video_id)
97 (video_url, filesize))
99 video_urls_dict[format_id] = video_urls
100 return video_urls_dict
102 def get_format(self, bid):
111 return _dict.get(str(bid), None)
113 def get_bid(self, format_id):
123 return _dict.get(format_id, None)
125 def get_raw_data(self, tvid, video_id, enc_key, _uuid):
126 tm = str(int(time.time()))
129 'src': hashlib.md5(b'youtube-dl').hexdigest(),
135 (enc_key + tm + tvid).encode('utf8')).hexdigest(),
137 'tn': random.random(),
139 'authkey': hashlib.md5(
140 (tm + tvid).encode('utf8')).hexdigest()
143 api_url = 'http://cache.video.qiyi.com/vms' + '?' + \
144 compat_urllib_parse.urlencode(param)
145 raw_data = self._download_json(api_url, video_id)
148 def get_enc_key(self, swf_url, video_id):
149 req = self._request_webpage(
150 swf_url, video_id, note='download swf content')
152 cn = zlib.decompress(cn[8:])
153 pt = re.compile(b'MixerRemote\x08(?P<enc_key>.+?)\$&vv')
154 enc_key = self._search_regex(pt, cn, 'enc_key').decode('utf8')
157 def _real_extract(self, url):
158 webpage = self._download_webpage(
159 url, 'temp_id', note='download video page')
160 tvid = self._search_regex(
161 r'data-player-tvid\s*=\s*[\'"](\d+)', webpage, 'tvid')
162 video_id = self._search_regex(
163 r'data-player-videoid\s*=\s*[\'"]([a-f\d]+)', webpage, 'video_id')
164 swf_url = self._search_regex(
165 r'(http://.+?MainPlayer.+?\.swf)', webpage, 'swf player URL')
166 _uuid = uuid.uuid4().hex
168 enc_key = self.get_enc_key(swf_url, video_id)
170 raw_data = self.get_raw_data(tvid, video_id, enc_key, _uuid)
172 if raw_data['code'] != 'A000000':
173 raise ExtractorError('Unable to load data. Error code: ' + raw_data['code'])
175 if not raw_data['data']['vp']['tkl']:
176 raise ExtractorError('No support iQiqy VIP video')
178 data = raw_data['data']
180 title = data['vi']['vn']
182 # generate video_urls_dict
183 video_urls_dict = self.construct_video_urls(
184 data, video_id, _uuid)
188 for format_id in video_urls_dict:
189 video_urls = video_urls_dict[format_id]
190 for i, video_url_info in enumerate(video_urls):
191 if len(entries) < i + 1:
192 entries.append({'formats': []})
193 entries[i]['formats'].append(
195 'url': video_url_info[0],
196 'filesize': video_url_info[-1],
197 'format_id': format_id,
198 'preference': int(self.get_bid(format_id))
202 for i in range(len(entries)):
203 self._sort_formats(entries[i]['formats'])
206 'id': '_part%d' % (i + 1),
213 '_type': 'multi_video',
220 info['id'] = video_id
221 info['title'] = title