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