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