letv.py 7.0 KB

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