3 from __future__
import unicode_literals
5 from .common
import InfoExtractor
7 from ..compat
import compat_urllib_parse
9 from ..utils
import ExtractorError
20 class IqiyiIE(InfoExtractor
):
23 _VALID_URL
= r
'http://(?:www\.)iqiyi.com/.+?\.html'
26 'url': 'http://www.iqiyi.com/v_19rrojlavg.html',
27 'md5': '2cb594dc2781e6c941a110d8f358118b',
29 'id': '9c1fb1b99d192b21c559e5a1a2cb3c73',
30 'title': '美国德州空中惊现奇异云团 酷似UFO',
35 def construct_video_urls(self
, data
, video_id
, _uuid
):
44 def get_encode_code(l
):
49 for i
in range(c
- 1, -1, -1):
50 a
= do_xor(int(b
[c
- i
- 1], 16), i
)
55 mg
= ')(*&^flash@#$%a'
56 tm
= self
._download
_json
(
57 'http://data.video.qiyi.com/t?tn=' + str(random
.random()), video_id
)['t']
58 t
= str(int(math
.floor(int(tm
) / (600.0))))
59 return hashlib
.md5((t
+ mg
+ x
).encode('utf8')).hexdigest()
62 for i
in data
['vp']['tkl'][0]['vs']:
63 if 0 < int(i
['bid']) <= 10:
64 format_id
= self
.get_format(i
['bid'])
70 video_urls_info
= i
['fs']
71 if not i
['fs'][0]['l'].startswith('/'):
72 t
= get_encode_code(i
['fs'][0]['l'])
74 video_urls_info
= i
['flvs']
76 for ii
in video_urls_info
:
78 if not vl
.startswith('/'):
79 vl
= get_encode_code(vl
)
81 vl
.split('/')[-1].split('.')[0])
83 base_url
= data
['vp']['du'].split('/')
84 base_url
.insert(-1, key
)
85 base_url
= '/'.join(base_url
)
88 'qyid': uuid
.uuid4().hex,
93 'tn': str(int(time
.time()))
95 api_video_url
= base_url
+ vl
+ '?' + \
96 compat_urllib_parse
.urlencode(param
)
97 js
= self
._download
_json
(api_video_url
, video_id
)
100 (video_url
, filesize
))
102 video_urls_dict
[format_id
] = video_urls
103 return video_urls_dict
105 def get_format(self
, bid
):
114 return _dict
.get(str(bid
), None)
116 def get_bid(self
, format_id
):
126 return _dict
.get(format_id
, None)
128 def get_raw_data(self
, tvid
, video_id
, enc_key
, _uuid
):
129 tm
= str(int(time
.time()))
132 'src': hashlib
.md5(b
'youtube-dl').hexdigest(),
138 (enc_key
+ tm
+ tvid
).encode('utf8')).hexdigest(),
140 'tn': random
.random(),
142 'authkey': hashlib
.md5(
143 (tm
+ tvid
).encode('utf8')).hexdigest()
146 api_url
= 'http://cache.video.qiyi.com/vms' + '?' + \
147 compat_urllib_parse
.urlencode(param
)
148 raw_data
= self
._download
_json
(api_url
, video_id
)
151 def get_enc_key(self
, swf_url
, video_id
):
152 req
= self
._request
_webpage
(
153 swf_url
, video_id
, note
='download swf content')
155 cn
= zlib
.decompress(cn
[8:])
156 pt
= re
.compile(b
'MixerRemote\x08(?P<enc_key>.+?)\$&vv')
157 enc_key
= self
._search
_regex
(pt
, cn
, 'enc_key').decode('utf8')
160 def _real_extract(self
, url
):
161 webpage
= self
._download
_webpage
(
162 url
, 'temp_id', note
='download video page')
163 tvid
= self
._search
_regex
(
164 r
'tvId ?= ?(\'|
\")(?P
<tvid
>\d
+)', webpage, 'tvid
', flags=re.I, group='tvid
')
165 video_id = self._search_regex(
166 r'videoId ?
= ?
(\'|
\")(?P
<video_id
>[a
-z\d
]+)',
167 webpage, 'video_id
', flags=re.I, group='video_id
')
168 swf_url = self._search_regex(
169 r'(?P
<swf
>http
://.+?MainPlayer
.+?\
.swf
)', webpage, 'swf
')
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)
175 assert raw_data['code
'] == 'A000000
'
176 if not raw_data['data
']['vp
']['tkl
']:
177 raise ExtractorError('No support iQiqy VIP video
')
179 data = raw_data['data
']
181 title = data['vi
']['vn
']
183 # generate video_urls_dict
184 video_urls_dict = self.construct_video_urls(
185 data, video_id, _uuid)
189 for format_id in video_urls_dict:
190 video_urls = video_urls_dict[format_id]
191 for i, video_url_info in enumerate(video_urls):
192 if len(entries) < i + 1:
193 entries.append({'formats': []})
194 entries[i]['formats
'].append(
196 'url
': video_url_info[0],
197 'filesize
': video_url_info[-1],
198 'format_id
': format_id,
199 'preference
': int(self.get_bid(format_id))
203 for i in range(len(entries)):
204 self._sort_formats(entries[i]['formats
'])
207 'id': '_part
%d' % (i + 1),
214 '_type
': 'multi_video
',
221 info['id'] = video_id
222 info['title
'] = title