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.

94 lines
3.4 KiB

10 years ago
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. class TudouIE(InfoExtractor):
  6. _VALID_URL = r'https?://(?:www\.)?tudou\.com/(?:listplay|programs(?:/view)?|albumplay)/([^/]+/)*(?P<id>[^/?#]+?)(?:\.html)?/?(?:$|[?#])'
  7. _TESTS = [{
  8. 'url': 'http://www.tudou.com/listplay/zzdE77v6Mmo/2xN2duXMxmw.html',
  9. 'md5': '140a49ed444bd22f93330985d8475fcb',
  10. 'info_dict': {
  11. 'id': '159448201',
  12. 'ext': 'f4v',
  13. 'title': '卡马乔国足开大脚长传冲吊集锦',
  14. 'thumbnail': 're:^https?://.*\.jpg$',
  15. }
  16. }, {
  17. 'url': 'http://www.tudou.com/programs/view/ajX3gyhL0pc/',
  18. 'info_dict': {
  19. 'id': '117049447',
  20. 'ext': 'f4v',
  21. 'title': 'La Sylphide-Bolshoi-Ekaterina Krysanova & Vyacheslav Lopatin 2012',
  22. 'thumbnail': 're:^https?://.*\.jpg$',
  23. }
  24. }, {
  25. 'url': 'http://www.tudou.com/albumplay/cJAHGih4yYg.html',
  26. 'only_matching': True,
  27. }]
  28. _PLAYER_URL = 'http://js.tudouui.com/bin/lingtong/PortalPlayer_177.swf'
  29. def _url_for_id(self, video_id, quality=None):
  30. info_url = 'http://v2.tudou.com/f?id=' + compat_str(video_id)
  31. if quality:
  32. info_url += '&hd' + quality
  33. xml_data = self._download_xml(info_url, video_id, "Opening the info XML page")
  34. final_url = xml_data.text
  35. return final_url
  36. def _real_extract(self, url):
  37. video_id = self._match_id(url)
  38. webpage = self._download_webpage(url, video_id)
  39. youku_vcode = self._search_regex(
  40. r'vcode\s*:\s*[\'"]([^\'"]*)[\'"]', webpage, 'youku vcode', default=None)
  41. if youku_vcode:
  42. return self.url_result('youku:' + youku_vcode, ie='Youku')
  43. title = self._search_regex(
  44. r',kw\s*:\s*[\'"]([^\'"]+)[\'"]', webpage, 'title')
  45. thumbnail_url = self._search_regex(
  46. r',pic\s*:\s*[\'"]([^\'"]+)[\'"]', webpage, 'thumbnail URL', fatal=False)
  47. player_url = self._search_regex(
  48. r'playerUrl\s*:\s*[\'"]([^\'"]+\.swf)[\'"]',
  49. webpage, 'player URL', default=self._PLAYER_URL)
  50. segments = self._parse_json(self._search_regex(
  51. r'segs: \'([^\']+)\'', webpage, 'segments'), video_id)
  52. # It looks like the keys are the arguments that have to be passed as
  53. # the hd field in the request url, we pick the higher
  54. # Also, filter non-number qualities (see issue #3643).
  55. quality = sorted(filter(lambda k: k.isdigit(), segments.keys()),
  56. key=lambda k: int(k))[-1]
  57. parts = segments[quality]
  58. result = []
  59. len_parts = len(parts)
  60. if len_parts > 1:
  61. self.to_screen('%s: found %s parts' % (video_id, len_parts))
  62. for part in parts:
  63. part_id = part['k']
  64. final_url = self._url_for_id(part_id, quality)
  65. ext = (final_url.split('?')[0]).split('.')[-1]
  66. part_info = {
  67. 'id': '%s' % part_id,
  68. 'url': final_url,
  69. 'ext': ext,
  70. 'title': title,
  71. 'thumbnail': thumbnail_url,
  72. 'http_headers': {
  73. 'Referer': player_url,
  74. },
  75. }
  76. result.append(part_info)
  77. return {
  78. '_type': 'multi_video',
  79. 'entries': result,
  80. 'id': video_id,
  81. 'title': title,
  82. }