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.

77 lines
2.7 KiB

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