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