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/v_.+?\.html'
23 'url': 'http://www.iqiyi.com/v_19rrojlavg.html',
24 'md5': '2cb594dc2781e6c941a110d8f358118b',
26 'id': '9c1fb1b99d192b21c559e5a1a2cb3c73',
27 'title': '美国德州空中惊现奇异云团 酷似UFO',
31 'url': 'http://www.iqiyi.com/v_19rrhnnclk.html',
33 'id': 'e3f585b550a280af23c98b6cb2be19fb',
34 'title': '名侦探柯南第752集',
37 'md5': '7e49376fecaffa115d951634917fe105',
39 'id': 'e3f585b550a280af23c98b6cb2be19fb_part1',
41 'title': '名侦探柯南第752集',
44 'md5': '41b75ba13bb7ac0e411131f92bc4f6ca',
46 'id': 'e3f585b550a280af23c98b6cb2be19fb_part2',
48 'title': '名侦探柯南第752集',
51 'md5': '0cee1dd0a3d46a83e71e2badeae2aab0',
53 'id': 'e3f585b550a280af23c98b6cb2be19fb_part3',
55 'title': '名侦探柯南第752集',
58 'md5': '4f8ad72373b0c491b582e7c196b0b1f9',
60 'id': 'e3f585b550a280af23c98b6cb2be19fb_part4',
62 'title': '名侦探柯南第752集',
65 'md5': 'd89ad028bcfad282918e8098e811711d',
67 'id': 'e3f585b550a280af23c98b6cb2be19fb_part5',
69 'title': '名侦探柯南第752集',
72 'md5': '9cb1e5c95da25dff0660c32ae50903b7',
74 'id': 'e3f585b550a280af23c98b6cb2be19fb_part6',
76 'title': '名侦探柯南第752集',
79 'md5': '155116e0ff1867bbc9b98df294faabc9',
81 'id': 'e3f585b550a280af23c98b6cb2be19fb_part7',
83 'title': '名侦探柯南第752集',
86 'md5': '53f5db77622ae14fa493ed2a278a082b',
88 'id': 'e3f585b550a280af23c98b6cb2be19fb_part8',
90 'title': '名侦探柯南第752集',
95 def construct_video_urls(self, data, video_id, _uuid):
104 def get_encode_code(l):
109 for i in range(c - 1, -1, -1):
110 a = do_xor(int(b[c - i - 1], 16), i)
114 def get_path_key(x, format_id, segment_index):
115 mg = ')(*&^flash@#$%a'
116 tm = self._download_json(
117 'http://data.video.qiyi.com/t?tn=' + str(random.random()), video_id,
118 note='Download path key of segment %d for format %s' % (segment_index + 1, format_id)
120 t = str(int(math.floor(int(tm) / (600.0))))
121 return hashlib.md5((t + mg + x).encode('utf8')).hexdigest()
124 for format_item in data['vp']['tkl'][0]['vs']:
125 if 0 < int(format_item['bid']) <= 10:
126 format_id = self.get_format(format_item['bid'])
132 video_urls_info = format_item['fs']
133 if not format_item['fs'][0]['l'].startswith('/'):
134 t = get_encode_code(format_item['fs'][0]['l'])
135 if t.endswith('mp4'):
136 video_urls_info = format_item['flvs']
138 for segment_index, segment in enumerate(video_urls_info):
140 if not vl.startswith('/'):
141 vl = get_encode_code(vl)
143 vl.split('/')[-1].split('.')[0], format_id, segment_index)
144 filesize = segment['b']
145 base_url = data['vp']['du'].split('/')
146 base_url.insert(-1, key)
147 base_url = '/'.join(base_url)
150 'qyid': uuid.uuid4().hex,
155 'tn': str(int(time.time()))
157 api_video_url = base_url + vl + '?' + \
158 compat_urllib_parse.urlencode(param)
159 js = self._download_json(
160 api_video_url, video_id,
161 note='Download video info of segment %d for format %s' % (segment_index + 1, format_id))
164 (video_url, filesize))
166 video_urls_dict[format_id] = video_urls
167 return video_urls_dict
169 def get_format(self, bid):
178 return _dict.get(str(bid), None)
180 def get_bid(self, format_id):
190 return _dict.get(format_id, None)
192 def get_raw_data(self, tvid, video_id, enc_key, _uuid):
193 tm = str(int(time.time()))
196 'src': hashlib.md5(b'youtube-dl').hexdigest(),
202 (enc_key + tm + tvid).encode('utf8')).hexdigest(),
204 'tn': random.random(),
206 'authkey': hashlib.md5(
207 (tm + tvid).encode('utf8')).hexdigest()
210 api_url = 'http://cache.video.qiyi.com/vms' + '?' + \
211 compat_urllib_parse.urlencode(param)
212 raw_data = self._download_json(api_url, video_id)
215 def get_enc_key(self, swf_url, video_id):
216 req = self._request_webpage(
217 swf_url, video_id, note='download swf content')
219 cn = zlib.decompress(cn[8:])
220 pt = re.compile(b'MixerRemote\x08(?P<enc_key>.+?)\$&vv')
221 enc_key = self._search_regex(pt, cn, 'enc_key').decode('utf8')
224 def _real_extract(self, url):
225 webpage = self._download_webpage(
226 url, 'temp_id', note='download video page')
227 tvid = self._search_regex(
228 r'data-player-tvid\s*=\s*[\'"](\d+)', webpage, 'tvid')
229 video_id = self._search_regex(
230 r'data-player-videoid\s*=\s*[\'"]([a-f\d]+)', webpage, 'video_id')
231 swf_url = self._search_regex(
232 r'(http://.+?MainPlayer.+?\.swf)', webpage, 'swf player URL')
233 _uuid = uuid.uuid4().hex
235 enc_key = self.get_enc_key(swf_url, video_id)
237 raw_data = self.get_raw_data(tvid, video_id, enc_key, _uuid)
239 if raw_data['code'] != 'A000000':
240 raise ExtractorError('Unable to load data. Error code: ' + raw_data['code'])
242 if not raw_data['data']['vp']['tkl']:
243 raise ExtractorError('No support iQiqy VIP video')
245 data = raw_data['data']
247 title = data['vi']['vn']
249 # generate video_urls_dict
250 video_urls_dict = self.construct_video_urls(
251 data, video_id, _uuid)
255 for format_id in video_urls_dict:
256 video_urls = video_urls_dict[format_id]
257 for i, video_url_info in enumerate(video_urls):
258 if len(entries) < i + 1:
259 entries.append({'formats': []})
260 entries[i]['formats'].append(
262 'url': video_url_info[0],
263 'filesize': video_url_info[-1],
264 'format_id': format_id,
265 'preference': int(self.get_bid(format_id))
269 for i in range(len(entries)):
270 self._sort_formats(entries[i]['formats'])
273 'id': '%s_part%d' % (video_id, i + 1),
280 '_type': 'multi_video',
287 info['id'] = video_id
288 info['title'] = title