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