]>
Commit | Line | Data |
---|---|---|
7f09a662 YCH |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
7a8fef31 | 4 | import base64 |
265bfa2c | 5 | import datetime |
7a8fef31 | 6 | import hashlib |
57031161 | 7 | import re |
7f09a662 | 8 | import time |
7f09a662 YCH |
9 | |
10 | from .common import InfoExtractor | |
265bfa2c | 11 | from ..compat import ( |
1e399778 | 12 | compat_ord, |
7b7507d6 | 13 | compat_str, |
15707c7e | 14 | compat_urllib_parse_urlencode, |
265bfa2c PH |
15 | ) |
16 | from ..utils import ( | |
17 | determine_ext, | |
7a8fef31 | 18 | encode_data_uri, |
265bfa2c | 19 | ExtractorError, |
7a8fef31 YCH |
20 | int_or_none, |
21 | orderedSet, | |
265bfa2c | 22 | parse_iso8601, |
5f432ac8 | 23 | str_or_none, |
bec30224 | 24 | url_basename, |
30105f4a | 25 | urshift, |
07d7689f | 26 | update_url_query, |
265bfa2c | 27 | ) |
7f09a662 YCH |
28 | |
29 | ||
8c5a9944 | 30 | class LeIE(InfoExtractor): |
963d0ce7 | 31 | IE_DESC = '乐视网' |
0d72ff9c | 32 | _VALID_URL = r'https?://(?:www\.le\.com/ptv/vplay|(?:sports\.le|(?:www\.)?lesports)\.com/(?:match|video))/(?P<id>\d+)\.html' |
5e73a67d | 33 | |
f3bc19a9 | 34 | _URL_TEMPLATE = 'http://www.le.com/ptv/vplay/%s.html' |
7f09a662 YCH |
35 | |
36 | _TESTS = [{ | |
5e73a67d | 37 | 'url': 'http://www.le.com/ptv/vplay/22005890.html', |
1e399778 | 38 | 'md5': 'edadcfe5406976f42f9f266057ee5e40', |
7f09a662 YCH |
39 | 'info_dict': { |
40 | 'id': '22005890', | |
41 | 'ext': 'mp4', | |
42 | 'title': '第87届奥斯卡颁奖礼完美落幕 《鸟人》成最大赢家', | |
67706359 | 43 | 'description': 'md5:a9cb175fd753e2962176b7beca21a47c', |
1e399778 YCH |
44 | }, |
45 | 'params': { | |
46 | 'hls_prefer_native': True, | |
47 | }, | |
7f09a662 | 48 | }, { |
5e73a67d | 49 | 'url': 'http://www.le.com/ptv/vplay/1415246.html', |
7f09a662 | 50 | 'info_dict': { |
67706359 | 51 | 'id': '1415246', |
7f09a662 | 52 | 'ext': 'mp4', |
67706359 YCH |
53 | 'title': '美人天下01', |
54 | 'description': 'md5:f88573d9d7225ada1359eaf0dbf8bcda', | |
55 | }, | |
1e399778 YCH |
56 | 'params': { |
57 | 'hls_prefer_native': True, | |
58 | }, | |
91410c9b PH |
59 | }, { |
60 | 'note': 'This video is available only in Mainland China, thus a proxy is needed', | |
5e73a67d | 61 | 'url': 'http://www.le.com/ptv/vplay/1118082.html', |
1e399778 | 62 | 'md5': '2424c74948a62e5f31988438979c5ad1', |
91410c9b PH |
63 | 'info_dict': { |
64 | 'id': '1118082', | |
65 | 'ext': 'mp4', | |
66 | 'title': '与龙共舞 完整版', | |
67 | 'description': 'md5:7506a5eeb1722bb9d4068f85024e3986', | |
68 | }, | |
1e399778 YCH |
69 | 'params': { |
70 | 'hls_prefer_native': True, | |
71 | }, | |
051df9ad | 72 | 'skip': 'Only available in China', |
47787efa YCH |
73 | }, { |
74 | 'url': 'http://sports.le.com/video/25737697.html', | |
75 | 'only_matching': True, | |
0d72ff9c YCH |
76 | }, { |
77 | 'url': 'http://www.lesports.com/match/1023203003.html', | |
78 | 'only_matching': True, | |
79 | }, { | |
80 | 'url': 'http://sports.le.com/match/1023203003.html', | |
81 | 'only_matching': True, | |
7f09a662 YCH |
82 | }] |
83 | ||
265bfa2c | 84 | # ror() and calc_time_key() are reversed from a embedded swf file in KLetvPlayer.swf |
7f09a662 YCH |
85 | def ror(self, param1, param2): |
86 | _loc3_ = 0 | |
87 | while _loc3_ < param2: | |
30105f4a | 88 | param1 = urshift(param1, 1) + ((param1 & 1) << 31) |
7f09a662 YCH |
89 | _loc3_ += 1 |
90 | return param1 | |
91 | ||
265bfa2c | 92 | def calc_time_key(self, param1): |
7f09a662 YCH |
93 | _loc2_ = 773625421 |
94 | _loc3_ = self.ror(param1, _loc2_ % 13) | |
95 | _loc3_ = _loc3_ ^ _loc2_ | |
96 | _loc3_ = self.ror(_loc3_, _loc2_ % 17) | |
97 | return _loc3_ | |
98 | ||
07d7689f RA |
99 | # reversed from http://jstatic.letvcdn.com/sdk/player.js |
100 | def get_mms_key(self, time): | |
101 | return self.ror(time, 8) ^ 185025305 | |
102 | ||
1e399778 YCH |
103 | # see M3U8Encryption class in KLetvPlayer.swf |
104 | @staticmethod | |
105 | def decrypt_m3u8(encrypted_data): | |
106 | if encrypted_data[:5].decode('utf-8').lower() != 'vc_01': | |
107 | return encrypted_data | |
108 | encrypted_data = encrypted_data[5:] | |
109 | ||
2ebd2eac YCH |
110 | _loc4_ = bytearray(2 * len(encrypted_data)) |
111 | for idx, val in enumerate(encrypted_data): | |
112 | b = compat_ord(val) | |
113 | _loc4_[2 * idx] = b // 16 | |
114 | _loc4_[2 * idx + 1] = b % 16 | |
1e399778 YCH |
115 | idx = len(_loc4_) - 11 |
116 | _loc4_ = _loc4_[idx:] + _loc4_[:idx] | |
2ebd2eac YCH |
117 | _loc7_ = bytearray(len(encrypted_data)) |
118 | for i in range(len(encrypted_data)): | |
119 | _loc7_[i] = _loc4_[2 * i] * 16 + _loc4_[2 * i + 1] | |
1e399778 YCH |
120 | |
121 | return bytes(_loc7_) | |
122 | ||
07d7689f | 123 | def _check_errors(self, play_json): |
7f09a662 YCH |
124 | # Check for errors |
125 | playstatus = play_json['playstatus'] | |
126 | if playstatus['status'] == 0: | |
127 | flag = playstatus['flag'] | |
128 | if flag == 1: | |
129 | msg = 'Country %s auth error' % playstatus['country'] | |
130 | else: | |
131 | msg = 'Generic error. flag = %d' % flag | |
132 | raise ExtractorError(msg, expected=True) | |
133 | ||
07d7689f RA |
134 | def _real_extract(self, url): |
135 | media_id = self._match_id(url) | |
136 | page = self._download_webpage(url, media_id) | |
137 | ||
138 | play_json_h5 = self._download_json( | |
139 | 'http://api.le.com/mms/out/video/playJsonH5', | |
140 | media_id, 'Downloading html5 playJson data', query={ | |
141 | 'id': media_id, | |
142 | 'platid': 3, | |
143 | 'splatid': 304, | |
144 | 'format': 1, | |
145 | 'tkey': self.get_mms_key(int(time.time())), | |
146 | 'domain': 'www.le.com', | |
147 | 'tss': 'no', | |
148 | }, | |
149 | headers=self.geo_verification_headers()) | |
150 | self._check_errors(play_json_h5) | |
7f09a662 | 151 | |
07d7689f RA |
152 | play_json_flash = self._download_json( |
153 | 'http://api.le.com/mms/out/video/playJson', | |
154 | media_id, 'Downloading flash playJson data', query={ | |
155 | 'id': media_id, | |
156 | 'platid': 1, | |
157 | 'splatid': 101, | |
158 | 'format': 1, | |
159 | 'tkey': self.calc_time_key(int(time.time())), | |
160 | 'domain': 'www.le.com', | |
161 | }, | |
162 | headers=self.geo_verification_headers()) | |
163 | self._check_errors(play_json_flash) | |
7f09a662 | 164 | |
07d7689f RA |
165 | def get_h5_urls(media_url, format_id): |
166 | location = self._download_json( | |
167 | media_url, media_id, | |
168 | 'Download JSON metadata for format %s' % format_id, query={ | |
1e399778 YCH |
169 | 'format': 1, |
170 | 'expect': 3, | |
07d7689f RA |
171 | 'tss': 'no', |
172 | })['location'] | |
173 | ||
174 | return { | |
175 | 'http': update_url_query(location, {'tss': 'no'}), | |
176 | 'hls': update_url_query(location, {'tss': 'ios'}), | |
177 | } | |
178 | ||
179 | def get_flash_urls(media_url, format_id): | |
180 | media_url += '&' + compat_urllib_parse_urlencode({ | |
181 | 'm3v': 1, | |
182 | 'format': 1, | |
183 | 'expect': 3, | |
184 | 'rateid': format_id, | |
185 | }) | |
186 | ||
187 | nodes_data = self._download_json( | |
188 | media_url, media_id, | |
189 | 'Download JSON metadata for format %s' % format_id) | |
190 | ||
191 | req = self._request_webpage( | |
192 | nodes_data['nodelist'][0]['location'], media_id, | |
193 | note='Downloading m3u8 information for format %s' % format_id) | |
194 | ||
195 | m3u8_data = self.decrypt_m3u8(req.read()) | |
196 | ||
197 | return { | |
198 | 'hls': encode_data_uri(m3u8_data, 'application/vnd.apple.mpegurl'), | |
199 | } | |
200 | ||
201 | extracted_formats = [] | |
202 | formats = [] | |
203 | for play_json, get_urls in ((play_json_h5, get_h5_urls), (play_json_flash, get_flash_urls)): | |
204 | playurl = play_json['playurl'] | |
205 | play_domain = playurl['domain'][0] | |
206 | ||
207 | for format_id, format_data in playurl.get('dispatch', []).items(): | |
208 | if format_id in extracted_formats: | |
209 | continue | |
210 | extracted_formats.append(format_id) | |
211 | ||
212 | media_url = play_domain + format_data[0] | |
213 | for protocol, format_url in get_urls(media_url, format_id).items(): | |
214 | f = { | |
215 | 'url': format_url, | |
216 | 'ext': determine_ext(format_data[1]), | |
217 | 'format_id': '%s-%s' % (protocol, format_id), | |
218 | 'protocol': 'm3u8_native' if protocol == 'hls' else 'http', | |
219 | 'quality': int_or_none(format_id), | |
220 | } | |
221 | ||
222 | if format_id[-1:] == 'p': | |
223 | f['height'] = int_or_none(format_id[:-1]) | |
224 | ||
225 | formats.append(f) | |
226 | self._sort_formats(formats, ('height', 'quality', 'format_id')) | |
7f09a662 YCH |
227 | |
228 | publish_time = parse_iso8601(self._html_search_regex( | |
91410c9b | 229 | r'发布时间 ([^<>]+) ', page, 'publish time', default=None), |
7f09a662 | 230 | delimiter=' ', timezone=datetime.timedelta(hours=8)) |
67706359 | 231 | description = self._html_search_meta('description', page, fatal=False) |
7f09a662 YCH |
232 | |
233 | return { | |
234 | 'id': media_id, | |
07d7689f | 235 | 'formats': formats, |
7f09a662 YCH |
236 | 'title': playurl['title'], |
237 | 'thumbnail': playurl['pic'], | |
67706359 | 238 | 'description': description, |
7f09a662 YCH |
239 | 'timestamp': publish_time, |
240 | } | |
57031161 YCH |
241 | |
242 | ||
8c5a9944 | 243 | class LePlaylistIE(InfoExtractor): |
47787efa | 244 | _VALID_URL = r'https?://[a-z]+\.le\.com/(?!video)[a-z]+/(?P<id>[a-z0-9_]+)' |
2eb25b25 | 245 | |
57031161 | 246 | _TESTS = [{ |
5e73a67d | 247 | 'url': 'http://www.le.com/tv/46177.html', |
57031161 YCH |
248 | 'info_dict': { |
249 | 'id': '46177', | |
250 | 'title': '美人天下', | |
251 | 'description': 'md5:395666ff41b44080396e59570dbac01c' | |
252 | }, | |
253 | 'playlist_count': 35 | |
2eb25b25 | 254 | }, { |
5e73a67d | 255 | 'url': 'http://tv.le.com/izt/wuzetian/index.html', |
57031161 YCH |
256 | 'info_dict': { |
257 | 'id': 'wuzetian', | |
258 | 'title': '武媚娘传奇', | |
259 | 'description': 'md5:e12499475ab3d50219e5bba00b3cb248' | |
260 | }, | |
67706359 YCH |
261 | # This playlist contains some extra videos other than the drama itself |
262 | 'playlist_mincount': 96 | |
57031161 | 263 | }, { |
5e73a67d | 264 | 'url': 'http://tv.le.com/pzt/lswjzzjc/index.shtml', |
d58cb3ec YCH |
265 | # This series is moved to http://www.le.com/tv/10005297.html |
266 | 'only_matching': True, | |
2eb25b25 YCH |
267 | }, { |
268 | 'url': 'http://www.le.com/comic/92063.html', | |
269 | 'only_matching': True, | |
270 | }, { | |
271 | 'url': 'http://list.le.com/listn/c1009_sc532002_d2_p1_o1.html', | |
272 | 'only_matching': True, | |
57031161 | 273 | }] |
5f432ac8 | 274 | |
2eb25b25 YCH |
275 | @classmethod |
276 | def suitable(cls, url): | |
8c5a9944 | 277 | return False if LeIE.suitable(url) else super(LePlaylistIE, cls).suitable(url) |
2eb25b25 YCH |
278 | |
279 | def _real_extract(self, url): | |
280 | playlist_id = self._match_id(url) | |
281 | page = self._download_webpage(url, playlist_id) | |
282 | ||
283 | # Currently old domain names are still used in playlists | |
284 | media_ids = orderedSet(re.findall( | |
285 | r'<a[^>]+href="http://www\.letv\.com/ptv/vplay/(\d+)\.html', page)) | |
8c5a9944 | 286 | entries = [self.url_result(LeIE._URL_TEMPLATE % media_id, ie='Le') |
2eb25b25 YCH |
287 | for media_id in media_ids] |
288 | ||
289 | title = self._html_search_meta('keywords', page, | |
290 | fatal=False).split(',')[0] | |
291 | description = self._html_search_meta('description', page, fatal=False) | |
292 | ||
293 | return self.playlist_result(entries, playlist_id, playlist_title=title, | |
294 | playlist_description=description) | |
295 | ||
5f432ac8 FF |
296 | |
297 | class LetvCloudIE(InfoExtractor): | |
8c5a9944 YCH |
298 | # Most of *.letv.com is changed to *.le.com on 2016/01/02 |
299 | # but yuntv.letv.com is kept, so also keep the extractor name | |
5f432ac8 | 300 | IE_DESC = '乐视云' |
73e74424 | 301 | _VALID_URL = r'https?://yuntv\.letv\.com/bcloud.html\?.+' |
5f432ac8 FF |
302 | |
303 | _TESTS = [{ | |
304 | 'url': 'http://yuntv.letv.com/bcloud.html?uu=p7jnfw5hw9&vu=467623dedf', | |
305 | 'md5': '26450599afd64c513bc77030ad15db44', | |
306 | 'info_dict': { | |
307 | 'id': 'p7jnfw5hw9_467623dedf', | |
308 | 'ext': 'mp4', | |
0428106d | 309 | 'title': 'Video p7jnfw5hw9_467623dedf', |
5f432ac8 FF |
310 | }, |
311 | }, { | |
312 | 'url': 'http://yuntv.letv.com/bcloud.html?uu=p7jnfw5hw9&vu=ec93197892&pu=2c7cd40209&auto_play=1&gpcflag=1&width=640&height=360', | |
7b7507d6 | 313 | 'md5': 'e03d9cc8d9c13191e1caf277e42dbd31', |
5f432ac8 FF |
314 | 'info_dict': { |
315 | 'id': 'p7jnfw5hw9_ec93197892', | |
316 | 'ext': 'mp4', | |
0428106d | 317 | 'title': 'Video p7jnfw5hw9_ec93197892', |
5f432ac8 FF |
318 | }, |
319 | }, { | |
320 | 'url': 'http://yuntv.letv.com/bcloud.html?uu=p7jnfw5hw9&vu=187060b6fd', | |
7b7507d6 | 321 | 'md5': 'cb988699a776b22d4a41b9d43acfb3ac', |
5f432ac8 FF |
322 | 'info_dict': { |
323 | 'id': 'p7jnfw5hw9_187060b6fd', | |
324 | 'ext': 'mp4', | |
0428106d | 325 | 'title': 'Video p7jnfw5hw9_187060b6fd', |
5f432ac8 FF |
326 | }, |
327 | }] | |
328 | ||
7b7507d6 YCH |
329 | @staticmethod |
330 | def sign_data(obj): | |
331 | if obj['cf'] == 'flash': | |
332 | salt = '2f9d6924b33a165a6d8b5d3d42f4f987' | |
333 | items = ['cf', 'format', 'ran', 'uu', 'ver', 'vu'] | |
334 | elif obj['cf'] == 'html5': | |
335 | salt = 'fbeh5player12c43eccf2bec3300344' | |
336 | items = ['cf', 'ran', 'uu', 'bver', 'vu'] | |
337 | input_data = ''.join([item + obj[item] for item in items]) + salt | |
338 | obj['sign'] = hashlib.md5(input_data.encode('utf-8')).hexdigest() | |
339 | ||
340 | def _get_formats(self, cf, uu, vu, media_id): | |
341 | def get_play_json(cf, timestamp): | |
342 | data = { | |
343 | 'cf': cf, | |
344 | 'ver': '2.2', | |
345 | 'bver': 'firefox44.0', | |
346 | 'format': 'json', | |
347 | 'uu': uu, | |
348 | 'vu': vu, | |
349 | 'ran': compat_str(timestamp), | |
350 | } | |
351 | self.sign_data(data) | |
352 | return self._download_json( | |
15707c7e | 353 | 'http://api.letvcloud.com/gpc.php?' + compat_urllib_parse_urlencode(data), |
7b7507d6 YCH |
354 | media_id, 'Downloading playJson data for type %s' % cf) |
355 | ||
356 | play_json = get_play_json(cf, time.time()) | |
357 | # The server time may be different from local time | |
358 | if play_json.get('code') == 10071: | |
359 | play_json = get_play_json(cf, play_json['timestamp']) | |
5f432ac8 | 360 | |
26de1bba YCH |
361 | if not play_json.get('data'): |
362 | if play_json.get('message'): | |
363 | raise ExtractorError('Letv cloud said: %s' % play_json['message'], expected=True) | |
364 | elif play_json.get('code'): | |
365 | raise ExtractorError('Letv cloud returned error %d' % play_json['code'], expected=True) | |
366 | else: | |
367 | raise ExtractorError('Letv cloud returned an unknwon error') | |
368 | ||
bec30224 YCH |
369 | def b64decode(s): |
370 | return base64.b64decode(s.encode('utf-8')).decode('utf-8') | |
371 | ||
10defdd0 YCH |
372 | formats = [] |
373 | for media in play_json['data']['video_info']['media'].values(): | |
374 | play_url = media['play_url'] | |
bec30224 YCH |
375 | url = b64decode(play_url['main_url']) |
376 | decoded_url = b64decode(url_basename(url)) | |
10defdd0 | 377 | formats.append({ |
bec30224 YCH |
378 | 'url': url, |
379 | 'ext': determine_ext(decoded_url), | |
8a370aed | 380 | 'format_id': str_or_none(play_url.get('vtype')), |
10defdd0 YCH |
381 | 'format_note': str_or_none(play_url.get('definition')), |
382 | 'width': int_or_none(play_url.get('vwidth')), | |
383 | 'height': int_or_none(play_url.get('vheight')), | |
384 | }) | |
7b7507d6 YCH |
385 | |
386 | return formats | |
387 | ||
388 | def _real_extract(self, url): | |
ec85ded8 YCH |
389 | uu_mobj = re.search(r'uu=([\w]+)', url) |
390 | vu_mobj = re.search(r'vu=([\w]+)', url) | |
7b7507d6 YCH |
391 | |
392 | if not uu_mobj or not vu_mobj: | |
393 | raise ExtractorError('Invalid URL: %s' % url, expected=True) | |
394 | ||
395 | uu = uu_mobj.group(1) | |
396 | vu = vu_mobj.group(1) | |
397 | media_id = uu + '_' + vu | |
398 | ||
399 | formats = self._get_formats('flash', uu, vu, media_id) + self._get_formats('html5', uu, vu, media_id) | |
5f432ac8 FF |
400 | self._sort_formats(formats) |
401 | ||
402 | return { | |
403 | 'id': media_id, | |
0428106d | 404 | 'title': 'Video %s' % media_id, |
5f432ac8 FF |
405 | 'formats': formats, |
406 | } |