2 from __future__
import unicode_literals
9 from .common
import InfoExtractor
10 from ..compat
import (
12 compat_urllib_parse_urlencode
,
18 get_element_by_attribute
,
26 return hashlib
.md5(text
.encode('utf-8')).hexdigest()
29 class IqiyiSDK(object):
30 def __init__(self
, target
, ip
, timestamp
):
33 self
.timestamp
= timestamp
37 return compat_str(sum(map(lambda p
: int(p
, 16), list(data
))))
41 if isinstance(num
, int):
43 return compat_str(sum(map(int, num
)))
46 even
= self
.digit_sum(compat_str(self
.timestamp
)[::2])
47 odd
= self
.digit_sum(compat_str(self
.timestamp
)[1::2])
50 def preprocess(self
, chunksize
):
51 self
.target
= md5_text(self
.target
)
53 for i
in range(32 // chunksize
):
54 chunks
.append(self
.target
[chunksize
* i
:chunksize
* (i
+ 1)])
56 chunks
.append(self
.target
[32 - 32 % chunksize
:])
57 return chunks
, list(map(int, self
.ip
.split('.')))
59 def mod(self
, modulus
):
60 chunks
, ip
= self
.preprocess(32)
61 self
.target
= chunks
[0] + ''.join(map(lambda p
: compat_str(p
% modulus
), ip
))
63 def split(self
, chunksize
):
70 chunks
, ip
= self
.preprocess(chunksize
)
72 for i
in range(len(chunks
)):
73 ip_part
= compat_str(ip
[i
] % modulus_map
[chunksize
]) if i
< 4 else ''
75 ret
+= ip_part
+ chunks
[i
]
77 ret
+= chunks
[i
] + ip_part
80 def handle_input16(self
):
81 self
.target
= md5_text(self
.target
)
82 self
.target
= self
.split_sum(self
.target
[:16]) + self
.target
+ self
.split_sum(self
.target
[16:])
84 def handle_input8(self
):
85 self
.target
= md5_text(self
.target
)
88 part
= self
.target
[8 * i
:8 * (i
+ 1)]
89 ret
+= self
.split_sum(part
) + part
93 self
.target
= md5_text(self
.target
)
94 self
.target
= self
.split_sum(self
.target
) + self
.target
96 def date(self
, scheme
):
97 self
.target
= md5_text(self
.target
)
98 d
= time
.localtime(self
.timestamp
)
100 'y': compat_str(d
.tm_year
),
101 'm': '%02d' % d
.tm_mon
,
102 'd': '%02d' % d
.tm_mday
,
104 self
.target
+= ''.join(map(lambda c
: strings
[c
], list(scheme
)))
106 def split_time_even_odd(self
):
107 even
, odd
= self
.even_odd()
108 self
.target
= odd
+ md5_text(self
.target
) + even
110 def split_time_odd_even(self
):
111 even
, odd
= self
.even_odd()
112 self
.target
= even
+ md5_text(self
.target
) + odd
114 def split_ip_time_sum(self
):
115 chunks
, ip
= self
.preprocess(32)
116 self
.target
= compat_str(sum(ip
)) + chunks
[0] + self
.digit_sum(self
.timestamp
)
118 def split_time_ip_sum(self
):
119 chunks
, ip
= self
.preprocess(32)
120 self
.target
= self
.digit_sum(self
.timestamp
) + chunks
[0] + compat_str(sum(ip
))
123 class IqiyiSDKInterpreter(object):
124 def __init__(self
, sdk_code
):
125 self
.sdk_code
= sdk_code
127 def run(self
, target
, ip
, timestamp
):
128 self
.sdk_code
= decode_packed_codes(self
.sdk_code
)
130 functions
= re
.findall(r
'input=([a-zA-Z0-9]+)\(input', self
.sdk_code
)
132 sdk
= IqiyiSDK(target
, ip
, timestamp
)
135 'handleSum': sdk
.handleSum
,
136 'handleInput8': sdk
.handle_input8
,
137 'handleInput16': sdk
.handle_input16
,
138 'splitTimeEvenOdd': sdk
.split_time_even_odd
,
139 'splitTimeOddEven': sdk
.split_time_odd_even
,
140 'splitIpTimeSum': sdk
.split_ip_time_sum
,
141 'splitTimeIpSum': sdk
.split_time_ip_sum
,
143 for function
in functions
:
144 if re
.match(r
'mod\d+', function
):
145 sdk
.mod(int(function
[3:]))
146 elif re
.match(r
'date[ymd]{3}', function
):
147 sdk
.date(function
[4:])
148 elif re
.match(r
'split\d+', function
):
149 sdk
.split(int(function
[5:]))
150 elif function
in other_functions
:
151 other_functions
[function
]()
153 raise ExtractorError('Unknown funcion %s' % function
)
158 class IqiyiIE(InfoExtractor
):
162 _VALID_URL
= r
'https?://(?:(?:[^.]+\.)?iqiyi\.com|www\.pps\.tv)/.+\.html'
164 _NETRC_MACHINE
= 'iqiyi'
167 'url': 'http://www.iqiyi.com/v_19rrojlavg.html',
168 # MD5 checksum differs on my machine and Travis CI
170 'id': '9c1fb1b99d192b21c559e5a1a2cb3c73',
172 'title': '美国德州空中惊现奇异云团 酷似UFO',
175 'url': 'http://www.iqiyi.com/v_19rrhnnclk.html',
176 'md5': '667171934041350c5de3f5015f7f1152',
178 'id': 'e3f585b550a280af23c98b6cb2be19fb',
180 'title': '名侦探柯南 国语版:第752集 迫近灰原秘密的黑影 下篇',
182 'skip': 'Geo-restricted to China',
184 'url': 'http://www.iqiyi.com/w_19rt6o8t9p.html',
185 'only_matching': True,
187 'url': 'http://www.iqiyi.com/a_19rrhbc6kt.html',
188 'only_matching': True,
190 'url': 'http://yule.iqiyi.com/pcb.html',
191 'only_matching': True,
193 # VIP-only video. The first 2 parts (6 minutes) are available without login
194 # MD5 sums omitted as values are different on Travis CI and my machine
195 'url': 'http://www.iqiyi.com/v_19rrny4w8w.html',
197 'id': 'f3cf468b39dddb30d676f89a91200dc1',
201 'skip': 'Geo-restricted to China',
203 'url': 'http://www.iqiyi.com/a_19rrhb8ce1.html',
208 'playlist_count': 101,
210 'url': 'http://www.pps.tv/w_19rrbav0ph.html',
211 'only_matching': True,
215 '96': 1, # 216p, 240p
221 '5': 6, # 1072p, 1080p
225 def _real_initialize(self
):
230 # public key extracted from http://static.iqiyi.com/js/qiyiV2/20160129180840/jobs/i18n/i18nIndex.js
231 N
= 0xab86b6371b5318aaa1d3c9e612a9f1264f372323c8c0f19875b5fc3b3fd3afcc1e5bec527aa94bfa85bffc157e4245aebda05389a5357b75115ac94f074aefcd
234 return ohdave_rsa_encrypt(data
, e
, N
)
237 (username
, password
) = self
._get
_login
_info
()
239 # No authentication to be performed
243 data
= self
._download
_json
(
244 'http://kylin.iqiyi.com/get_token', None,
245 note
='Get token for logging', errnote
='Unable to get token for logging')
247 timestamp
= int(time
.time())
248 target
= '/apis/reglogin/login.action?lang=zh_TW&area_code=null&email=%s&passwd=%s&agenttype=1&from=undefined&keeplogin=0&piccode=&fromurl=&_pos=1' % (
249 username
, self
._rsa
_fun
(password
.encode('utf-8')))
251 interp
= IqiyiSDKInterpreter(sdk
)
252 sign
= interp
.run(target
, data
['ip'], timestamp
)
254 validation_params
= {
256 'server': 'BEA3AA1908656AABCCFF76582C4C6660',
257 'token': data
['token'],
258 'bird_src': 'f8d91d57af224da7893dd397d52d811a',
262 validation_result
= self
._download
_json
(
263 'http://kylin.iqiyi.com/validate?' + compat_urllib_parse_urlencode(validation_params
), None,
264 note
='Validate credentials', errnote
='Unable to validate credentials')
267 'P00107': 'please login via the web interface and enter the CAPTCHA code',
268 'P00117': 'bad username or password',
271 code
= validation_result
['code']
273 msg
= MSG_MAP
.get(code
)
275 msg
= 'error %s' % code
276 if validation_result
.get('msg'):
277 msg
+= ': ' + validation_result
['msg']
278 self
._downloader
.report_warning('unable to log in: ' + msg
)
283 def get_raw_data(self
, tvid
, video_id
):
284 tm
= int(time
.time() * 1000)
286 key
= 'd5fb4bd9d50c4be6948c97edd7254b0e'
287 sc
= md5_text(compat_str(tm
) + key
+ tvid
)
291 'src': '76f90cbd92f94a2e925d83e8ccd22cb7',
296 return self
._download
_json
(
297 'http://cache.m.iqiyi.com/jp/tmts/%s/%s/' % (tvid
, video_id
),
298 video_id
, transform_source
=lambda s
: remove_start(s
, 'var tvInfoJs='),
299 query
=params
, headers
=self
.geo_verification_headers())
301 def _extract_playlist(self
, webpage
):
305 r
'<a[^>]+class="site-piclist_pic_link"[^>]+href="(http://www\.iqiyi\.com/.+\.html)"',
310 album_id
= self
._search
_regex
(
311 r
'albumId\s*:\s*(\d+),', webpage
, 'album ID')
312 album_title
= self
._search
_regex
(
313 r
'data-share-title="([^"]+)"', webpage
, 'album title', fatal
=False)
315 entries
= list(map(self
.url_result
, links
))
317 # Start from 2 because links in the first page are already on webpage
318 for page_num
in itertools
.count(2):
319 pagelist_page
= self
._download
_webpage
(
320 'http://cache.video.qiyi.com/jp/avlist/%s/%d/%d/' % (album_id
, page_num
, PAGE_SIZE
),
322 note
='Download playlist page %d' % page_num
,
323 errnote
='Failed to download playlist page %d' % page_num
)
324 pagelist
= self
._parse
_json
(
325 remove_start(pagelist_page
, 'var tvInfoJs='), album_id
)
326 vlist
= pagelist
['data']['vlist']
328 entries
.append(self
.url_result(item
['vurl']))
329 if len(vlist
) < PAGE_SIZE
:
332 return self
.playlist_result(entries
, album_id
, album_title
)
334 def _real_extract(self
, url
):
335 webpage
= self
._download
_webpage
(
336 url
, 'temp_id', note
='download video page')
338 # There's no simple way to determine whether an URL is a playlist or not
340 playlist_result
= self
._extract
_playlist
(webpage
)
342 return playlist_result
344 tvid
= self
._search
_regex
(
345 r
'data-player-tvid\s*=\s*[\'"](\d+)', webpage, 'tvid')
346 video_id = self._search_regex(
347 r'data-player-videoid\s*=\s*[\'"]([a
-f\d
]+)', webpage, 'video_id
')
351 raw_data = self.get_raw_data(tvid, video_id)
353 if raw_data['code
'] != 'A00000
':
354 if raw_data['code
'] == 'A00111
':
355 self.raise_geo_restricted()
356 raise ExtractorError('Unable to load data
. Error code
: ' + raw_data['code
'])
358 data = raw_data['data
']
360 for stream in data['vidl
']:
361 if 'm3utx
' not in stream:
363 vd = compat_str(stream['vd
'])
365 'url
': stream['m3utx
'],
368 'preference
': self._FORMATS_MAP.get(vd, -1),
369 'protocol
': 'm3u8_native
',
375 self._sleep(5, video_id)
377 self._sort_formats(formats)
378 title = (get_element_by_id('widget
-videotitle
', webpage) or
379 clean_html(get_element_by_attribute('class', 'mod
-play
-tit
', webpage)))