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)
51 def get_path_key(x, format_id, segment_index):
52 mg = ')(*&^flash@#$%a'
53 tm = self._download_json(
54 'http://data.video.qiyi.com/t?tn=' + str(random.random()), video_id,
55 note='Download path key of segment %d for format %s' % (segment_index + 1, format_id)
57 t = str(int(math.floor(int(tm) / (600.0))))
58 return hashlib.md5((t + mg + x).encode('utf8')).hexdigest()
61 for format_item in data['vp']['tkl'][0]['vs']:
62 if 0 < int(format_item['bid']) <= 10:
63 format_id = self.get_format(format_item['bid'])
69 video_urls_info = format_item['fs']
70 if not format_item['fs'][0]['l'].startswith('/'):
71 t = get_encode_code(format_item['fs'][0]['l'])
73 video_urls_info = format_item['flvs']
75 for segment_index, segment in enumerate(video_urls_info):
77 if not vl.startswith('/'):
78 vl = get_encode_code(vl)
80 vl.split('/')[-1].split('.')[0], format_id, segment_index)
81 filesize = segment['b']
82 base_url = data['vp']['du'].split('/')
83 base_url.insert(-1, key)
84 base_url = '/'.join(base_url)
87 'qyid': uuid.uuid4().hex,
92 'tn': str(int(time.time()))
94 api_video_url = base_url + vl + '?' + \
95 compat_urllib_parse.urlencode(param)
96 js = self._download_json(
97 api_video_url, video_id,
98 note='Download video info of segment %d for format %s' % (segment_index + 1, format_id))
101 (video_url, filesize))
103 video_urls_dict[format_id] = video_urls
104 return video_urls_dict
106 def get_format(self, bid):
115 return _dict.get(str(bid), None)
117 def get_bid(self, format_id):
127 return _dict.get(format_id, None)
129 def get_raw_data(self, tvid, video_id, enc_key, _uuid):
130 tm = str(int(time.time()))
133 'src': hashlib.md5(b'youtube-dl').hexdigest(),
139 (enc_key + tm + tvid).encode('utf8')).hexdigest(),
141 'tn': random.random(),
143 'authkey': hashlib.md5(
144 (tm + tvid).encode('utf8')).hexdigest()
147 api_url = 'http://cache.video.qiyi.com/vms' + '?' + \
148 compat_urllib_parse.urlencode(param)
149 raw_data = self._download_json(api_url, video_id)
152 def get_enc_key(self, swf_url, video_id):
153 req = self._request_webpage(
154 swf_url, video_id, note='download swf content')
156 cn = zlib.decompress(cn[8:])
157 pt = re.compile(b'MixerRemote\x08(?P<enc_key>.+?)\$&vv')
158 enc_key = self._search_regex(pt, cn, 'enc_key').decode('utf8')
161 def _real_extract(self, url):
162 webpage = self._download_webpage(
163 url, 'temp_id', note='download video page')
164 tvid = self._search_regex(
165 r'data-player-tvid\s*=\s*[\'"](\d+)', webpage, 'tvid')
166 video_id = self._search_regex(
167 r'data-player-videoid\s*=\s*[\'"]([a-f\d]+)', webpage, 'video_id')
168 swf_url = self._search_regex(
169 r'(http://.+?MainPlayer.+?\.swf)', webpage, 'swf player URL')
170 _uuid = uuid.uuid4().hex
172 enc_key = self.get_enc_key(swf_url, video_id)
174 raw_data = self.get_raw_data(tvid, video_id, enc_key, _uuid)
176 if raw_data['code'] != 'A000000':
177 raise ExtractorError('Unable to load data. Error code: ' + raw_data['code'])
179 if not raw_data['data']['vp']['tkl']:
180 raise ExtractorError('No support iQiqy VIP video')
182 data = raw_data['data']
184 title = data['vi']['vn']
186 # generate video_urls_dict
187 video_urls_dict = self.construct_video_urls(
188 data, video_id, _uuid)
192 for format_id in video_urls_dict:
193 video_urls = video_urls_dict[format_id]
194 for i, video_url_info in enumerate(video_urls):
195 if len(entries) < i + 1:
196 entries.append({'formats': []})
197 entries[i]['formats'].append(
199 'url': video_url_info[0],
200 'filesize': video_url_info[-1],
201 'format_id': format_id,
202 'preference': int(self.get_bid(format_id))
206 for i in range(len(entries)):
207 self._sort_formats(entries[i]['formats'])
210 'id': '%s_part%d' % (video_id, i + 1),
217 '_type': 'multi_video',
224 info['id'] = video_id
225 info['title'] = title