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.

63 lines
2.3 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)/(?:view|(.+?))/(?:([^/]+)|([^/]+))(?:\.html)?'
  7. _TEST = {
  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. def _url_for_id(self, id, quality = None):
  16. info_url = "http://v2.tudou.com/f?id="+str(id)
  17. if quality:
  18. info_url += '&hd' + quality
  19. webpage = self._download_webpage(info_url, id, "Opening the info webpage")
  20. final_url = self._html_search_regex('>(.+?)</f>',webpage, 'video url')
  21. return final_url
  22. def _real_extract(self, url):
  23. mobj = re.match(self._VALID_URL, url)
  24. video_id = mobj.group(2)
  25. webpage = self._download_webpage(url, video_id)
  26. title = re.search(",kw:\"(.+)\"",webpage)
  27. if title is None:
  28. title = re.search(",kw: \'(.+)\'",webpage)
  29. title = title.group(1)
  30. thumbnail_url = re.search(",pic: \'(.+?)\'",webpage)
  31. if thumbnail_url is None:
  32. thumbnail_url = re.search(",pic:\"(.+?)\"",webpage)
  33. thumbnail_url = thumbnail_url.group(1)
  34. segs_json = self._search_regex(r'segs: \'(.*)\'', webpage, 'segments')
  35. segments = json.loads(segs_json)
  36. # It looks like the keys are the arguments that have to be passed as
  37. # the hd field in the request url, we pick the higher
  38. quality = sorted(segments.keys())[-1]
  39. parts = segments[quality]
  40. result = []
  41. len_parts = len(parts)
  42. if len_parts > 1:
  43. self.to_screen(u'%s: found %s parts' % (video_id, len_parts))
  44. for part in parts:
  45. part_id = part['k']
  46. final_url = self._url_for_id(part_id, quality)
  47. ext = (final_url.split('?')[0]).split('.')[-1]
  48. part_info = {'id': part_id,
  49. 'url': final_url,
  50. 'ext': ext,
  51. 'title': title,
  52. 'thumbnail': thumbnail_url,
  53. }
  54. result.append(part_info)
  55. return result