letv.py 6.2 KB

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