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.

78 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(
  44. r",kw:\s*['\"](.+?)[\"']", webpage, u'title')
  45. thumbnail_url = self._search_regex(
  46. r",pic:\s*[\"'](.+?)[\"']", webpage, u'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. quality = sorted(segments.keys())[-1]
  52. parts = segments[quality]
  53. result = []
  54. len_parts = len(parts)
  55. if len_parts > 1:
  56. self.to_screen(u'%s: found %s parts' % (video_id, len_parts))
  57. for part in parts:
  58. part_id = part['k']
  59. final_url = self._url_for_id(part_id, quality)
  60. ext = (final_url.split('?')[0]).split('.')[-1]
  61. part_info = {'id': part_id,
  62. 'url': final_url,
  63. 'ext': ext,
  64. 'title': title,
  65. 'thumbnail': thumbnail_url,
  66. }
  67. result.append(part_info)
  68. return result