tudou.py 3.1 KB

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