3 from __future__ import unicode_literals
5 from .common import InfoExtractor
11 compat_urllib_request,
16 from ..utils import ExtractorError
27 class IqiyiIE(InfoExtractor):
30 _VALID_URL = r'http://(?:www\.)iqiyi.com/.+?\.html'
33 'url': 'http://www.iqiyi.com/v_19rrojlavg.html',
34 'md5': '260f0f59686e65e886995d0ba791ab83',
36 'id': '9c1fb1b99d192b21c559e5a1a2cb3c73',
37 'title': '美国德州空中惊现奇异云团 酷似UFO',
42 def construct_video_urls(self, data, video_id, _uuid):
51 def get_encode_code(l):
56 for i in range(c - 1, -1, -1):
57 a = do_xor(int(b[c-i-1], 16), i)
62 mg = ')(*&^flash@#$%a'
63 tm = self._download_json(
64 'http://data.video.qiyi.com/t?tn=' + str(random.random()), video_id)['t']
65 t = str(int(math.floor(int(tm)/(600.0))))
67 (t+mg+x).encode('utf8')).hexdigest()
70 for i in data['vp']['tkl'][0]['vs']:
71 if 0 < int(i['bid']) <= 10:
72 format_id = self.get_format(i['bid'])
74 video_urls_info = i['fs']
75 if not i['fs'][0]['l'].startswith('/'):
76 t = get_encode_code(i['fs'][0]['l'])
78 video_urls_info = i['flvs']
81 for ii in video_urls_info:
83 if not vl.startswith('/'):
84 vl = get_encode_code(vl)
86 vl.split('/')[-1].split('.')[0])
88 base_url = data['vp']['du'].split('/')
89 base_url.insert(-1, key)
90 base_url = '/'.join(base_url)
93 'qyid': uuid.uuid4().hex,
98 'tn': str(int(time.time()))
100 api_video_url = base_url + vl + '?' + \
101 compat_urllib_parse.urlencode(param)
102 js = self._download_json(api_video_url, video_id)
105 (video_url, filesize))
107 video_urls_dict[format_id] = video_urls
108 return video_urls_dict
110 def get_format(self, bid):
119 return bid_dict[str(bid)]
121 def get_raw_data(self, tvid, video_id, enc_key, _uuid):
122 tm = str(int(time.time()))
125 'src': hashlib.md5(b'youtube-dl').hexdigest(),
131 (enc_key + tm + tvid).encode('utf8')).hexdigest(),
133 'tn': random.random(),
135 'authkey': hashlib.md5(
136 (tm + tvid).encode('utf8')).hexdigest()
139 api_url = 'http://cache.video.qiyi.com/vms' + '?' + \
140 compat_urllib_parse.urlencode(param)
141 raw_data = self._download_json(api_url, video_id)
144 def get_enc_key(self, swf_url, video_id):
145 req = self._request_webpage(
146 swf_url, video_id, note='download swf content')
148 cn = zlib.decompress(cn[8:])
149 pt = re.compile(b'MixerRemote\x08(?P<enc_key>.+?)\$&vv')
150 enc_key = self._search_regex(pt, cn, 'enc_key').decode('utf8')
153 def _real_extract(self, url):
154 webpage = self._download_webpage(
155 url, 'temp_id', note='download video page')
156 tvid = self._search_regex(
157 r'tvId ?= ?(\'|\")(?P<tvid>\d+)', webpage, 'tvid', flags=re.I, group='tvid')
158 video_id = self._search_regex(
159 r'videoId ?= ?(\'|\")(?P<video_id>[a-z\d]+)',
160 webpage, 'video_id', flags=re.I, group='video_id')
161 swf_url = self._search_regex(
162 r'(?P<swf>http://.+?MainPlayer.+?\.swf)', webpage, 'swf')
163 _uuid = uuid.uuid4().hex
165 enc_key = self.get_enc_key(swf_url, video_id)
167 raw_data = self.get_raw_data(tvid, video_id, enc_key, _uuid)
168 assert raw_data['code'] == 'A000000'
169 if not raw_data['data']['vp']['tkl']:
170 raise ExtractorError('No support iQiqy VIP video')
172 data = raw_data['data']
174 title = data['vi']['vn']
176 # generate video_urls_dict
177 video_urls_dict = self.construct_video_urls(data, video_id, _uuid)
181 for format_id in video_urls_dict:
182 video_urls = video_urls_dict[format_id]
183 for i, video_url_info in enumerate(video_urls):
184 if len(entries) < i+1:
185 entries.append({'formats': []})
186 entries[i]['formats'].append(
188 'url': video_url_info[0],
189 'filesize': video_url_info[-1],
190 'format_id': format_id,
194 for i in range(len(entries)):
197 'id': '_part%d' % (i+1),
204 '_type': 'multi_video',
211 info['id'] = video_id
212 info['title'] = title