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.

181 lines
6.8 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': r'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': r'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. error = xml_data.attrib.get('error')
  62. if error is not None:
  63. raise ExtractorError('Tudou said: %s' % error, expected=True)
  64. final_url = xml_data.text
  65. return final_url
  66. def _real_extract(self, url):
  67. video_id = self._match_id(url)
  68. item_data = self._download_json(
  69. 'http://www.tudou.com/tvp/getItemInfo.action?ic=%s' % video_id, video_id)
  70. youku_vcode = item_data.get('vcode')
  71. if youku_vcode:
  72. return self.url_result('youku:' + youku_vcode, ie='Youku')
  73. if not item_data.get('itemSegs'):
  74. tvc_code = item_data.get('tvcCode')
  75. if tvc_code:
  76. err_msg = self.TVC_ERRORS.get(tvc_code)
  77. if err_msg:
  78. raise ExtractorError('Tudou said: %s' % err_msg, expected=True)
  79. raise ExtractorError('Unexpected error %s returned from Tudou' % tvc_code)
  80. raise ExtractorError('Unxpected error returned from Tudou')
  81. title = unescapeHTML(item_data['kw'])
  82. description = item_data.get('desc')
  83. thumbnail_url = item_data.get('pic')
  84. view_count = int_or_none(item_data.get('playTimes'))
  85. timestamp = int_or_none(item_data.get('pt'))
  86. segments = self._parse_json(item_data['itemSegs'], video_id)
  87. # It looks like the keys are the arguments that have to be passed as
  88. # the hd field in the request url, we pick the higher
  89. # Also, filter non-number qualities (see issue #3643).
  90. quality = sorted(filter(lambda k: k.isdigit(), segments.keys()),
  91. key=lambda k: int(k))[-1]
  92. parts = segments[quality]
  93. len_parts = len(parts)
  94. if len_parts > 1:
  95. self.to_screen('%s: found %s parts' % (video_id, len_parts))
  96. def part_func(partnum):
  97. part = parts[partnum]
  98. part_id = part['k']
  99. final_url = self._url_for_id(part_id, quality)
  100. ext = (final_url.split('?')[0]).split('.')[-1]
  101. return [{
  102. 'id': '%s' % part_id,
  103. 'url': final_url,
  104. 'ext': ext,
  105. 'title': title,
  106. 'thumbnail': thumbnail_url,
  107. 'description': description,
  108. 'view_count': view_count,
  109. 'timestamp': timestamp,
  110. 'duration': float_or_none(part.get('seconds'), 1000),
  111. 'filesize': int_or_none(part.get('size')),
  112. 'http_headers': {
  113. 'Referer': self._PLAYER_URL,
  114. },
  115. }]
  116. entries = InAdvancePagedList(part_func, len_parts, 1)
  117. return {
  118. '_type': 'multi_video',
  119. 'entries': entries,
  120. 'id': video_id,
  121. 'title': title,
  122. }
  123. class TudouPlaylistIE(InfoExtractor):
  124. IE_NAME = 'tudou:playlist'
  125. _VALID_URL = r'https?://(?:www\.)?tudou\.com/listplay/(?P<id>[\w-]{11})\.html'
  126. _TESTS = [{
  127. 'url': 'http://www.tudou.com/listplay/zzdE77v6Mmo.html',
  128. 'info_dict': {
  129. 'id': 'zzdE77v6Mmo',
  130. },
  131. 'playlist_mincount': 209,
  132. }]
  133. def _real_extract(self, url):
  134. playlist_id = self._match_id(url)
  135. playlist_data = self._download_json(
  136. 'http://www.tudou.com/tvp/plist.action?lcode=%s' % playlist_id, playlist_id)
  137. entries = [self.url_result(
  138. 'http://www.tudou.com/programs/view/%s' % item['icode'],
  139. 'Tudou', item['icode'],
  140. item['kw']) for item in playlist_data['items']]
  141. return self.playlist_result(entries, playlist_id)
  142. class TudouAlbumIE(InfoExtractor):
  143. IE_NAME = 'tudou:album'
  144. _VALID_URL = r'https?://(?:www\.)?tudou\.com/album(?:cover|play)/(?P<id>[\w-]{11})'
  145. _TESTS = [{
  146. 'url': 'http://www.tudou.com/albumplay/v5qckFJvNJg.html',
  147. 'info_dict': {
  148. 'id': 'v5qckFJvNJg',
  149. },
  150. 'playlist_mincount': 45,
  151. }]
  152. def _real_extract(self, url):
  153. album_id = self._match_id(url)
  154. album_data = self._download_json(
  155. 'http://www.tudou.com/tvp/alist.action?acode=%s' % album_id, album_id)
  156. entries = [self.url_result(
  157. 'http://www.tudou.com/programs/view/%s' % item['icode'],
  158. 'Tudou', item['icode'],
  159. item['kw']) for item in album_data['items']]
  160. return self.playlist_result(entries, album_id)