2 from __future__ import unicode_literals
10 from .common import InfoExtractor
11 from ..compat import compat_urllib_parse
12 from ..utils import ExtractorError
15 class IqiyiIE(InfoExtractor):
19 _VALID_URL = r'http://(?:www\.)iqiyi.com/v_.+?\.html'
22 'url': 'http://www.iqiyi.com/v_19rrojlavg.html',
23 'md5': '2cb594dc2781e6c941a110d8f358118b',
25 'id': '9c1fb1b99d192b21c559e5a1a2cb3c73',
26 'title': '美国德州空中惊现奇异云团 酷似UFO',
30 'url': 'http://www.iqiyi.com/v_19rrhnnclk.html',
32 'id': 'e3f585b550a280af23c98b6cb2be19fb',
33 'title': '名侦探柯南第752集',
36 'md5': '7e49376fecaffa115d951634917fe105',
38 'id': 'e3f585b550a280af23c98b6cb2be19fb_part1',
40 'title': '名侦探柯南第752集',
43 'md5': '41b75ba13bb7ac0e411131f92bc4f6ca',
45 'id': 'e3f585b550a280af23c98b6cb2be19fb_part2',
47 'title': '名侦探柯南第752集',
50 'md5': '0cee1dd0a3d46a83e71e2badeae2aab0',
52 'id': 'e3f585b550a280af23c98b6cb2be19fb_part3',
54 'title': '名侦探柯南第752集',
57 'md5': '4f8ad72373b0c491b582e7c196b0b1f9',
59 'id': 'e3f585b550a280af23c98b6cb2be19fb_part4',
61 'title': '名侦探柯南第752集',
64 'md5': 'd89ad028bcfad282918e8098e811711d',
66 'id': 'e3f585b550a280af23c98b6cb2be19fb_part5',
68 'title': '名侦探柯南第752集',
71 'md5': '9cb1e5c95da25dff0660c32ae50903b7',
73 'id': 'e3f585b550a280af23c98b6cb2be19fb_part6',
75 'title': '名侦探柯南第752集',
78 'md5': '155116e0ff1867bbc9b98df294faabc9',
80 'id': 'e3f585b550a280af23c98b6cb2be19fb_part7',
82 'title': '名侦探柯南第752集',
85 'md5': '53f5db77622ae14fa493ed2a278a082b',
87 'id': 'e3f585b550a280af23c98b6cb2be19fb_part8',
89 'title': '名侦探柯南第752集',
103 def construct_video_urls(self, data, video_id, _uuid):
112 def get_encode_code(l):
117 for i in range(c - 1, -1, -1):
118 a = do_xor(int(b[c - i - 1], 16), i)
122 def get_path_key(x, format_id, segment_index):
123 mg = ')(*&^flash@#$%a'
124 tm = self._download_json(
125 'http://data.video.qiyi.com/t?tn=' + str(random.random()), video_id,
126 note='Download path key of segment %d for format %s' % (segment_index + 1, format_id)
128 t = str(int(math.floor(int(tm) / (600.0))))
129 return hashlib.md5((t + mg + x).encode('utf8')).hexdigest()
132 for format_item in data['vp']['tkl'][0]['vs']:
133 if 0 < int(format_item['bid']) <= 10:
134 format_id = self.get_format(format_item['bid'])
140 video_urls_info = format_item['fs']
141 if not format_item['fs'][0]['l'].startswith('/'):
142 t = get_encode_code(format_item['fs'][0]['l'])
143 if t.endswith('mp4'):
144 video_urls_info = format_item['flvs']
146 for segment_index, segment in enumerate(video_urls_info):
148 if not vl.startswith('/'):
149 vl = get_encode_code(vl)
151 vl.split('/')[-1].split('.')[0], format_id, segment_index)
152 filesize = segment['b']
153 base_url = data['vp']['du'].split('/')
154 base_url.insert(-1, key)
155 base_url = '/'.join(base_url)
158 'qyid': uuid.uuid4().hex,
163 'tn': str(int(time.time()))
165 api_video_url = base_url + vl + '?' + \
166 compat_urllib_parse.urlencode(param)
167 js = self._download_json(
168 api_video_url, video_id,
169 note='Download video info of segment %d for format %s' % (segment_index + 1, format_id))
172 (video_url, filesize))
174 video_urls_dict[format_id] = video_urls
175 return video_urls_dict
177 def get_format(self, bid):
178 matched_format_ids = [_format_id for _bid, _format_id in self._FORMATS_MAP if _bid == str(bid)]
179 return matched_format_ids[0] if len(matched_format_ids) else None
181 def get_bid(self, format_id):
182 matched_bids = [_bid for _bid, _format_id in self._FORMATS_MAP if _format_id == format_id]
183 return matched_bids[0] if len(matched_bids) else None
185 def get_raw_data(self, tvid, video_id, enc_key, _uuid):
186 tm = str(int(time.time()))
189 'src': hashlib.md5(b'youtube-dl').hexdigest(),
195 (enc_key + tm + tvid).encode('utf8')).hexdigest(),
197 'tn': random.random(),
199 'authkey': hashlib.md5(
200 (tm + tvid).encode('utf8')).hexdigest()
203 api_url = 'http://cache.video.qiyi.com/vms' + '?' + \
204 compat_urllib_parse.urlencode(param)
205 raw_data = self._download_json(api_url, video_id)
208 def get_enc_key(self, swf_url, video_id):
209 enc_key = '8e29ab5666d041c3a1ea76e06dabdffb'
212 def _real_extract(self, url):
213 webpage = self._download_webpage(
214 url, 'temp_id', note='download video page')
215 tvid = self._search_regex(
216 r'data-player-tvid\s*=\s*[\'"](\d+)', webpage, 'tvid')
217 video_id = self._search_regex(
218 r'data-player-videoid\s*=\s*[\'"]([a-f\d]+)', webpage, 'video_id')
219 swf_url = self._search_regex(
220 r'(http://[^\'"]+MainPlayer[^.]+\.swf)', webpage, 'swf player URL')
221 _uuid = uuid.uuid4().hex
223 enc_key = self.get_enc_key(swf_url, video_id)
225 raw_data = self.get_raw_data(tvid, video_id, enc_key, _uuid)
227 if raw_data['code'] != 'A000000':
228 raise ExtractorError('Unable to load data. Error code: ' + raw_data['code'])
230 if not raw_data['data']['vp']['tkl']:
231 raise ExtractorError('No support iQiqy VIP video')
233 data = raw_data['data']
235 title = data['vi']['vn']
237 # generate video_urls_dict
238 video_urls_dict = self.construct_video_urls(
239 data, video_id, _uuid)
243 for format_id in video_urls_dict:
244 video_urls = video_urls_dict[format_id]
245 for i, video_url_info in enumerate(video_urls):
246 if len(entries) < i + 1:
247 entries.append({'formats': []})
248 entries[i]['formats'].append(
250 'url': video_url_info[0],
251 'filesize': video_url_info[-1],
252 'format_id': format_id,
253 'preference': int(self.get_bid(format_id))
257 for i in range(len(entries)):
258 self._sort_formats(entries[i]['formats'])
261 'id': '%s_part%d' % (video_id, i + 1),
268 '_type': 'multi_video',
275 info['id'] = video_id
276 info['title'] = title