letv.py 6.2 KB

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