2 from __future__ import unicode_literals
10 from .common import InfoExtractor
11 from ..compat import (
28 class LetvIE(InfoExtractor):
30 _VALID_URL = r'http://www\.letv\.com/ptv/vplay/(?P<id>\d+).html'
33 'url': 'http://www.letv.com/ptv/vplay/22005890.html',
34 'md5': 'edadcfe5406976f42f9f266057ee5e40',
38 'title': '第87届奥斯卡颁奖礼完美落幕 《鸟人》成最大赢家',
39 'description': 'md5:a9cb175fd753e2962176b7beca21a47c',
42 'hls_prefer_native': True,
45 'url': 'http://www.letv.com/ptv/vplay/1415246.html',
50 'description': 'md5:f88573d9d7225ada1359eaf0dbf8bcda',
53 'hls_prefer_native': True,
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',
58 'md5': '2424c74948a62e5f31988438979c5ad1',
63 'description': 'md5:7506a5eeb1722bb9d4068f85024e3986',
66 'hls_prefer_native': True,
68 'skip': 'Only available in China',
73 return val >> n if val >= 0 else (val + 0x100000000) >> n
75 # ror() and calc_time_key() are reversed from a embedded swf file in KLetvPlayer.swf
76 def ror(self, param1, param2):
78 while _loc3_ < param2:
79 param1 = self.urshift(param1, 1) + ((param1 & 1) << 31)
83 def calc_time_key(self, param1):
85 _loc3_ = self.ror(param1, _loc2_ % 13)
86 _loc3_ = _loc3_ ^ _loc2_
87 _loc3_ = self.ror(_loc3_, _loc2_ % 17)
90 # see M3U8Encryption class in KLetvPlayer.swf
92 def decrypt_m3u8(encrypted_data):
93 if encrypted_data[:5].decode('utf-8').lower() != 'vc_01':
95 encrypted_data = encrypted_data[5:]
99 b = compat_ord(encrypted_data[0])
100 _loc4_.extend([b // 16, b & 0x0f])
101 encrypted_data = encrypted_data[1:]
102 idx = len(_loc4_) - 11
103 _loc4_ = _loc4_[idx:] + _loc4_[:idx]
106 _loc7_.append(_loc4_[0] * 16 + _loc4_[1])
111 def _real_extract(self, url):
112 media_id = self._match_id(url)
113 page = self._download_webpage(url, media_id)
119 'tkey': self.calc_time_key(int(time.time())),
120 'domain': 'www.letv.com'
122 play_json_req = sanitized_Request(
123 'http://api.letv.com/mms/out/video/playJson?' + compat_urllib_parse.urlencode(params)
125 cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
126 if cn_verification_proxy:
127 play_json_req.add_header('Ytdl-request-proxy', cn_verification_proxy)
129 play_json = self._download_json(
131 media_id, 'Downloading playJson data')
134 playstatus = play_json['playstatus']
135 if playstatus['status'] == 0:
136 flag = playstatus['flag']
138 msg = 'Country %s auth error' % playstatus['country']
140 msg = 'Generic error. flag = %d' % flag
141 raise ExtractorError(msg, expected=True)
143 playurl = play_json['playurl']
145 formats = ['350', '1000', '1300', '720p', '1080p']
146 dispatch = playurl['dispatch']
149 for format_id in formats:
150 if format_id in dispatch:
151 media_url = playurl['domain'][0] + dispatch[format_id][0]
152 media_url += '&' + compat_urllib_parse.urlencode({
159 nodes_data = self._download_json(
161 'Download JSON metadata for format %s' % format_id)
163 req = self._request_webpage(
164 nodes_data['nodelist'][0]['location'], media_id,
165 note='Downloading m3u8 information for format %s' % format_id)
167 m3u8_data = self.decrypt_m3u8(req.read())
170 'url': encode_data_uri(m3u8_data, 'application/vnd.apple.mpegurl'),
171 'ext': determine_ext(dispatch[format_id][1]),
172 'format_id': format_id,
176 if format_id[-1:] == 'p':
177 url_info_dict['height'] = int_or_none(format_id[:-1])
179 urls.append(url_info_dict)
181 publish_time = parse_iso8601(self._html_search_regex(
182 r'发布时间 ([^<>]+) ', page, 'publish time', default=None),
183 delimiter=' ', timezone=datetime.timedelta(hours=8))
184 description = self._html_search_meta('description', page, fatal=False)
189 'title': playurl['title'],
190 'thumbnail': playurl['pic'],
191 'description': description,
192 'timestamp': publish_time,
196 class LetvTvIE(InfoExtractor):
197 _VALID_URL = r'http://www.letv.com/tv/(?P<id>\d+).html'
199 'url': 'http://www.letv.com/tv/46177.html',
203 'description': 'md5:395666ff41b44080396e59570dbac01c'
208 def _real_extract(self, url):
209 playlist_id = self._match_id(url)
210 page = self._download_webpage(url, playlist_id)
212 media_urls = list(set(re.findall(
213 r'http://www.letv.com/ptv/vplay/\d+.html', page)))
214 entries = [self.url_result(media_url, ie='Letv')
215 for media_url in media_urls]
217 title = self._html_search_meta('keywords', page,
218 fatal=False).split(',')[0]
219 description = self._html_search_meta('description', page, fatal=False)
221 return self.playlist_result(entries, playlist_id, playlist_title=title,
222 playlist_description=description)
225 class LetvPlaylistIE(LetvTvIE):
226 _VALID_URL = r'http://tv.letv.com/[a-z]+/(?P<id>[a-z]+)/index.s?html'
228 'url': 'http://tv.letv.com/izt/wuzetian/index.html',
232 'description': 'md5:e12499475ab3d50219e5bba00b3cb248'
234 # This playlist contains some extra videos other than the drama itself
235 'playlist_mincount': 96
237 'url': 'http://tv.letv.com/pzt/lswjzzjc/index.shtml',
240 # The title should be "劲舞青春", but I can't find a simple way to
241 # determine the playlist title
243 'description': 'md5:b1eef244f45589a7b5b1af9ff25a4489'
245 'playlist_mincount': 7
249 class LetvCloudIE(InfoExtractor):
251 _VALID_URL = r'https?://yuntv\.letv\.com/bcloud.html\?.+'
254 'url': 'http://yuntv.letv.com/bcloud.html?uu=p7jnfw5hw9&vu=467623dedf',
255 'md5': '26450599afd64c513bc77030ad15db44',
257 'id': 'p7jnfw5hw9_467623dedf',
259 'title': 'Video p7jnfw5hw9_467623dedf',
262 'url': 'http://yuntv.letv.com/bcloud.html?uu=p7jnfw5hw9&vu=ec93197892&pu=2c7cd40209&auto_play=1&gpcflag=1&width=640&height=360',
263 'md5': 'e03d9cc8d9c13191e1caf277e42dbd31',
265 'id': 'p7jnfw5hw9_ec93197892',
267 'title': 'Video p7jnfw5hw9_ec93197892',
270 'url': 'http://yuntv.letv.com/bcloud.html?uu=p7jnfw5hw9&vu=187060b6fd',
271 'md5': 'cb988699a776b22d4a41b9d43acfb3ac',
273 'id': 'p7jnfw5hw9_187060b6fd',
275 'title': 'Video p7jnfw5hw9_187060b6fd',
281 if obj['cf'] == 'flash':
282 salt = '2f9d6924b33a165a6d8b5d3d42f4f987'
283 items = ['cf', 'format', 'ran', 'uu', 'ver', 'vu']
284 elif obj['cf'] == 'html5':
285 salt = 'fbeh5player12c43eccf2bec3300344'
286 items = ['cf', 'ran', 'uu', 'bver', 'vu']
287 input_data = ''.join([item + obj[item] for item in items]) + salt
288 obj['sign'] = hashlib.md5(input_data.encode('utf-8')).hexdigest()
290 def _get_formats(self, cf, uu, vu, media_id):
291 def get_play_json(cf, timestamp):
295 'bver': 'firefox44.0',
299 'ran': compat_str(timestamp),
302 return self._download_json(
303 'http://api.letvcloud.com/gpc.php?' + compat_urllib_parse.urlencode(data),
304 media_id, 'Downloading playJson data for type %s' % cf)
306 play_json = get_play_json(cf, time.time())
307 # The server time may be different from local time
308 if play_json.get('code') == 10071:
309 play_json = get_play_json(cf, play_json['timestamp'])
311 if not play_json.get('data'):
312 if play_json.get('message'):
313 raise ExtractorError('Letv cloud said: %s' % play_json['message'], expected=True)
314 elif play_json.get('code'):
315 raise ExtractorError('Letv cloud returned error %d' % play_json['code'], expected=True)
317 raise ExtractorError('Letv cloud returned an unknwon error')
320 return base64.b64decode(s.encode('utf-8')).decode('utf-8')
323 for media in play_json['data']['video_info']['media'].values():
324 play_url = media['play_url']
325 url = b64decode(play_url['main_url'])
326 decoded_url = b64decode(url_basename(url))
329 'ext': determine_ext(decoded_url),
330 'format_id': int_or_none(play_url.get('vtype')),
331 'format_note': str_or_none(play_url.get('definition')),
332 'width': int_or_none(play_url.get('vwidth')),
333 'height': int_or_none(play_url.get('vheight')),
338 def _real_extract(self, url):
339 uu_mobj = re.search('uu=([\w]+)', url)
340 vu_mobj = re.search('vu=([\w]+)', url)
342 if not uu_mobj or not vu_mobj:
343 raise ExtractorError('Invalid URL: %s' % url, expected=True)
345 uu = uu_mobj.group(1)
346 vu = vu_mobj.group(1)
347 media_id = uu + '_' + vu
349 formats = self._get_formats('flash', uu, vu, media_id) + self._get_formats('html5', uu, vu, media_id)
350 self._sort_formats(formats)
354 'title': 'Video %s' % media_id,