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.

178 lines
6.6 KiB

10 years ago
10 years ago
10 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_str
  5. from ..utils import (
  6. ExtractorError,
  7. int_or_none,
  8. InAdvancePagedList,
  9. float_or_none,
  10. unescapeHTML,
  11. )
  12. class TudouIE(InfoExtractor):
  13. IE_NAME = 'tudou'
  14. _VALID_URL = r'https?://(?:www\.)?tudou\.com/(?:(?:programs|wlplay)/view|(?:listplay|albumplay)/[\w-]{11})/(?P<id>[\w-]{11})'
  15. _TESTS = [{
  16. 'url': 'http://www.tudou.com/listplay/zzdE77v6Mmo/2xN2duXMxmw.html',
  17. 'md5': '140a49ed444bd22f93330985d8475fcb',
  18. 'info_dict': {
  19. 'id': '159448201',
  20. 'ext': 'f4v',
  21. 'title': '卡马乔国足开大脚长传冲吊集锦',
  22. 'thumbnail': 're:^https?://.*\.jpg$',
  23. 'timestamp': 1372113489000,
  24. 'description': '卡马乔卡家军,开大脚先进战术不完全集锦!',
  25. 'duration': 289.04,
  26. 'view_count': int,
  27. 'filesize': int,
  28. }
  29. }, {
  30. 'url': 'http://www.tudou.com/programs/view/ajX3gyhL0pc/',
  31. 'info_dict': {
  32. 'id': '117049447',
  33. 'ext': 'f4v',
  34. 'title': 'La Sylphide-Bolshoi-Ekaterina Krysanova & Vyacheslav Lopatin 2012',
  35. 'thumbnail': 're:^https?://.*\.jpg$',
  36. 'timestamp': 1349207518000,
  37. 'description': 'md5:294612423894260f2dcd5c6c04fe248b',
  38. 'duration': 5478.33,
  39. 'view_count': int,
  40. 'filesize': int,
  41. }
  42. }]
  43. _PLAYER_URL = 'http://js.tudouui.com/bin/lingtong/PortalPlayer_177.swf'
  44. # Translated from tudou/tools/TVCHelper.as in PortalPlayer_193.swf
  45. # 0001, 0002 and 4001 are not included as they indicate temporary issues
  46. TVC_ERRORS = {
  47. '0003': 'The video is deleted or does not exist',
  48. '1001': 'This video is unavailable due to licensing issues',
  49. '1002': 'This video is unavailable as it\'s under review',
  50. '1003': 'This video is unavailable as it\'s under review',
  51. '3001': 'Password required',
  52. '5001': 'This video is available in Mainland China only due to licensing issues',
  53. '7001': 'This video is unavailable',
  54. '8001': 'This video is unavailable due to licensing issues',
  55. }
  56. def _url_for_id(self, video_id, quality=None):
  57. info_url = 'http://v2.tudou.com/f?id=' + compat_str(video_id)
  58. if quality:
  59. info_url += '&hd' + quality
  60. xml_data = self._download_xml(info_url, video_id, 'Opening the info XML page')
  61. final_url = xml_data.text
  62. return final_url
  63. def _real_extract(self, url):
  64. video_id = self._match_id(url)
  65. item_data = self._download_json(
  66. 'http://www.tudou.com/tvp/getItemInfo.action?ic=%s' % video_id, video_id)
  67. youku_vcode = item_data.get('vcode')
  68. if youku_vcode:
  69. return self.url_result('youku:' + youku_vcode, ie='Youku')
  70. if not item_data.get('itemSegs'):
  71. tvc_code = item_data.get('tvcCode')
  72. if tvc_code:
  73. err_msg = self.TVC_ERRORS.get(tvc_code)
  74. if err_msg:
  75. raise ExtractorError('Tudou said: %s' % err_msg, expected=True)
  76. raise ExtractorError('Unexpected error %s returned from Tudou' % tvc_code)
  77. raise ExtractorError('Unxpected error returned from Tudou')
  78. title = unescapeHTML(item_data['kw'])
  79. description = item_data.get('desc')
  80. thumbnail_url = item_data.get('pic')
  81. view_count = int_or_none(item_data.get('playTimes'))
  82. timestamp = int_or_none(item_data.get('pt'))
  83. segments = self._parse_json(item_data['itemSegs'], video_id)
  84. # It looks like the keys are the arguments that have to be passed as
  85. # the hd field in the request url, we pick the higher
  86. # Also, filter non-number qualities (see issue #3643).
  87. quality = sorted(filter(lambda k: k.isdigit(), segments.keys()),
  88. key=lambda k: int(k))[-1]
  89. parts = segments[quality]
  90. len_parts = len(parts)
  91. if len_parts > 1:
  92. self.to_screen('%s: found %s parts' % (video_id, len_parts))
  93. def part_func(partnum):
  94. part = parts[partnum]
  95. part_id = part['k']
  96. final_url = self._url_for_id(part_id, quality)
  97. ext = (final_url.split('?')[0]).split('.')[-1]
  98. return [{
  99. 'id': '%s' % part_id,
  100. 'url': final_url,
  101. 'ext': ext,
  102. 'title': title,
  103. 'thumbnail': thumbnail_url,
  104. 'description': description,
  105. 'view_count': view_count,
  106. 'timestamp': timestamp,
  107. 'duration': float_or_none(part.get('seconds'), 1000),
  108. 'filesize': int_or_none(part.get('size')),
  109. 'http_headers': {
  110. 'Referer': self._PLAYER_URL,
  111. },
  112. }]
  113. entries = InAdvancePagedList(part_func, len_parts, 1)
  114. return {
  115. '_type': 'multi_video',
  116. 'entries': entries,
  117. 'id': video_id,
  118. 'title': title,
  119. }
  120. class TudouPlaylistIE(InfoExtractor):
  121. IE_NAME = 'tudou:playlist'
  122. _VALID_URL = r'https?://(?:www\.)?tudou\.com/listplay/(?P<id>[\w-]{11})\.html'
  123. _TESTS = [{
  124. 'url': 'http://www.tudou.com/listplay/zzdE77v6Mmo.html',
  125. 'info_dict': {
  126. 'id': 'zzdE77v6Mmo',
  127. },
  128. 'playlist_mincount': 209,
  129. }]
  130. def _real_extract(self, url):
  131. playlist_id = self._match_id(url)
  132. playlist_data = self._download_json(
  133. 'http://www.tudou.com/tvp/plist.action?lcode=%s' % playlist_id, playlist_id)
  134. entries = [self.url_result(
  135. 'http://www.tudou.com/programs/view/%s' % item['icode'],
  136. 'Tudou', item['icode'],
  137. item['kw']) for item in playlist_data['items']]
  138. return self.playlist_result(entries, playlist_id)
  139. class TudouAlbumIE(InfoExtractor):
  140. IE_NAME = 'tudou:album'
  141. _VALID_URL = r'https?://(?:www\.)?tudou\.com/album(?:cover|play)/(?P<id>[\w-]{11})'
  142. _TESTS = [{
  143. 'url': 'http://www.tudou.com/albumplay/v5qckFJvNJg.html',
  144. 'info_dict': {
  145. 'id': 'v5qckFJvNJg',
  146. },
  147. 'playlist_mincount': 45,
  148. }]
  149. def _real_extract(self, url):
  150. album_id = self._match_id(url)
  151. album_data = self._download_json(
  152. 'http://www.tudou.com/tvp/alist.action?acode=%s' % album_id, album_id)
  153. entries = [self.url_result(
  154. 'http://www.tudou.com/programs/view/%s' % item['icode'],
  155. 'Tudou', item['icode'],
  156. item['kw']) for item in album_data['items']]
  157. return self.playlist_result(entries, album_id)