2 from __future__
import unicode_literals
10 from .common
import InfoExtractor
11 from ..compat
import (
14 compat_urllib_parse_urlencode
,
30 class LeIE(InfoExtractor
):
32 _VALID_URL
= r
'https?://(?:www\.le\.com/ptv/vplay|(?:sports\.le|(?:www\.)?lesports)\.com/(?:match|video))/(?P<id>\d+)\.html'
33 _GEO_COUNTRIES
= ['CN']
34 _URL_TEMPLATE
= 'http://www.le.com/ptv/vplay/%s.html'
37 'url': 'http://www.le.com/ptv/vplay/22005890.html',
38 'md5': 'edadcfe5406976f42f9f266057ee5e40',
42 'title': '第87届奥斯卡颁奖礼完美落幕 《鸟人》成最大赢家',
43 'description': 'md5:a9cb175fd753e2962176b7beca21a47c',
46 'hls_prefer_native': True,
49 'url': 'http://www.le.com/ptv/vplay/1415246.html',
54 'description': 'md5:f88573d9d7225ada1359eaf0dbf8bcda',
57 'hls_prefer_native': True,
60 'note': 'This video is available only in Mainland China, thus a proxy is needed',
61 'url': 'http://www.le.com/ptv/vplay/1118082.html',
62 'md5': '2424c74948a62e5f31988438979c5ad1',
67 'description': 'md5:7506a5eeb1722bb9d4068f85024e3986',
70 'hls_prefer_native': True,
72 'skip': 'Only available in China',
74 'url': 'http://sports.le.com/video/25737697.html',
75 'only_matching': True,
77 'url': 'http://www.lesports.com/match/1023203003.html',
78 'only_matching': True,
80 'url': 'http://sports.le.com/match/1023203003.html',
81 'only_matching': True,
84 # ror() and calc_time_key() are reversed from a embedded swf file in KLetvPlayer.swf
85 def ror(self
, param1
, param2
):
87 while _loc3_
< param2
:
88 param1
= urshift(param1
, 1) + ((param1
& 1) << 31)
92 def calc_time_key(self
, param1
):
94 _loc3_
= self
.ror(param1
, _loc2_
% 13)
95 _loc3_
= _loc3_ ^ _loc2_
96 _loc3_
= self
.ror(_loc3_
, _loc2_
% 17)
99 # reversed from http://jstatic.letvcdn.com/sdk/player.js
100 def get_mms_key(self
, time
):
101 return self
.ror(time
, 8) ^
185025305
103 # see M3U8Encryption class in KLetvPlayer.swf
105 def decrypt_m3u8(encrypted_data
):
106 if encrypted_data
[:5].decode('utf-8').lower() != 'vc_01':
107 return encrypted_data
108 encrypted_data
= encrypted_data
[5:]
110 _loc4_
= bytearray(2 * len(encrypted_data
))
111 for idx
, val
in enumerate(encrypted_data
):
113 _loc4_
[2 * idx
] = b
// 16
114 _loc4_
[2 * idx
+ 1] = b
% 16
115 idx
= len(_loc4_
) - 11
116 _loc4_
= _loc4_
[idx
:] + _loc4_
[:idx
]
117 _loc7_
= bytearray(len(encrypted_data
))
118 for i
in range(len(encrypted_data
)):
119 _loc7_
[i
] = _loc4_
[2 * i
] * 16 + _loc4_
[2 * i
+ 1]
123 def _check_errors(self
, play_json
):
125 playstatus
= play_json
['playstatus']
126 if playstatus
['status'] == 0:
127 flag
= playstatus
['flag']
129 self
.raise_geo_restricted()
131 raise ExtractorError('Generic error. flag = %d' % flag
, expected
=True)
133 def _real_extract(self
, url
):
134 media_id
= self
._match
_id
(url
)
135 page
= self
._download
_webpage
(url
, media_id
)
137 play_json_h5
= self
._download
_json
(
138 'http://api.le.com/mms/out/video/playJsonH5',
139 media_id
, 'Downloading html5 playJson data', query
={
144 'tkey': self
.get_mms_key(int(time
.time())),
145 'domain': 'www.le.com',
148 headers
=self
.geo_verification_headers())
149 self
._check
_errors
(play_json_h5
)
151 play_json_flash
= self
._download
_json
(
152 'http://api.le.com/mms/out/video/playJson',
153 media_id
, 'Downloading flash playJson data', query
={
158 'tkey': self
.calc_time_key(int(time
.time())),
159 'domain': 'www.le.com',
161 headers
=self
.geo_verification_headers())
162 self
._check
_errors
(play_json_flash
)
164 def get_h5_urls(media_url
, format_id
):
165 location
= self
._download
_json
(
167 'Download JSON metadata for format %s' % format_id
, query
={
174 'http': update_url_query(location
, {'tss': 'no'}),
175 'hls': update_url_query(location
, {'tss': 'ios'}),
178 def get_flash_urls(media_url
, format_id
):
179 media_url
+= '&' + compat_urllib_parse_urlencode({
186 nodes_data
= self
._download
_json
(
188 'Download JSON metadata for format %s' % format_id
)
190 req
= self
._request
_webpage
(
191 nodes_data
['nodelist'][0]['location'], media_id
,
192 note
='Downloading m3u8 information for format %s' % format_id
)
194 m3u8_data
= self
.decrypt_m3u8(req
.read())
197 'hls': encode_data_uri(m3u8_data
, 'application/vnd.apple.mpegurl'),
200 extracted_formats
= []
202 for play_json
, get_urls
in ((play_json_h5
, get_h5_urls
), (play_json_flash
, get_flash_urls
)):
203 playurl
= play_json
['playurl']
204 play_domain
= playurl
['domain'][0]
206 for format_id
, format_data
in playurl
.get('dispatch', []).items():
207 if format_id
in extracted_formats
:
209 extracted_formats
.append(format_id
)
211 media_url
= play_domain
+ format_data
[0]
212 for protocol
, format_url
in get_urls(media_url
, format_id
).items():
215 'ext': determine_ext(format_data
[1]),
216 'format_id': '%s-%s' % (protocol
, format_id
),
217 'protocol': 'm3u8_native' if protocol
== 'hls' else 'http',
218 'quality': int_or_none(format_id
),
221 if format_id
[-1:] == 'p':
222 f
['height'] = int_or_none(format_id
[:-1])
225 self
._sort
_formats
(formats
, ('height', 'quality', 'format_id'))
227 publish_time
= parse_iso8601(self
._html
_search
_regex
(
228 r
'发布时间 ([^<>]+) ', page
, 'publish time', default
=None),
229 delimiter
=' ', timezone
=datetime
.timedelta(hours
=8))
230 description
= self
._html
_search
_meta
('description', page
, fatal
=False)
235 'title': playurl
['title'],
236 'thumbnail': playurl
['pic'],
237 'description': description
,
238 'timestamp': publish_time
,
242 class LePlaylistIE(InfoExtractor
):
243 _VALID_URL
= r
'https?://[a-z]+\.le\.com/(?!video)[a-z]+/(?P<id>[a-z0-9_]+)'
246 'url': 'http://www.le.com/tv/46177.html',
250 'description': 'md5:395666ff41b44080396e59570dbac01c'
254 'url': 'http://tv.le.com/izt/wuzetian/index.html',
258 'description': 'md5:e12499475ab3d50219e5bba00b3cb248'
260 # This playlist contains some extra videos other than the drama itself
261 'playlist_mincount': 96
263 'url': 'http://tv.le.com/pzt/lswjzzjc/index.shtml',
264 # This series is moved to http://www.le.com/tv/10005297.html
265 'only_matching': True,
267 'url': 'http://www.le.com/comic/92063.html',
268 'only_matching': True,
270 'url': 'http://list.le.com/listn/c1009_sc532002_d2_p1_o1.html',
271 'only_matching': True,
275 def suitable(cls
, url
):
276 return False if LeIE
.suitable(url
) else super(LePlaylistIE
, cls
).suitable(url
)
278 def _real_extract(self
, url
):
279 playlist_id
= self
._match
_id
(url
)
280 page
= self
._download
_webpage
(url
, playlist_id
)
282 # Currently old domain names are still used in playlists
283 media_ids
= orderedSet(re
.findall(
284 r
'<a[^>]+href="http://www\.letv\.com/ptv/vplay/(\d+)\.html', page
))
285 entries
= [self
.url_result(LeIE
._URL
_TEMPLATE
% media_id
, ie
='Le')
286 for media_id
in media_ids
]
288 title
= self
._html
_search
_meta
('keywords', page
,
289 fatal
=False).split(',')[0]
290 description
= self
._html
_search
_meta
('description', page
, fatal
=False)
292 return self
.playlist_result(entries
, playlist_id
, playlist_title
=title
,
293 playlist_description
=description
)
296 class LetvCloudIE(InfoExtractor
):
297 # Most of *.letv.com is changed to *.le.com on 2016/01/02
298 # but yuntv.letv.com is kept, so also keep the extractor name
300 _VALID_URL
= r
'https?://yuntv\.letv\.com/bcloud.html\?.+'
303 'url': 'http://yuntv.letv.com/bcloud.html?uu=p7jnfw5hw9&vu=467623dedf',
304 'md5': '26450599afd64c513bc77030ad15db44',
306 'id': 'p7jnfw5hw9_467623dedf',
308 'title': 'Video p7jnfw5hw9_467623dedf',
311 'url': 'http://yuntv.letv.com/bcloud.html?uu=p7jnfw5hw9&vu=ec93197892&pu=2c7cd40209&auto_play=1&gpcflag=1&width=640&height=360',
312 'md5': 'e03d9cc8d9c13191e1caf277e42dbd31',
314 'id': 'p7jnfw5hw9_ec93197892',
316 'title': 'Video p7jnfw5hw9_ec93197892',
319 'url': 'http://yuntv.letv.com/bcloud.html?uu=p7jnfw5hw9&vu=187060b6fd',
320 'md5': 'cb988699a776b22d4a41b9d43acfb3ac',
322 'id': 'p7jnfw5hw9_187060b6fd',
324 'title': 'Video p7jnfw5hw9_187060b6fd',
330 if obj
['cf'] == 'flash':
331 salt
= '2f9d6924b33a165a6d8b5d3d42f4f987'
332 items
= ['cf', 'format', 'ran', 'uu', 'ver', 'vu']
333 elif obj
['cf'] == 'html5':
334 salt
= 'fbeh5player12c43eccf2bec3300344'
335 items
= ['cf', 'ran', 'uu', 'bver', 'vu']
336 input_data
= ''.join([item
+ obj
[item
] for item
in items
]) + salt
337 obj
['sign'] = hashlib
.md5(input_data
.encode('utf-8')).hexdigest()
339 def _get_formats(self
, cf
, uu
, vu
, media_id
):
340 def get_play_json(cf
, timestamp
):
344 'bver': 'firefox44.0',
348 'ran': compat_str(timestamp
),
351 return self
._download
_json
(
352 'http://api.letvcloud.com/gpc.php?' + compat_urllib_parse_urlencode(data
),
353 media_id
, 'Downloading playJson data for type %s' % cf
)
355 play_json
= get_play_json(cf
, time
.time())
356 # The server time may be different from local time
357 if play_json
.get('code') == 10071:
358 play_json
= get_play_json(cf
, play_json
['timestamp'])
360 if not play_json
.get('data'):
361 if play_json
.get('message'):
362 raise ExtractorError('Letv cloud said: %s' % play_json
['message'], expected
=True)
363 elif play_json
.get('code'):
364 raise ExtractorError('Letv cloud returned error %d' % play_json
['code'], expected
=True)
366 raise ExtractorError('Letv cloud returned an unknwon error')
369 return base64
.b64decode(s
.encode('utf-8')).decode('utf-8')
372 for media
in play_json
['data']['video_info']['media'].values():
373 play_url
= media
['play_url']
374 url
= b64decode(play_url
['main_url'])
375 decoded_url
= b64decode(url_basename(url
))
378 'ext': determine_ext(decoded_url
),
379 'format_id': str_or_none(play_url
.get('vtype')),
380 'format_note': str_or_none(play_url
.get('definition')),
381 'width': int_or_none(play_url
.get('vwidth')),
382 'height': int_or_none(play_url
.get('vheight')),
387 def _real_extract(self
, url
):
388 uu_mobj
= re
.search(r
'uu=([\w]+)', url
)
389 vu_mobj
= re
.search(r
'vu=([\w]+)', url
)
391 if not uu_mobj
or not vu_mobj
:
392 raise ExtractorError('Invalid URL: %s' % url
, expected
=True)
394 uu
= uu_mobj
.group(1)
395 vu
= vu_mobj
.group(1)
396 media_id
= uu
+ '_' + vu
398 formats
= self
._get
_formats
('flash', uu
, vu
, media_id
) + self
._get
_formats
('html5', uu
, vu
, media_id
)
399 self
._sort
_formats
(formats
)
403 'title': 'Video %s' % media_id
,