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