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