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.

205 lines
6.9 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import datetime
  4. import re
  5. import time
  6. from .common import InfoExtractor
  7. from ..compat import (
  8. compat_urllib_parse,
  9. compat_urllib_request,
  10. compat_urlparse,
  11. )
  12. from ..utils import (
  13. determine_ext,
  14. ExtractorError,
  15. parse_iso8601,
  16. )
  17. class LetvIE(InfoExtractor):
  18. _VALID_URL = r'http://www\.letv\.com/ptv/vplay/(?P<id>\d+).html'
  19. _TESTS = [{
  20. 'url': 'http://www.letv.com/ptv/vplay/22005890.html',
  21. 'md5': 'cab23bd68d5a8db9be31c9a222c1e8df',
  22. 'info_dict': {
  23. 'id': '22005890',
  24. 'ext': 'mp4',
  25. 'title': '第87届奥斯卡颁奖礼完美落幕 《鸟人》成最大赢家',
  26. 'timestamp': 1424747397,
  27. 'upload_date': '20150224',
  28. 'description': 'md5:a9cb175fd753e2962176b7beca21a47c',
  29. }
  30. }, {
  31. 'url': 'http://www.letv.com/ptv/vplay/1415246.html',
  32. 'info_dict': {
  33. 'id': '1415246',
  34. 'ext': 'mp4',
  35. 'title': '美人天下01',
  36. 'description': 'md5:f88573d9d7225ada1359eaf0dbf8bcda',
  37. },
  38. }, {
  39. 'note': 'This video is available only in Mainland China, thus a proxy is needed',
  40. 'url': 'http://www.letv.com/ptv/vplay/1118082.html',
  41. 'md5': 'f80936fbe20fb2f58648e81386ff7927',
  42. 'info_dict': {
  43. 'id': '1118082',
  44. 'ext': 'mp4',
  45. 'title': '与龙共舞 完整版',
  46. 'description': 'md5:7506a5eeb1722bb9d4068f85024e3986',
  47. },
  48. 'skip': 'Only available in China',
  49. }]
  50. @staticmethod
  51. def urshift(val, n):
  52. return val >> n if val >= 0 else (val + 0x100000000) >> n
  53. # ror() and calc_time_key() are reversed from a embedded swf file in KLetvPlayer.swf
  54. def ror(self, param1, param2):
  55. _loc3_ = 0
  56. while _loc3_ < param2:
  57. param1 = self.urshift(param1, 1) + ((param1 & 1) << 31)
  58. _loc3_ += 1
  59. return param1
  60. def calc_time_key(self, param1):
  61. _loc2_ = 773625421
  62. _loc3_ = self.ror(param1, _loc2_ % 13)
  63. _loc3_ = _loc3_ ^ _loc2_
  64. _loc3_ = self.ror(_loc3_, _loc2_ % 17)
  65. return _loc3_
  66. def _real_extract(self, url):
  67. media_id = self._match_id(url)
  68. page = self._download_webpage(url, media_id)
  69. params = {
  70. 'id': media_id,
  71. 'platid': 1,
  72. 'splatid': 101,
  73. 'format': 1,
  74. 'tkey': self.calc_time_key(int(time.time())),
  75. 'domain': 'www.letv.com'
  76. }
  77. play_json_req = compat_urllib_request.Request(
  78. 'http://api.letv.com/mms/out/video/playJson?' + compat_urllib_parse.urlencode(params)
  79. )
  80. cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
  81. if cn_verification_proxy:
  82. play_json_req.add_header('Ytdl-request-proxy', cn_verification_proxy)
  83. play_json = self._download_json(
  84. play_json_req,
  85. media_id, 'Downloading playJson data')
  86. # Check for errors
  87. playstatus = play_json['playstatus']
  88. if playstatus['status'] == 0:
  89. flag = playstatus['flag']
  90. if flag == 1:
  91. msg = 'Country %s auth error' % playstatus['country']
  92. else:
  93. msg = 'Generic error. flag = %d' % flag
  94. raise ExtractorError(msg, expected=True)
  95. playurl = play_json['playurl']
  96. formats = ['350', '1000', '1300', '720p', '1080p']
  97. dispatch = playurl['dispatch']
  98. urls = []
  99. for format_id in formats:
  100. if format_id in dispatch:
  101. media_url = playurl['domain'][0] + dispatch[format_id][0]
  102. # Mimic what flvxz.com do
  103. url_parts = list(compat_urlparse.urlparse(media_url))
  104. qs = dict(compat_urlparse.parse_qs(url_parts[4]))
  105. qs.update({
  106. 'platid': '14',
  107. 'splatid': '1401',
  108. 'tss': 'no',
  109. 'retry': 1
  110. })
  111. url_parts[4] = compat_urllib_parse.urlencode(qs)
  112. media_url = compat_urlparse.urlunparse(url_parts)
  113. url_info_dict = {
  114. 'url': media_url,
  115. 'ext': determine_ext(dispatch[format_id][1]),
  116. 'format_id': format_id,
  117. }
  118. if format_id[-1:] == 'p':
  119. url_info_dict['height'] = format_id[:-1]
  120. urls.append(url_info_dict)
  121. publish_time = parse_iso8601(self._html_search_regex(
  122. r'发布时间&nbsp;([^<>]+) ', page, 'publish time', default=None),
  123. delimiter=' ', timezone=datetime.timedelta(hours=8))
  124. description = self._html_search_meta('description', page, fatal=False)
  125. return {
  126. 'id': media_id,
  127. 'formats': urls,
  128. 'title': playurl['title'],
  129. 'thumbnail': playurl['pic'],
  130. 'description': description,
  131. 'timestamp': publish_time,
  132. }
  133. class LetvTvIE(InfoExtractor):
  134. _VALID_URL = r'http://www.letv.com/tv/(?P<id>\d+).html'
  135. _TESTS = [{
  136. 'url': 'http://www.letv.com/tv/46177.html',
  137. 'info_dict': {
  138. 'id': '46177',
  139. 'title': '美人天下',
  140. 'description': 'md5:395666ff41b44080396e59570dbac01c'
  141. },
  142. 'playlist_count': 35
  143. }]
  144. def _real_extract(self, url):
  145. playlist_id = self._match_id(url)
  146. page = self._download_webpage(url, playlist_id)
  147. media_urls = list(set(re.findall(
  148. r'http://www.letv.com/ptv/vplay/\d+.html', page)))
  149. entries = [self.url_result(media_url, ie='Letv')
  150. for media_url in media_urls]
  151. title = self._html_search_meta('keywords', page,
  152. fatal=False).split('')[0]
  153. description = self._html_search_meta('description', page, fatal=False)
  154. return self.playlist_result(entries, playlist_id, playlist_title=title,
  155. playlist_description=description)
  156. class LetvPlaylistIE(LetvTvIE):
  157. _VALID_URL = r'http://tv.letv.com/[a-z]+/(?P<id>[a-z]+)/index.s?html'
  158. _TESTS = [{
  159. 'url': 'http://tv.letv.com/izt/wuzetian/index.html',
  160. 'info_dict': {
  161. 'id': 'wuzetian',
  162. 'title': '武媚娘传奇',
  163. 'description': 'md5:e12499475ab3d50219e5bba00b3cb248'
  164. },
  165. # This playlist contains some extra videos other than the drama itself
  166. 'playlist_mincount': 96
  167. }, {
  168. 'url': 'http://tv.letv.com/pzt/lswjzzjc/index.shtml',
  169. 'info_dict': {
  170. 'id': 'lswjzzjc',
  171. # The title should be "劲舞青春", but I can't find a simple way to
  172. # determine the playlist title
  173. 'title': '乐视午间自制剧场',
  174. 'description': 'md5:b1eef244f45589a7b5b1af9ff25a4489'
  175. },
  176. 'playlist_mincount': 7
  177. }]