87 lines
3.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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'https?://(?:www\.)?tudou\.com/(?:listplay|programs(?:/view)?|albumplay)/.*?/(?P<id>[^/?#]+?)(?:\.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. def _url_for_id(self, id, quality=None):
  27. info_url = "http://v2.tudou.com/f?id=" + str(id)
  28. if quality:
  29. info_url += '&hd' + quality
  30. webpage = self._download_webpage(info_url, id, "Opening the info webpage")
  31. final_url = self._html_search_regex('>(.+?)</f>', webpage, 'video url')
  32. return final_url
  33. def _real_extract(self, url):
  34. video_id = self._match_id(url)
  35. webpage = self._download_webpage(url, video_id)
  36. m = re.search(r'vcode:\s*[\'"](.+?)[\'"]', webpage)
  37. if m and m.group(1):
  38. return {
  39. '_type': 'url',
  40. 'url': 'youku:' + m.group(1),
  41. 'ie_key': 'Youku'
  42. }
  43. title = self._search_regex(
  44. r",kw:\s*['\"](.+?)[\"']", webpage, 'title')
  45. thumbnail_url = self._search_regex(
  46. r",pic:\s*[\"'](.+?)[\"']", webpage, 'thumbnail URL', fatal=False)
  47. segs_json = self._search_regex(r'segs: \'(.*)\'', webpage, 'segments')
  48. segments = json.loads(segs_json)
  49. # It looks like the keys are the arguments that have to be passed as
  50. # the hd field in the request url, we pick the higher
  51. # Also, filter non-number qualities (see issue #3643).
  52. quality = sorted(filter(lambda k: k.isdigit(), segments.keys()),
  53. key=lambda k: int(k))[-1]
  54. parts = segments[quality]
  55. result = []
  56. len_parts = len(parts)
  57. if len_parts > 1:
  58. self.to_screen('%s: found %s parts' % (video_id, len_parts))
  59. for part in parts:
  60. part_id = part['k']
  61. final_url = self._url_for_id(part_id, quality)
  62. ext = (final_url.split('?')[0]).split('.')[-1]
  63. part_info = {
  64. 'id': '%s' % part_id,
  65. 'url': final_url,
  66. 'ext': ext,
  67. 'title': title,
  68. 'thumbnail': thumbnail_url,
  69. }
  70. result.append(part_info)
  71. return {
  72. '_type': 'multi_video',
  73. 'entries': result,
  74. 'id': video_id,
  75. 'title': title,
  76. }