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