]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/leeco.py
[youtube:channel] Improve channel id extraction and detect unavailable channels ...
[yt-dlp.git] / youtube_dl / extractor / leeco.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import base64
5 import datetime
6 import hashlib
7 import re
8 import time
9
10 from .common import InfoExtractor
11 from ..compat import (
12 compat_ord,
13 compat_str,
14 compat_urllib_parse_urlencode,
15 )
16 from ..utils import (
17 determine_ext,
18 encode_data_uri,
19 ExtractorError,
20 int_or_none,
21 orderedSet,
22 parse_iso8601,
23 str_or_none,
24 url_basename,
25 urshift,
26 )
27
28
29 class LeIE(InfoExtractor):
30 IE_DESC = '乐视网'
31 _VALID_URL = r'https?://(?:www\.le\.com/ptv/vplay|sports\.le\.com/video)/(?P<id>\d+)\.html'
32
33 _URL_TEMPLATE = 'http://www.le.com/ptv/vplay/%s.html'
34
35 _TESTS = [{
36 'url': 'http://www.le.com/ptv/vplay/22005890.html',
37 'md5': 'edadcfe5406976f42f9f266057ee5e40',
38 'info_dict': {
39 'id': '22005890',
40 'ext': 'mp4',
41 'title': '第87届奥斯卡颁奖礼完美落幕 《鸟人》成最大赢家',
42 'description': 'md5:a9cb175fd753e2962176b7beca21a47c',
43 },
44 'params': {
45 'hls_prefer_native': True,
46 },
47 }, {
48 'url': 'http://www.le.com/ptv/vplay/1415246.html',
49 'info_dict': {
50 'id': '1415246',
51 'ext': 'mp4',
52 'title': '美人天下01',
53 'description': 'md5:f88573d9d7225ada1359eaf0dbf8bcda',
54 },
55 'params': {
56 'hls_prefer_native': True,
57 },
58 }, {
59 'note': 'This video is available only in Mainland China, thus a proxy is needed',
60 'url': 'http://www.le.com/ptv/vplay/1118082.html',
61 'md5': '2424c74948a62e5f31988438979c5ad1',
62 'info_dict': {
63 'id': '1118082',
64 'ext': 'mp4',
65 'title': '与龙共舞 完整版',
66 'description': 'md5:7506a5eeb1722bb9d4068f85024e3986',
67 },
68 'params': {
69 'hls_prefer_native': True,
70 },
71 'skip': 'Only available in China',
72 }, {
73 'url': 'http://sports.le.com/video/25737697.html',
74 'only_matching': True,
75 }]
76
77 # ror() and calc_time_key() are reversed from a embedded swf file in KLetvPlayer.swf
78 def ror(self, param1, param2):
79 _loc3_ = 0
80 while _loc3_ < param2:
81 param1 = urshift(param1, 1) + ((param1 & 1) << 31)
82 _loc3_ += 1
83 return param1
84
85 def calc_time_key(self, param1):
86 _loc2_ = 773625421
87 _loc3_ = self.ror(param1, _loc2_ % 13)
88 _loc3_ = _loc3_ ^ _loc2_
89 _loc3_ = self.ror(_loc3_, _loc2_ % 17)
90 return _loc3_
91
92 # see M3U8Encryption class in KLetvPlayer.swf
93 @staticmethod
94 def decrypt_m3u8(encrypted_data):
95 if encrypted_data[:5].decode('utf-8').lower() != 'vc_01':
96 return encrypted_data
97 encrypted_data = encrypted_data[5:]
98
99 _loc4_ = bytearray(2 * len(encrypted_data))
100 for idx, val in enumerate(encrypted_data):
101 b = compat_ord(val)
102 _loc4_[2 * idx] = b // 16
103 _loc4_[2 * idx + 1] = b % 16
104 idx = len(_loc4_) - 11
105 _loc4_ = _loc4_[idx:] + _loc4_[:idx]
106 _loc7_ = bytearray(len(encrypted_data))
107 for i in range(len(encrypted_data)):
108 _loc7_[i] = _loc4_[2 * i] * 16 + _loc4_[2 * i + 1]
109
110 return bytes(_loc7_)
111
112 def _real_extract(self, url):
113 media_id = self._match_id(url)
114 page = self._download_webpage(url, media_id)
115 params = {
116 'id': media_id,
117 'platid': 1,
118 'splatid': 101,
119 'format': 1,
120 'tkey': self.calc_time_key(int(time.time())),
121 'domain': 'www.le.com'
122 }
123
124 play_json = self._download_json(
125 'http://api.le.com/mms/out/video/playJson',
126 media_id, 'Downloading playJson data', query=params,
127 headers=self.geo_verification_headers())
128
129 # Check for errors
130 playstatus = play_json['playstatus']
131 if playstatus['status'] == 0:
132 flag = playstatus['flag']
133 if flag == 1:
134 msg = 'Country %s auth error' % playstatus['country']
135 else:
136 msg = 'Generic error. flag = %d' % flag
137 raise ExtractorError(msg, expected=True)
138
139 playurl = play_json['playurl']
140
141 formats = ['350', '1000', '1300', '720p', '1080p']
142 dispatch = playurl['dispatch']
143
144 urls = []
145 for format_id in formats:
146 if format_id in dispatch:
147 media_url = playurl['domain'][0] + dispatch[format_id][0]
148 media_url += '&' + compat_urllib_parse_urlencode({
149 'm3v': 1,
150 'format': 1,
151 'expect': 3,
152 'rateid': format_id,
153 })
154
155 nodes_data = self._download_json(
156 media_url, media_id,
157 'Download JSON metadata for format %s' % format_id)
158
159 req = self._request_webpage(
160 nodes_data['nodelist'][0]['location'], media_id,
161 note='Downloading m3u8 information for format %s' % format_id)
162
163 m3u8_data = self.decrypt_m3u8(req.read())
164
165 url_info_dict = {
166 'url': encode_data_uri(m3u8_data, 'application/vnd.apple.mpegurl'),
167 'ext': determine_ext(dispatch[format_id][1]),
168 'format_id': format_id,
169 'protocol': 'm3u8',
170 }
171
172 if format_id[-1:] == 'p':
173 url_info_dict['height'] = int_or_none(format_id[:-1])
174
175 urls.append(url_info_dict)
176
177 publish_time = parse_iso8601(self._html_search_regex(
178 r'发布时间&nbsp;([^<>]+) ', page, 'publish time', default=None),
179 delimiter=' ', timezone=datetime.timedelta(hours=8))
180 description = self._html_search_meta('description', page, fatal=False)
181
182 return {
183 'id': media_id,
184 'formats': urls,
185 'title': playurl['title'],
186 'thumbnail': playurl['pic'],
187 'description': description,
188 'timestamp': publish_time,
189 }
190
191
192 class LePlaylistIE(InfoExtractor):
193 _VALID_URL = r'https?://[a-z]+\.le\.com/(?!video)[a-z]+/(?P<id>[a-z0-9_]+)'
194
195 _TESTS = [{
196 'url': 'http://www.le.com/tv/46177.html',
197 'info_dict': {
198 'id': '46177',
199 'title': '美人天下',
200 'description': 'md5:395666ff41b44080396e59570dbac01c'
201 },
202 'playlist_count': 35
203 }, {
204 'url': 'http://tv.le.com/izt/wuzetian/index.html',
205 'info_dict': {
206 'id': 'wuzetian',
207 'title': '武媚娘传奇',
208 'description': 'md5:e12499475ab3d50219e5bba00b3cb248'
209 },
210 # This playlist contains some extra videos other than the drama itself
211 'playlist_mincount': 96
212 }, {
213 'url': 'http://tv.le.com/pzt/lswjzzjc/index.shtml',
214 # This series is moved to http://www.le.com/tv/10005297.html
215 'only_matching': True,
216 }, {
217 'url': 'http://www.le.com/comic/92063.html',
218 'only_matching': True,
219 }, {
220 'url': 'http://list.le.com/listn/c1009_sc532002_d2_p1_o1.html',
221 'only_matching': True,
222 }]
223
224 @classmethod
225 def suitable(cls, url):
226 return False if LeIE.suitable(url) else super(LePlaylistIE, cls).suitable(url)
227
228 def _real_extract(self, url):
229 playlist_id = self._match_id(url)
230 page = self._download_webpage(url, playlist_id)
231
232 # Currently old domain names are still used in playlists
233 media_ids = orderedSet(re.findall(
234 r'<a[^>]+href="http://www\.letv\.com/ptv/vplay/(\d+)\.html', page))
235 entries = [self.url_result(LeIE._URL_TEMPLATE % media_id, ie='Le')
236 for media_id in media_ids]
237
238 title = self._html_search_meta('keywords', page,
239 fatal=False).split(',')[0]
240 description = self._html_search_meta('description', page, fatal=False)
241
242 return self.playlist_result(entries, playlist_id, playlist_title=title,
243 playlist_description=description)
244
245
246 class LetvCloudIE(InfoExtractor):
247 # Most of *.letv.com is changed to *.le.com on 2016/01/02
248 # but yuntv.letv.com is kept, so also keep the extractor name
249 IE_DESC = '乐视云'
250 _VALID_URL = r'https?://yuntv\.letv\.com/bcloud.html\?.+'
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',
258 'title': 'Video p7jnfw5hw9_467623dedf',
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',
262 'md5': 'e03d9cc8d9c13191e1caf277e42dbd31',
263 'info_dict': {
264 'id': 'p7jnfw5hw9_ec93197892',
265 'ext': 'mp4',
266 'title': 'Video p7jnfw5hw9_ec93197892',
267 },
268 }, {
269 'url': 'http://yuntv.letv.com/bcloud.html?uu=p7jnfw5hw9&vu=187060b6fd',
270 'md5': 'cb988699a776b22d4a41b9d43acfb3ac',
271 'info_dict': {
272 'id': 'p7jnfw5hw9_187060b6fd',
273 'ext': 'mp4',
274 'title': 'Video p7jnfw5hw9_187060b6fd',
275 },
276 }]
277
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'])
309
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
318 def b64decode(s):
319 return base64.b64decode(s.encode('utf-8')).decode('utf-8')
320
321 formats = []
322 for media in play_json['data']['video_info']['media'].values():
323 play_url = media['play_url']
324 url = b64decode(play_url['main_url'])
325 decoded_url = b64decode(url_basename(url))
326 formats.append({
327 'url': url,
328 'ext': determine_ext(decoded_url),
329 'format_id': str_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 })
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)
349 self._sort_formats(formats)
350
351 return {
352 'id': media_id,
353 'title': 'Video %s' % media_id,
354 'formats': formats,
355 }