]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/tudou.py
[tudou] Add the test case (#6273)
[yt-dlp.git] / youtube_dl / extractor / tudou.py
1 # coding: utf-8
2
3 from __future__ import unicode_literals
4
5 import re
6 import json
7
8 from .common import InfoExtractor
9
10
11 class TudouIE(InfoExtractor):
12 _VALID_URL = r'https?://(?:www\.)?tudou\.com/(?:listplay|programs(?:/view)?|albumplay)/?.*/(?P<id>[^/?#]+?)(?:\.html)?/?(?:$|[?#])'
13 _TESTS = [{
14 'url': 'http://www.tudou.com/listplay/zzdE77v6Mmo/2xN2duXMxmw.html',
15 'md5': '140a49ed444bd22f93330985d8475fcb',
16 'info_dict': {
17 'id': '159448201',
18 'ext': 'f4v',
19 'title': '卡马乔国足开大脚长传冲吊集锦',
20 'thumbnail': 're:^https?://.*\.jpg$',
21 }
22 }, {
23 'url': 'http://www.tudou.com/programs/view/ajX3gyhL0pc/',
24 'info_dict': {
25 'id': '117049447',
26 'ext': 'f4v',
27 'title': 'La Sylphide-Bolshoi-Ekaterina Krysanova & Vyacheslav Lopatin 2012',
28 'thumbnail': 're:^https?://.*\.jpg$',
29 }
30 }, {
31 'url': 'http://www.tudou.com/albumplay/cJAHGih4yYg.html',
32 'only_matching': True,
33 }]
34
35 _PLAYER_URL = 'http://js.tudouui.com/bin/lingtong/PortalPlayer_177.swf'
36
37 def _url_for_id(self, id, quality=None):
38 info_url = "http://v2.tudou.com/f?id=" + str(id)
39 if quality:
40 info_url += '&hd' + quality
41 webpage = self._download_webpage(info_url, id, "Opening the info webpage")
42 final_url = self._html_search_regex('>(.+?)</f>', webpage, 'video url')
43 return final_url
44
45 def _real_extract(self, url):
46 video_id = self._match_id(url)
47 webpage = self._download_webpage(url, video_id)
48
49 m = re.search(r'vcode:\s*[\'"](.+?)[\'"]', webpage)
50 if m and m.group(1):
51 return {
52 '_type': 'url',
53 'url': 'youku:' + m.group(1),
54 'ie_key': 'Youku'
55 }
56
57 title = self._search_regex(
58 r",kw:\s*['\"](.+?)[\"']", webpage, 'title')
59 thumbnail_url = self._search_regex(
60 r",pic:\s*[\"'](.+?)[\"']", webpage, 'thumbnail URL', fatal=False)
61
62 player_url = self._search_regex(
63 r"playerUrl\s*:\s*['\"](.+?\.swf)[\"']",
64 webpage, 'player URL', default=self._PLAYER_URL)
65
66 segs_json = self._search_regex(r'segs: \'(.*)\'', webpage, 'segments')
67 segments = json.loads(segs_json)
68 # It looks like the keys are the arguments that have to be passed as
69 # the hd field in the request url, we pick the higher
70 # Also, filter non-number qualities (see issue #3643).
71 quality = sorted(filter(lambda k: k.isdigit(), segments.keys()),
72 key=lambda k: int(k))[-1]
73 parts = segments[quality]
74 result = []
75 len_parts = len(parts)
76 if len_parts > 1:
77 self.to_screen('%s: found %s parts' % (video_id, len_parts))
78 for part in parts:
79 part_id = part['k']
80 final_url = self._url_for_id(part_id, quality)
81 ext = (final_url.split('?')[0]).split('.')[-1]
82 part_info = {
83 'id': '%s' % part_id,
84 'url': final_url,
85 'ext': ext,
86 'title': title,
87 'thumbnail': thumbnail_url,
88 'http_headers': {
89 'Referer': player_url,
90 },
91 }
92 result.append(part_info)
93
94 return {
95 '_type': 'multi_video',
96 'entries': result,
97 'id': video_id,
98 'title': title,
99 }