letv.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import datetime
  4. import re
  5. import time
  6. import base64
  7. from .common import InfoExtractor
  8. from ..compat import (
  9. compat_urllib_parse,
  10. compat_ord,
  11. )
  12. from ..utils import (
  13. determine_ext,
  14. ExtractorError,
  15. parse_iso8601,
  16. sanitized_Request,
  17. int_or_none,
  18. str_or_none,
  19. encode_data_uri,
  20. )
  21. class LetvIE(InfoExtractor):
  22. IE_DESC = '乐视网'
  23. _VALID_URL = r'http://www\.letv\.com/ptv/vplay/(?P<id>\d+).html'
  24. _TESTS = [{
  25. 'url': 'http://www.letv.com/ptv/vplay/22005890.html',
  26. 'md5': 'edadcfe5406976f42f9f266057ee5e40',
  27. 'info_dict': {
  28. 'id': '22005890',
  29. 'ext': 'mp4',
  30. 'title': '第87届奥斯卡颁奖礼完美落幕 《鸟人》成最大赢家',
  31. 'description': 'md5:a9cb175fd753e2962176b7beca21a47c',
  32. },
  33. 'params': {
  34. 'hls_prefer_native': True,
  35. },
  36. }, {
  37. 'url': 'http://www.letv.com/ptv/vplay/1415246.html',
  38. 'info_dict': {
  39. 'id': '1415246',
  40. 'ext': 'mp4',
  41. 'title': '美人天下01',
  42. 'description': 'md5:f88573d9d7225ada1359eaf0dbf8bcda',
  43. },
  44. 'params': {
  45. 'hls_prefer_native': True,
  46. },
  47. }, {
  48. 'note': 'This video is available only in Mainland China, thus a proxy is needed',
  49. 'url': 'http://www.letv.com/ptv/vplay/1118082.html',
  50. 'md5': '2424c74948a62e5f31988438979c5ad1',
  51. 'info_dict': {
  52. 'id': '1118082',
  53. 'ext': 'mp4',
  54. 'title': '与龙共舞 完整版',
  55. 'description': 'md5:7506a5eeb1722bb9d4068f85024e3986',
  56. },
  57. 'params': {
  58. 'hls_prefer_native': True,
  59. },
  60. 'skip': 'Only available in China',
  61. }]
  62. @staticmethod
  63. def urshift(val, n):
  64. return val >> n if val >= 0 else (val + 0x100000000) >> n
  65. # ror() and calc_time_key() are reversed from a embedded swf file in KLetvPlayer.swf
  66. def ror(self, param1, param2):
  67. _loc3_ = 0
  68. while _loc3_ < param2:
  69. param1 = self.urshift(param1, 1) + ((param1 & 1) << 31)
  70. _loc3_ += 1
  71. return param1
  72. def calc_time_key(self, param1):
  73. _loc2_ = 773625421
  74. _loc3_ = self.ror(param1, _loc2_ % 13)
  75. _loc3_ = _loc3_ ^ _loc2_
  76. _loc3_ = self.ror(_loc3_, _loc2_ % 17)
  77. return _loc3_
  78. # see M3U8Encryption class in KLetvPlayer.swf
  79. @staticmethod
  80. def decrypt_m3u8(encrypted_data):
  81. if encrypted_data[:5].decode('utf-8').lower() != 'vc_01':
  82. return encrypted_data
  83. encrypted_data = encrypted_data[5:]
  84. _loc4_ = bytearray()
  85. while encrypted_data:
  86. b = compat_ord(encrypted_data[0])
  87. _loc4_.extend([b // 16, b & 0x0f])
  88. encrypted_data = encrypted_data[1:]
  89. idx = len(_loc4_) - 11
  90. _loc4_ = _loc4_[idx:] + _loc4_[:idx]
  91. _loc7_ = bytearray()
  92. while _loc4_:
  93. _loc7_.append(_loc4_[0] * 16 + _loc4_[1])
  94. _loc4_ = _loc4_[2:]
  95. return bytes(_loc7_)
  96. def _real_extract(self, url):
  97. media_id = self._match_id(url)
  98. page = self._download_webpage(url, media_id)
  99. params = {
  100. 'id': media_id,
  101. 'platid': 1,
  102. 'splatid': 101,
  103. 'format': 1,
  104. 'tkey': self.calc_time_key(int(time.time())),
  105. 'domain': 'www.letv.com'
  106. }
  107. play_json_req = sanitized_Request(
  108. 'http://api.letv.com/mms/out/video/playJson?' + compat_urllib_parse.urlencode(params)
  109. )
  110. cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
  111. if cn_verification_proxy:
  112. play_json_req.add_header('Ytdl-request-proxy', cn_verification_proxy)
  113. play_json = self._download_json(
  114. play_json_req,
  115. media_id, 'Downloading playJson data')
  116. # Check for errors
  117. playstatus = play_json['playstatus']
  118. if playstatus['status'] == 0:
  119. flag = playstatus['flag']
  120. if flag == 1:
  121. msg = 'Country %s auth error' % playstatus['country']
  122. else:
  123. msg = 'Generic error. flag = %d' % flag
  124. raise ExtractorError(msg, expected=True)
  125. playurl = play_json['playurl']
  126. formats = ['350', '1000', '1300', '720p', '1080p']
  127. dispatch = playurl['dispatch']
  128. urls = []
  129. for format_id in formats:
  130. if format_id in dispatch:
  131. media_url = playurl['domain'][0] + dispatch[format_id][0]
  132. media_url += '&' + compat_urllib_parse.urlencode({
  133. 'm3v': 1,
  134. 'format': 1,
  135. 'expect': 3,
  136. 'rateid': format_id,
  137. })
  138. nodes_data = self._download_json(
  139. media_url, media_id,
  140. 'Download JSON metadata for format %s' % format_id)
  141. req = self._request_webpage(
  142. nodes_data['nodelist'][0]['location'], media_id,
  143. note='Downloading m3u8 information for format %s' % format_id)
  144. m3u8_data = self.decrypt_m3u8(req.read())
  145. url_info_dict = {
  146. 'url': encode_data_uri(m3u8_data, 'application/vnd.apple.mpegurl'),
  147. 'ext': determine_ext(dispatch[format_id][1]),
  148. 'format_id': format_id,
  149. 'protocol': 'm3u8',
  150. }
  151. if format_id[-1:] == 'p':
  152. url_info_dict['height'] = int_or_none(format_id[:-1])
  153. urls.append(url_info_dict)
  154. publish_time = parse_iso8601(self._html_search_regex(
  155. r'发布时间&nbsp;([^<>]+) ', page, 'publish time', default=None),
  156. delimiter=' ', timezone=datetime.timedelta(hours=8))
  157. description = self._html_search_meta('description', page, fatal=False)
  158. return {
  159. 'id': media_id,
  160. 'formats': urls,
  161. 'title': playurl['title'],
  162. 'thumbnail': playurl['pic'],
  163. 'description': description,
  164. 'timestamp': publish_time,
  165. }
  166. class LetvTvIE(InfoExtractor):
  167. _VALID_URL = r'http://www.letv.com/tv/(?P<id>\d+).html'
  168. _TESTS = [{
  169. 'url': 'http://www.letv.com/tv/46177.html',
  170. 'info_dict': {
  171. 'id': '46177',
  172. 'title': '美人天下',
  173. 'description': 'md5:395666ff41b44080396e59570dbac01c'
  174. },
  175. 'playlist_count': 35
  176. }]
  177. def _real_extract(self, url):
  178. playlist_id = self._match_id(url)
  179. page = self._download_webpage(url, playlist_id)
  180. media_urls = list(set(re.findall(
  181. r'http://www.letv.com/ptv/vplay/\d+.html', page)))
  182. entries = [self.url_result(media_url, ie='Letv')
  183. for media_url in media_urls]
  184. title = self._html_search_meta('keywords', page,
  185. fatal=False).split(',')[0]
  186. description = self._html_search_meta('description', page, fatal=False)
  187. return self.playlist_result(entries, playlist_id, playlist_title=title,
  188. playlist_description=description)
  189. class LetvPlaylistIE(LetvTvIE):
  190. _VALID_URL = r'http://tv.letv.com/[a-z]+/(?P<id>[a-z]+)/index.s?html'
  191. _TESTS = [{
  192. 'url': 'http://tv.letv.com/izt/wuzetian/index.html',
  193. 'info_dict': {
  194. 'id': 'wuzetian',
  195. 'title': '武媚娘传奇',
  196. 'description': 'md5:e12499475ab3d50219e5bba00b3cb248'
  197. },
  198. # This playlist contains some extra videos other than the drama itself
  199. 'playlist_mincount': 96
  200. }, {
  201. 'url': 'http://tv.letv.com/pzt/lswjzzjc/index.shtml',
  202. 'info_dict': {
  203. 'id': 'lswjzzjc',
  204. # The title should be "劲舞青春", but I can't find a simple way to
  205. # determine the playlist title
  206. 'title': '乐视午间自制剧场',
  207. 'description': 'md5:b1eef244f45589a7b5b1af9ff25a4489'
  208. },
  209. 'playlist_mincount': 7
  210. }]
  211. class LetvCloudIE(InfoExtractor):
  212. IE_DESC = '乐视云'
  213. _VALID_URL = r'http://yuntv\.letv\.com/bcloud.html\?.*$'
  214. _TESTS = [{
  215. 'url': 'http://yuntv.letv.com/bcloud.html?uu=p7jnfw5hw9&vu=467623dedf',
  216. 'md5': '26450599afd64c513bc77030ad15db44',
  217. 'info_dict': {
  218. 'id': 'p7jnfw5hw9_467623dedf',
  219. 'ext': 'mp4',
  220. 'title': 'p7jnfw5hw9_467623dedf',
  221. },
  222. }, {
  223. 'url': 'http://yuntv.letv.com/bcloud.html?uu=p7jnfw5hw9&vu=ec93197892&pu=2c7cd40209&auto_play=1&gpcflag=1&width=640&height=360',
  224. 'info_dict': {
  225. 'id': 'p7jnfw5hw9_ec93197892',
  226. 'ext': 'mp4',
  227. 'title': 'p7jnfw5hw9_ec93197892',
  228. },
  229. }, {
  230. 'url': 'http://yuntv.letv.com/bcloud.html?uu=p7jnfw5hw9&vu=187060b6fd',
  231. 'info_dict': {
  232. 'id': 'p7jnfw5hw9_187060b6fd',
  233. 'ext': 'mp4',
  234. 'title': 'p7jnfw5hw9_187060b6fd',
  235. },
  236. }]
  237. def _real_extract(self, url):
  238. uu = re.search('uu=([\w]+)', url).group(1)
  239. vu = re.search('vu=([\w]+)', url).group(1)
  240. media_id = uu + '_' + vu
  241. play_json_req = sanitized_Request(
  242. 'http://api.letvcloud.com/gpc.php?cf=html5&sign=signxxxxx&ver=2.2&format=json&' +
  243. "uu=" + uu + "&vu=" + vu)
  244. play_json = self._download_json(play_json_req, media_id, 'Downloading playJson data')
  245. formats = [{
  246. 'url': base64.b64decode(media['play_url']['main_url'].encode('utf-8')).decode("utf-8"),
  247. 'ext': 'mp4',
  248. 'format_id': int_or_none(media.get('play_url', {}).get('vtype')),
  249. 'format_note': str_or_none(media.get('play_url', {}).get('definition')),
  250. 'width': int_or_none(media.get('play_url', {}).get('vwidth')),
  251. 'height': int_or_none(media.get('play_url', {}).get('vheight')),
  252. } for media in play_json['data']['video_info']['media'].values()]
  253. self._sort_formats(formats)
  254. return {
  255. 'id': media_id,
  256. 'title': media_id,
  257. 'formats': formats,
  258. }