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