Merge pull request #8898 from dstftw/fragment-retries
[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 from ..compat import compat_str
7 from ..utils import (
8     int_or_none,
9     InAdvancePagedList,
10     float_or_none,
11     unescapeHTML,
12 )
13
14
15 class TudouIE(InfoExtractor):
16     IE_NAME = 'tudou'
17     _VALID_URL = r'https?://(?:www\.)?tudou\.com/(?:(?:programs|wlplay)/view|(?:listplay|albumplay)/[\w-]{11})/(?P<id>[\w-]{11})'
18     _TESTS = [{
19         'url': 'http://www.tudou.com/listplay/zzdE77v6Mmo/2xN2duXMxmw.html',
20         'md5': '140a49ed444bd22f93330985d8475fcb',
21         'info_dict': {
22             'id': '159448201',
23             'ext': 'f4v',
24             'title': '卡马乔国足开大脚长传冲吊集锦',
25             'thumbnail': 're:^https?://.*\.jpg$',
26             'timestamp': 1372113489000,
27             'description': '卡马乔卡家军,开大脚先进战术不完全集锦!',
28             'duration': 289.04,
29             'view_count': int,
30             'filesize': int,
31         }
32     }, {
33         'url': 'http://www.tudou.com/programs/view/ajX3gyhL0pc/',
34         'info_dict': {
35             'id': '117049447',
36             'ext': 'f4v',
37             'title': 'La Sylphide-Bolshoi-Ekaterina Krysanova & Vyacheslav Lopatin 2012',
38             'thumbnail': 're:^https?://.*\.jpg$',
39             'timestamp': 1349207518000,
40             'description': 'md5:294612423894260f2dcd5c6c04fe248b',
41             'duration': 5478.33,
42             'view_count': int,
43             'filesize': int,
44         }
45     }]
46
47     _PLAYER_URL = 'http://js.tudouui.com/bin/lingtong/PortalPlayer_177.swf'
48
49     def _url_for_id(self, video_id, quality=None):
50         info_url = 'http://v2.tudou.com/f?id=' + compat_str(video_id)
51         if quality:
52             info_url += '&hd' + quality
53         xml_data = self._download_xml(info_url, video_id, 'Opening the info XML page')
54         final_url = xml_data.text
55         return final_url
56
57     def _real_extract(self, url):
58         video_id = self._match_id(url)
59         item_data = self._download_json(
60             'http://www.tudou.com/tvp/getItemInfo.action?ic=%s' % video_id, video_id)
61
62         youku_vcode = item_data.get('vcode')
63         if youku_vcode:
64             return self.url_result('youku:' + youku_vcode, ie='Youku')
65
66         title = unescapeHTML(item_data['kw'])
67         description = item_data.get('desc')
68         thumbnail_url = item_data.get('pic')
69         view_count = int_or_none(item_data.get('playTimes'))
70         timestamp = int_or_none(item_data.get('pt'))
71
72         segments = self._parse_json(item_data['itemSegs'], video_id)
73         # It looks like the keys are the arguments that have to be passed as
74         # the hd field in the request url, we pick the higher
75         # Also, filter non-number qualities (see issue #3643).
76         quality = sorted(filter(lambda k: k.isdigit(), segments.keys()),
77                          key=lambda k: int(k))[-1]
78         parts = segments[quality]
79         len_parts = len(parts)
80         if len_parts > 1:
81             self.to_screen('%s: found %s parts' % (video_id, len_parts))
82
83         def part_func(partnum):
84             part = parts[partnum]
85             part_id = part['k']
86             final_url = self._url_for_id(part_id, quality)
87             ext = (final_url.split('?')[0]).split('.')[-1]
88             return [{
89                 'id': '%s' % part_id,
90                 'url': final_url,
91                 'ext': ext,
92                 'title': title,
93                 'thumbnail': thumbnail_url,
94                 'description': description,
95                 'view_count': view_count,
96                 'timestamp': timestamp,
97                 'duration': float_or_none(part.get('seconds'), 1000),
98                 'filesize': int_or_none(part.get('size')),
99                 'http_headers': {
100                     'Referer': self._PLAYER_URL,
101                 },
102             }]
103
104         entries = InAdvancePagedList(part_func, len_parts, 1)
105
106         return {
107             '_type': 'multi_video',
108             'entries': entries,
109             'id': video_id,
110             'title': title,
111         }
112
113
114 class TudouPlaylistIE(InfoExtractor):
115     IE_NAME = 'tudou:playlist'
116     _VALID_URL = r'https?://(?:www\.)?tudou\.com/listplay/(?P<id>[\w-]{11})\.html'
117     _TESTS = [{
118         'url': 'http://www.tudou.com/listplay/zzdE77v6Mmo.html',
119         'info_dict': {
120             'id': 'zzdE77v6Mmo',
121         },
122         'playlist_mincount': 209,
123     }]
124
125     def _real_extract(self, url):
126         playlist_id = self._match_id(url)
127         playlist_data = self._download_json(
128             'http://www.tudou.com/tvp/plist.action?lcode=%s' % playlist_id, playlist_id)
129         entries = [self.url_result(
130             'http://www.tudou.com/programs/view/%s' % item['icode'],
131             'Tudou', item['icode'],
132             item['kw']) for item in playlist_data['items']]
133         return self.playlist_result(entries, playlist_id)
134
135
136 class TudouAlbumIE(InfoExtractor):
137     IE_NAME = 'tudou:album'
138     _VALID_URL = r'https?://(?:www\.)?tudou\.com/album(?:cover|play)/(?P<id>[\w-]{11})'
139     _TESTS = [{
140         'url': 'http://www.tudou.com/albumplay/v5qckFJvNJg.html',
141         'info_dict': {
142             'id': 'v5qckFJvNJg',
143         },
144         'playlist_mincount': 45,
145     }]
146
147     def _real_extract(self, url):
148         album_id = self._match_id(url)
149         album_data = self._download_json(
150             'http://www.tudou.com/tvp/alist.action?acode=%s' % album_id, album_id)
151         entries = [self.url_result(
152             'http://www.tudou.com/programs/view/%s' % item['icode'],
153             'Tudou', item['icode'],
154             item['kw']) for item in album_data['items']]
155         return self.playlist_result(entries, album_id)