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.

155 lines
5.5 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. int_or_none,
  7. InAdvancePagedList,
  8. float_or_none,
  9. unescapeHTML,
  10. )
  11. class TudouIE(InfoExtractor):
  12. IE_NAME = 'tudou'
  13. _VALID_URL = r'https?://(?:www\.)?tudou\.com/(?:(?:programs|wlplay)/view|(?:listplay|albumplay)/[\w-]{11})/(?P<id>[\w-]{11})'
  14. _TESTS = [{
  15. 'url': 'http://www.tudou.com/listplay/zzdE77v6Mmo/2xN2duXMxmw.html',
  16. 'md5': '140a49ed444bd22f93330985d8475fcb',
  17. 'info_dict': {
  18. 'id': '159448201',
  19. 'ext': 'f4v',
  20. 'title': '卡马乔国足开大脚长传冲吊集锦',
  21. 'thumbnail': 're:^https?://.*\.jpg$',
  22. 'timestamp': 1372113489000,
  23. 'description': '卡马乔卡家军,开大脚先进战术不完全集锦!',
  24. 'duration': 289.04,
  25. 'view_count': int,
  26. 'filesize': int,
  27. }
  28. }, {
  29. 'url': 'http://www.tudou.com/programs/view/ajX3gyhL0pc/',
  30. 'info_dict': {
  31. 'id': '117049447',
  32. 'ext': 'f4v',
  33. 'title': 'La Sylphide-Bolshoi-Ekaterina Krysanova & Vyacheslav Lopatin 2012',
  34. 'thumbnail': 're:^https?://.*\.jpg$',
  35. 'timestamp': 1349207518000,
  36. 'description': 'md5:294612423894260f2dcd5c6c04fe248b',
  37. 'duration': 5478.33,
  38. 'view_count': int,
  39. 'filesize': int,
  40. }
  41. }]
  42. _PLAYER_URL = 'http://js.tudouui.com/bin/lingtong/PortalPlayer_177.swf'
  43. def _url_for_id(self, video_id, quality=None):
  44. info_url = 'http://v2.tudou.com/f?id=' + compat_str(video_id)
  45. if quality:
  46. info_url += '&hd' + quality
  47. xml_data = self._download_xml(info_url, video_id, 'Opening the info XML page')
  48. final_url = xml_data.text
  49. return final_url
  50. def _real_extract(self, url):
  51. video_id = self._match_id(url)
  52. item_data = self._download_json(
  53. 'http://www.tudou.com/tvp/getItemInfo.action?ic=%s' % video_id, video_id)
  54. youku_vcode = item_data.get('vcode')
  55. if youku_vcode:
  56. return self.url_result('youku:' + youku_vcode, ie='Youku')
  57. title = unescapeHTML(item_data['kw'])
  58. description = item_data.get('desc')
  59. thumbnail_url = item_data.get('pic')
  60. view_count = int_or_none(item_data.get('playTimes'))
  61. timestamp = int_or_none(item_data.get('pt'))
  62. segments = self._parse_json(item_data['itemSegs'], video_id)
  63. # It looks like the keys are the arguments that have to be passed as
  64. # the hd field in the request url, we pick the higher
  65. # Also, filter non-number qualities (see issue #3643).
  66. quality = sorted(filter(lambda k: k.isdigit(), segments.keys()),
  67. key=lambda k: int(k))[-1]
  68. parts = segments[quality]
  69. len_parts = len(parts)
  70. if len_parts > 1:
  71. self.to_screen('%s: found %s parts' % (video_id, len_parts))
  72. def part_func(partnum):
  73. part = parts[partnum]
  74. part_id = part['k']
  75. final_url = self._url_for_id(part_id, quality)
  76. ext = (final_url.split('?')[0]).split('.')[-1]
  77. return [{
  78. 'id': '%s' % part_id,
  79. 'url': final_url,
  80. 'ext': ext,
  81. 'title': title,
  82. 'thumbnail': thumbnail_url,
  83. 'description': description,
  84. 'view_count': view_count,
  85. 'timestamp': timestamp,
  86. 'duration': float_or_none(part.get('seconds'), 1000),
  87. 'filesize': int_or_none(part.get('size')),
  88. 'http_headers': {
  89. 'Referer': self._PLAYER_URL,
  90. },
  91. }]
  92. entries = InAdvancePagedList(part_func, len_parts, 1)
  93. return {
  94. '_type': 'multi_video',
  95. 'entries': entries,
  96. 'id': video_id,
  97. 'title': title,
  98. }
  99. class TudouPlaylistIE(InfoExtractor):
  100. IE_NAME = 'tudou:playlist'
  101. _VALID_URL = r'https?://(?:www\.)?tudou\.com/listplay/(?P<id>[\w-]{11})\.html'
  102. _TESTS = [{
  103. 'url': 'http://www.tudou.com/listplay/zzdE77v6Mmo.html',
  104. 'info_dict': {
  105. 'id': 'zzdE77v6Mmo',
  106. },
  107. 'playlist_mincount': 209,
  108. }]
  109. def _real_extract(self, url):
  110. playlist_id = self._match_id(url)
  111. playlist_data = self._download_json(
  112. 'http://www.tudou.com/tvp/plist.action?lcode=%s' % playlist_id, playlist_id)
  113. entries = [self.url_result(
  114. 'http://www.tudou.com/programs/view/%s' % item['icode'],
  115. 'Tudou', item['icode'],
  116. item['kw']) for item in playlist_data['items']]
  117. return self.playlist_result(entries, playlist_id)
  118. class TudouAlbumIE(InfoExtractor):
  119. IE_NAME = 'tudou:album'
  120. _VALID_URL = r'https?://(?:www\.)?tudou\.com/album(?:cover|play)/(?P<id>[\w-]{11})'
  121. _TESTS = [{
  122. 'url': 'http://www.tudou.com/albumplay/v5qckFJvNJg.html',
  123. 'info_dict': {
  124. 'id': 'v5qckFJvNJg',
  125. },
  126. 'playlist_mincount': 45,
  127. }]
  128. def _real_extract(self, url):
  129. album_id = self._match_id(url)
  130. album_data = self._download_json(
  131. 'http://www.tudou.com/tvp/alist.action?acode=%s' % album_id, album_id)
  132. entries = [self.url_result(
  133. 'http://www.tudou.com/programs/view/%s' % item['icode'],
  134. 'Tudou', item['icode'],
  135. item['kw']) for item in album_data['items']]
  136. return self.playlist_result(entries, album_id)