You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

355 lines
12 KiB

10 years ago
10 years ago
10 years ago
Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import datetime
  4. import re
  5. import time
  6. import base64
  7. import hashlib
  8. from .common import InfoExtractor
  9. from ..compat import (
  10. compat_urllib_parse,
  11. compat_ord,
  12. compat_str,
  13. )
  14. from ..utils import (
  15. determine_ext,
  16. ExtractorError,
  17. parse_iso8601,
  18. sanitized_Request,
  19. int_or_none,
  20. str_or_none,
  21. encode_data_uri,
  22. url_basename,
  23. )
  24. class LetvIE(InfoExtractor):
  25. IE_DESC = '乐视网'
  26. _VALID_URL = r'http://www\.letv\.com/ptv/vplay/(?P<id>\d+).html'
  27. _TESTS = [{
  28. 'url': 'http://www.letv.com/ptv/vplay/22005890.html',
  29. 'md5': 'edadcfe5406976f42f9f266057ee5e40',
  30. 'info_dict': {
  31. 'id': '22005890',
  32. 'ext': 'mp4',
  33. 'title': '第87届奥斯卡颁奖礼完美落幕 《鸟人》成最大赢家',
  34. 'description': 'md5:a9cb175fd753e2962176b7beca21a47c',
  35. },
  36. 'params': {
  37. 'hls_prefer_native': True,
  38. },
  39. }, {
  40. 'url': 'http://www.letv.com/ptv/vplay/1415246.html',
  41. 'info_dict': {
  42. 'id': '1415246',
  43. 'ext': 'mp4',
  44. 'title': '美人天下01',
  45. 'description': 'md5:f88573d9d7225ada1359eaf0dbf8bcda',
  46. },
  47. 'params': {
  48. 'hls_prefer_native': True,
  49. },
  50. }, {
  51. 'note': 'This video is available only in Mainland China, thus a proxy is needed',
  52. 'url': 'http://www.letv.com/ptv/vplay/1118082.html',
  53. 'md5': '2424c74948a62e5f31988438979c5ad1',
  54. 'info_dict': {
  55. 'id': '1118082',
  56. 'ext': 'mp4',
  57. 'title': '与龙共舞 完整版',
  58. 'description': 'md5:7506a5eeb1722bb9d4068f85024e3986',
  59. },
  60. 'params': {
  61. 'hls_prefer_native': True,
  62. },
  63. 'skip': 'Only available in China',
  64. }]
  65. @staticmethod
  66. def urshift(val, n):
  67. return val >> n if val >= 0 else (val + 0x100000000) >> n
  68. # ror() and calc_time_key() are reversed from a embedded swf file in KLetvPlayer.swf
  69. def ror(self, param1, param2):
  70. _loc3_ = 0
  71. while _loc3_ < param2:
  72. param1 = self.urshift(param1, 1) + ((param1 & 1) << 31)
  73. _loc3_ += 1
  74. return param1
  75. def calc_time_key(self, param1):
  76. _loc2_ = 773625421
  77. _loc3_ = self.ror(param1, _loc2_ % 13)
  78. _loc3_ = _loc3_ ^ _loc2_
  79. _loc3_ = self.ror(_loc3_, _loc2_ % 17)
  80. return _loc3_
  81. # see M3U8Encryption class in KLetvPlayer.swf
  82. @staticmethod
  83. def decrypt_m3u8(encrypted_data):
  84. if encrypted_data[:5].decode('utf-8').lower() != 'vc_01':
  85. return encrypted_data
  86. encrypted_data = encrypted_data[5:]
  87. _loc4_ = bytearray(2 * len(encrypted_data))
  88. for idx, val in enumerate(encrypted_data):
  89. b = compat_ord(val)
  90. _loc4_[2 * idx] = b // 16
  91. _loc4_[2 * idx + 1] = b % 16
  92. idx = len(_loc4_) - 11
  93. _loc4_ = _loc4_[idx:] + _loc4_[:idx]
  94. _loc7_ = bytearray(len(encrypted_data))
  95. for i in range(len(encrypted_data)):
  96. _loc7_[i] = _loc4_[2 * i] * 16 + _loc4_[2 * i + 1]
  97. return bytes(_loc7_)
  98. def _real_extract(self, url):
  99. media_id = self._match_id(url)
  100. page = self._download_webpage(url, media_id)
  101. params = {
  102. 'id': media_id,
  103. 'platid': 1,
  104. 'splatid': 101,
  105. 'format': 1,
  106. 'tkey': self.calc_time_key(int(time.time())),
  107. 'domain': 'www.letv.com'
  108. }
  109. play_json_req = sanitized_Request(
  110. 'http://api.letv.com/mms/out/video/playJson?' + compat_urllib_parse.urlencode(params)
  111. )
  112. cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
  113. if cn_verification_proxy:
  114. play_json_req.add_header('Ytdl-request-proxy', cn_verification_proxy)
  115. play_json = self._download_json(
  116. play_json_req,
  117. media_id, 'Downloading playJson data')
  118. # Check for errors
  119. playstatus = play_json['playstatus']
  120. if playstatus['status'] == 0:
  121. flag = playstatus['flag']
  122. if flag == 1:
  123. msg = 'Country %s auth error' % playstatus['country']
  124. else:
  125. msg = 'Generic error. flag = %d' % flag
  126. raise ExtractorError(msg, expected=True)
  127. playurl = play_json['playurl']
  128. formats = ['350', '1000', '1300', '720p', '1080p']
  129. dispatch = playurl['dispatch']
  130. urls = []
  131. for format_id in formats:
  132. if format_id in dispatch:
  133. media_url = playurl['domain'][0] + dispatch[format_id][0]
  134. media_url += '&' + compat_urllib_parse.urlencode({
  135. 'm3v': 1,
  136. 'format': 1,
  137. 'expect': 3,
  138. 'rateid': format_id,
  139. })
  140. nodes_data = self._download_json(
  141. media_url, media_id,
  142. 'Download JSON metadata for format %s' % format_id)
  143. req = self._request_webpage(
  144. nodes_data['nodelist'][0]['location'], media_id,
  145. note='Downloading m3u8 information for format %s' % format_id)
  146. m3u8_data = self.decrypt_m3u8(req.read())
  147. url_info_dict = {
  148. 'url': encode_data_uri(m3u8_data, 'application/vnd.apple.mpegurl'),
  149. 'ext': determine_ext(dispatch[format_id][1]),
  150. 'format_id': format_id,
  151. 'protocol': 'm3u8',
  152. }
  153. if format_id[-1:] == 'p':
  154. url_info_dict['height'] = int_or_none(format_id[:-1])
  155. urls.append(url_info_dict)
  156. publish_time = parse_iso8601(self._html_search_regex(
  157. r'发布时间&nbsp;([^<>]+) ', page, 'publish time', default=None),
  158. delimiter=' ', timezone=datetime.timedelta(hours=8))
  159. description = self._html_search_meta('description', page, fatal=False)
  160. return {
  161. 'id': media_id,
  162. 'formats': urls,
  163. 'title': playurl['title'],
  164. 'thumbnail': playurl['pic'],
  165. 'description': description,
  166. 'timestamp': publish_time,
  167. }
  168. class LetvTvIE(InfoExtractor):
  169. _VALID_URL = r'http://www.letv.com/tv/(?P<id>\d+).html'
  170. _TESTS = [{
  171. 'url': 'http://www.letv.com/tv/46177.html',
  172. 'info_dict': {
  173. 'id': '46177',
  174. 'title': '美人天下',
  175. 'description': 'md5:395666ff41b44080396e59570dbac01c'
  176. },
  177. 'playlist_count': 35
  178. }]
  179. def _real_extract(self, url):
  180. playlist_id = self._match_id(url)
  181. page = self._download_webpage(url, playlist_id)
  182. media_urls = list(set(re.findall(
  183. r'http://www.letv.com/ptv/vplay/\d+.html', page)))
  184. entries = [self.url_result(media_url, ie='Letv')
  185. for media_url in media_urls]
  186. title = self._html_search_meta('keywords', page,
  187. fatal=False).split('')[0]
  188. description = self._html_search_meta('description', page, fatal=False)
  189. return self.playlist_result(entries, playlist_id, playlist_title=title,
  190. playlist_description=description)
  191. class LetvPlaylistIE(LetvTvIE):
  192. _VALID_URL = r'http://tv.letv.com/[a-z]+/(?P<id>[a-z]+)/index.s?html'
  193. _TESTS = [{
  194. 'url': 'http://tv.letv.com/izt/wuzetian/index.html',
  195. 'info_dict': {
  196. 'id': 'wuzetian',
  197. 'title': '武媚娘传奇',
  198. 'description': 'md5:e12499475ab3d50219e5bba00b3cb248'
  199. },
  200. # This playlist contains some extra videos other than the drama itself
  201. 'playlist_mincount': 96
  202. }, {
  203. 'url': 'http://tv.letv.com/pzt/lswjzzjc/index.shtml',
  204. 'info_dict': {
  205. 'id': 'lswjzzjc',
  206. # The title should be "劲舞青春", but I can't find a simple way to
  207. # determine the playlist title
  208. 'title': '乐视午间自制剧场',
  209. 'description': 'md5:b1eef244f45589a7b5b1af9ff25a4489'
  210. },
  211. 'playlist_mincount': 7
  212. }]
  213. class LetvCloudIE(InfoExtractor):
  214. IE_DESC = '乐视云'
  215. _VALID_URL = r'https?://yuntv\.letv\.com/bcloud.html\?.+'
  216. _TESTS = [{
  217. 'url': 'http://yuntv.letv.com/bcloud.html?uu=p7jnfw5hw9&vu=467623dedf',
  218. 'md5': '26450599afd64c513bc77030ad15db44',
  219. 'info_dict': {
  220. 'id': 'p7jnfw5hw9_467623dedf',
  221. 'ext': 'mp4',
  222. 'title': 'Video p7jnfw5hw9_467623dedf',
  223. },
  224. }, {
  225. 'url': 'http://yuntv.letv.com/bcloud.html?uu=p7jnfw5hw9&vu=ec93197892&pu=2c7cd40209&auto_play=1&gpcflag=1&width=640&height=360',
  226. 'md5': 'e03d9cc8d9c13191e1caf277e42dbd31',
  227. 'info_dict': {
  228. 'id': 'p7jnfw5hw9_ec93197892',
  229. 'ext': 'mp4',
  230. 'title': 'Video p7jnfw5hw9_ec93197892',
  231. },
  232. }, {
  233. 'url': 'http://yuntv.letv.com/bcloud.html?uu=p7jnfw5hw9&vu=187060b6fd',
  234. 'md5': 'cb988699a776b22d4a41b9d43acfb3ac',
  235. 'info_dict': {
  236. 'id': 'p7jnfw5hw9_187060b6fd',
  237. 'ext': 'mp4',
  238. 'title': 'Video p7jnfw5hw9_187060b6fd',
  239. },
  240. }]
  241. @staticmethod
  242. def sign_data(obj):
  243. if obj['cf'] == 'flash':
  244. salt = '2f9d6924b33a165a6d8b5d3d42f4f987'
  245. items = ['cf', 'format', 'ran', 'uu', 'ver', 'vu']
  246. elif obj['cf'] == 'html5':
  247. salt = 'fbeh5player12c43eccf2bec3300344'
  248. items = ['cf', 'ran', 'uu', 'bver', 'vu']
  249. input_data = ''.join([item + obj[item] for item in items]) + salt
  250. obj['sign'] = hashlib.md5(input_data.encode('utf-8')).hexdigest()
  251. def _get_formats(self, cf, uu, vu, media_id):
  252. def get_play_json(cf, timestamp):
  253. data = {
  254. 'cf': cf,
  255. 'ver': '2.2',
  256. 'bver': 'firefox44.0',
  257. 'format': 'json',
  258. 'uu': uu,
  259. 'vu': vu,
  260. 'ran': compat_str(timestamp),
  261. }
  262. self.sign_data(data)
  263. return self._download_json(
  264. 'http://api.letvcloud.com/gpc.php?' + compat_urllib_parse.urlencode(data),
  265. media_id, 'Downloading playJson data for type %s' % cf)
  266. play_json = get_play_json(cf, time.time())
  267. # The server time may be different from local time
  268. if play_json.get('code') == 10071:
  269. play_json = get_play_json(cf, play_json['timestamp'])
  270. if not play_json.get('data'):
  271. if play_json.get('message'):
  272. raise ExtractorError('Letv cloud said: %s' % play_json['message'], expected=True)
  273. elif play_json.get('code'):
  274. raise ExtractorError('Letv cloud returned error %d' % play_json['code'], expected=True)
  275. else:
  276. raise ExtractorError('Letv cloud returned an unknwon error')
  277. def b64decode(s):
  278. return base64.b64decode(s.encode('utf-8')).decode('utf-8')
  279. formats = []
  280. for media in play_json['data']['video_info']['media'].values():
  281. play_url = media['play_url']
  282. url = b64decode(play_url['main_url'])
  283. decoded_url = b64decode(url_basename(url))
  284. formats.append({
  285. 'url': url,
  286. 'ext': determine_ext(decoded_url),
  287. 'format_id': int_or_none(play_url.get('vtype')),
  288. 'format_note': str_or_none(play_url.get('definition')),
  289. 'width': int_or_none(play_url.get('vwidth')),
  290. 'height': int_or_none(play_url.get('vheight')),
  291. })
  292. return formats
  293. def _real_extract(self, url):
  294. uu_mobj = re.search('uu=([\w]+)', url)
  295. vu_mobj = re.search('vu=([\w]+)', url)
  296. if not uu_mobj or not vu_mobj:
  297. raise ExtractorError('Invalid URL: %s' % url, expected=True)
  298. uu = uu_mobj.group(1)
  299. vu = vu_mobj.group(1)
  300. media_id = uu + '_' + vu
  301. formats = self._get_formats('flash', uu, vu, media_id) + self._get_formats('html5', uu, vu, media_id)
  302. self._sort_formats(formats)
  303. return {
  304. 'id': media_id,
  305. 'title': 'Video %s' % media_id,
  306. 'formats': formats,
  307. }