letv.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import os.path
  4. import time
  5. import datetime
  6. from .common import InfoExtractor
  7. from ..compat import (compat_urlparse, compat_urllib_parse)
  8. from ..utils import (ExtractorError, parse_iso8601)
  9. class LetvIE(InfoExtractor):
  10. _VALID_URL = r'http://www.letv.com/ptv/vplay/(?P<id>\d+).html'
  11. _TESTS = [{
  12. 'url': 'http://www.letv.com/ptv/vplay/22005890.html',
  13. 'md5': 'cab23bd68d5a8db9be31c9a222c1e8df',
  14. 'info_dict': {
  15. 'id': '22005890',
  16. 'ext': 'mp4',
  17. 'title': '第87届奥斯卡颁奖礼完美落幕 《鸟人》成最大赢家',
  18. 'timestamp': 1424747397,
  19. 'upload_date': '20150224',
  20. }
  21. }, {
  22. 'url': 'http://www.letv.com/ptv/vplay/1118082.html',
  23. 'info_dict': {
  24. 'id': '1118082',
  25. 'ext': 'mp4',
  26. }
  27. }]
  28. @staticmethod
  29. def urshift(val, n):
  30. return val >> n if val >= 0 else (val + 0x100000000) >> n
  31. # ror() and calcTimeKey() are reversed from a embedded swf file in KLetvPlayer.swf
  32. def ror(self, param1, param2):
  33. _loc3_ = 0
  34. while _loc3_ < param2:
  35. param1 = self.urshift(param1, 1) + ((param1 & 1) << 31)
  36. _loc3_ += 1
  37. return param1
  38. def calcTimeKey(self, param1):
  39. _loc2_ = 773625421
  40. _loc3_ = self.ror(param1, _loc2_ % 13)
  41. _loc3_ = _loc3_ ^ _loc2_
  42. _loc3_ = self.ror(_loc3_, _loc2_ % 17)
  43. return _loc3_
  44. def _real_extract(self, url):
  45. media_id = self._match_id(url)
  46. page = self._download_webpage(url, media_id)
  47. params = {
  48. 'id': media_id,
  49. 'platid': 1,
  50. 'splatid': 101,
  51. 'format': 1,
  52. 'tkey': self.calcTimeKey(int(time.time())),
  53. 'domain': 'www.letv.com'
  54. }
  55. play_json = self._download_json(
  56. 'http://api.letv.com/mms/out/video/playJson?' + compat_urllib_parse.urlencode(params),
  57. media_id, 'playJson data')
  58. # Check for errors
  59. playstatus = play_json['playstatus']
  60. if playstatus['status'] == 0:
  61. flag = playstatus['flag']
  62. if flag == 1:
  63. msg = 'Country %s auth error' % playstatus['country']
  64. else:
  65. msg = 'Generic error. flag = %d' % flag
  66. raise ExtractorError(msg, expected=True)
  67. playurl = play_json['playurl']
  68. formats = ['350', '1000', '1300', '720p', '1080p']
  69. dispatch = playurl['dispatch']
  70. urls = []
  71. for format_id in formats:
  72. if format_id in dispatch:
  73. media_url = playurl['domain'][0] + dispatch[format_id][0]
  74. # Mimic what flvxz.com do
  75. url_parts = list(compat_urlparse.urlparse(media_url))
  76. qs = dict(compat_urlparse.parse_qs(url_parts[4]))
  77. qs.update({
  78. 'platid': '14',
  79. 'splatid': '1401',
  80. 'tss': 'no',
  81. 'retry': 1
  82. })
  83. url_parts[4] = compat_urllib_parse.urlencode(qs)
  84. media_url = compat_urlparse.urlunparse(url_parts)
  85. url_info_dict = {
  86. 'url': media_url,
  87. 'ext': os.path.splitext(dispatch[format_id][1])[1][1:]
  88. }
  89. if format_id[-1:] == 'p':
  90. url_info_dict['height'] = format_id[:-1]
  91. urls.append(url_info_dict)
  92. publish_time = parse_iso8601(self._html_search_regex(
  93. r'发布时间&nbsp;([^<>]+) ', page, 'publish time', fatal=False),
  94. delimiter=' ', timezone=datetime.timedelta(hours=8))
  95. return {
  96. 'id': media_id,
  97. 'formats': urls,
  98. 'title': playurl['title'],
  99. 'thumbnail': playurl['pic'],
  100. 'timestamp': publish_time,
  101. }