tudou.py 3.4 KB

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