[youtube] Skip unsupported adaptive stream type (#18804)
[youtube-dl] / youtube_dl / extractor / rutube.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import itertools
6
7 from .common import InfoExtractor
8 from ..compat import (
9     compat_str,
10     compat_parse_qs,
11     compat_urllib_parse_urlparse,
12 )
13 from ..utils import (
14     determine_ext,
15     bool_or_none,
16     int_or_none,
17     try_get,
18     unified_timestamp,
19     url_or_none,
20 )
21
22
23 class RutubeBaseIE(InfoExtractor):
24     def _extract_video(self, video, video_id=None, require_title=True):
25         title = video['title'] if require_title else video.get('title')
26
27         age_limit = video.get('is_adult')
28         if age_limit is not None:
29             age_limit = 18 if age_limit is True else 0
30
31         uploader_id = try_get(video, lambda x: x['author']['id'])
32         category = try_get(video, lambda x: x['category']['name'])
33
34         return {
35             'id': video.get('id') or video_id,
36             'title': title,
37             'description': video.get('description'),
38             'thumbnail': video.get('thumbnail_url'),
39             'duration': int_or_none(video.get('duration')),
40             'uploader': try_get(video, lambda x: x['author']['name']),
41             'uploader_id': compat_str(uploader_id) if uploader_id else None,
42             'timestamp': unified_timestamp(video.get('created_ts')),
43             'category': [category] if category else None,
44             'age_limit': age_limit,
45             'view_count': int_or_none(video.get('hits')),
46             'comment_count': int_or_none(video.get('comments_count')),
47             'is_live': bool_or_none(video.get('is_livestream')),
48         }
49
50
51 class RutubeIE(RutubeBaseIE):
52     IE_NAME = 'rutube'
53     IE_DESC = 'Rutube videos'
54     _VALID_URL = r'https?://rutube\.ru/(?:video|(?:play/)?embed)/(?P<id>[\da-z]{32})'
55
56     _TESTS = [{
57         'url': 'http://rutube.ru/video/3eac3b4561676c17df9132a9a1e62e3e/',
58         'md5': '79938ade01294ef7e27574890d0d3769',
59         'info_dict': {
60             'id': '3eac3b4561676c17df9132a9a1e62e3e',
61             'ext': 'flv',
62             'title': 'Раненный кенгуру забежал в аптеку',
63             'description': 'http://www.ntdtv.ru ',
64             'duration': 80,
65             'uploader': 'NTDRussian',
66             'uploader_id': '29790',
67             'timestamp': 1381943602,
68             'upload_date': '20131016',
69             'age_limit': 0,
70         },
71     }, {
72         'url': 'http://rutube.ru/play/embed/a10e53b86e8f349080f718582ce4c661',
73         'only_matching': True,
74     }, {
75         'url': 'http://rutube.ru/embed/a10e53b86e8f349080f718582ce4c661',
76         'only_matching': True,
77     }, {
78         'url': 'http://rutube.ru/video/3eac3b4561676c17df9132a9a1e62e3e/?pl_id=4252',
79         'only_matching': True,
80     }, {
81         'url': 'https://rutube.ru/video/10b3a03fc01d5bbcc632a2f3514e8aab/?pl_type=source',
82         'only_matching': True,
83     }]
84
85     @classmethod
86     def suitable(cls, url):
87         return False if RutubePlaylistIE.suitable(url) else super(RutubeIE, cls).suitable(url)
88
89     @staticmethod
90     def _extract_urls(webpage):
91         return [mobj.group('url') for mobj in re.finditer(
92             r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//rutube\.ru/embed/[\da-z]{32}.*?)\1',
93             webpage)]
94
95     def _real_extract(self, url):
96         video_id = self._match_id(url)
97
98         video = self._download_json(
99             'http://rutube.ru/api/video/%s/?format=json' % video_id,
100             video_id, 'Downloading video JSON')
101
102         info = self._extract_video(video, video_id)
103
104         options = self._download_json(
105             'http://rutube.ru/api/play/options/%s/?format=json' % video_id,
106             video_id, 'Downloading options JSON',
107             headers=self.geo_verification_headers())
108
109         formats = []
110         for format_id, format_url in options['video_balancer'].items():
111             ext = determine_ext(format_url)
112             if ext == 'm3u8':
113                 formats.extend(self._extract_m3u8_formats(
114                     format_url, video_id, 'mp4', m3u8_id=format_id, fatal=False))
115             elif ext == 'f4m':
116                 formats.extend(self._extract_f4m_formats(
117                     format_url, video_id, f4m_id=format_id, fatal=False))
118             else:
119                 formats.append({
120                     'url': format_url,
121                     'format_id': format_id,
122                 })
123         self._sort_formats(formats)
124
125         info['formats'] = formats
126         return info
127
128
129 class RutubeEmbedIE(InfoExtractor):
130     IE_NAME = 'rutube:embed'
131     IE_DESC = 'Rutube embedded videos'
132     _VALID_URL = r'https?://rutube\.ru/(?:video|play)/embed/(?P<id>[0-9]+)'
133
134     _TESTS = [{
135         'url': 'http://rutube.ru/video/embed/6722881?vk_puid37=&vk_puid38=',
136         'info_dict': {
137             'id': 'a10e53b86e8f349080f718582ce4c661',
138             'ext': 'flv',
139             'timestamp': 1387830582,
140             'upload_date': '20131223',
141             'uploader_id': '297833',
142             'description': 'Видео группы ★http://vk.com/foxkidsreset★ музей Fox Kids и Jetix<br/><br/> восстановлено и сделано в шикоформате subziro89 http://vk.com/subziro89',
143             'uploader': 'subziro89 ILya',
144             'title': 'Мистический городок Эйри в Индиан 5 серия озвучка subziro89',
145         },
146         'params': {
147             'skip_download': True,
148         },
149     }, {
150         'url': 'http://rutube.ru/play/embed/8083783',
151         'only_matching': True,
152     }]
153
154     def _real_extract(self, url):
155         embed_id = self._match_id(url)
156         webpage = self._download_webpage(url, embed_id)
157
158         canonical_url = self._html_search_regex(
159             r'<link\s+rel="canonical"\s+href="([^"]+?)"', webpage,
160             'Canonical URL')
161         return self.url_result(canonical_url, RutubeIE.ie_key())
162
163
164 class RutubePlaylistBaseIE(RutubeBaseIE):
165     def _next_page_url(self, page_num, playlist_id, *args, **kwargs):
166         return self._PAGE_TEMPLATE % (playlist_id, page_num)
167
168     def _entries(self, playlist_id, *args, **kwargs):
169         next_page_url = None
170         for pagenum in itertools.count(1):
171             page = self._download_json(
172                 next_page_url or self._next_page_url(
173                     pagenum, playlist_id, *args, **kwargs),
174                 playlist_id, 'Downloading page %s' % pagenum)
175
176             results = page.get('results')
177             if not results or not isinstance(results, list):
178                 break
179
180             for result in results:
181                 video_url = url_or_none(result.get('video_url'))
182                 if not video_url:
183                     continue
184                 entry = self._extract_video(result, require_title=False)
185                 entry.update({
186                     '_type': 'url',
187                     'url': video_url,
188                     'ie_key': RutubeIE.ie_key(),
189                 })
190                 yield entry
191
192             next_page_url = page.get('next')
193             if not next_page_url or not page.get('has_next'):
194                 break
195
196     def _extract_playlist(self, playlist_id, *args, **kwargs):
197         return self.playlist_result(
198             self._entries(playlist_id, *args, **kwargs),
199             playlist_id, kwargs.get('playlist_name'))
200
201     def _real_extract(self, url):
202         return self._extract_playlist(self._match_id(url))
203
204
205 class RutubeChannelIE(RutubePlaylistBaseIE):
206     IE_NAME = 'rutube:channel'
207     IE_DESC = 'Rutube channels'
208     _VALID_URL = r'https?://rutube\.ru/tags/video/(?P<id>\d+)'
209     _TESTS = [{
210         'url': 'http://rutube.ru/tags/video/1800/',
211         'info_dict': {
212             'id': '1800',
213         },
214         'playlist_mincount': 68,
215     }]
216
217     _PAGE_TEMPLATE = 'http://rutube.ru/api/tags/video/%s/?page=%s&format=json'
218
219
220 class RutubeMovieIE(RutubePlaylistBaseIE):
221     IE_NAME = 'rutube:movie'
222     IE_DESC = 'Rutube movies'
223     _VALID_URL = r'https?://rutube\.ru/metainfo/tv/(?P<id>\d+)'
224     _TESTS = []
225
226     _MOVIE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/?format=json'
227     _PAGE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/video?page=%s&format=json'
228
229     def _real_extract(self, url):
230         movie_id = self._match_id(url)
231         movie = self._download_json(
232             self._MOVIE_TEMPLATE % movie_id, movie_id,
233             'Downloading movie JSON')
234         return self._extract_playlist(
235             movie_id, playlist_name=movie.get('name'))
236
237
238 class RutubePersonIE(RutubePlaylistBaseIE):
239     IE_NAME = 'rutube:person'
240     IE_DESC = 'Rutube person videos'
241     _VALID_URL = r'https?://rutube\.ru/video/person/(?P<id>\d+)'
242     _TESTS = [{
243         'url': 'http://rutube.ru/video/person/313878/',
244         'info_dict': {
245             'id': '313878',
246         },
247         'playlist_mincount': 37,
248     }]
249
250     _PAGE_TEMPLATE = 'http://rutube.ru/api/video/person/%s/?page=%s&format=json'
251
252
253 class RutubePlaylistIE(RutubePlaylistBaseIE):
254     IE_NAME = 'rutube:playlist'
255     IE_DESC = 'Rutube playlists'
256     _VALID_URL = r'https?://rutube\.ru/(?:video|(?:play/)?embed)/[\da-z]{32}/\?.*?\bpl_id=(?P<id>\d+)'
257     _TESTS = [{
258         'url': 'https://rutube.ru/video/cecd58ed7d531fc0f3d795d51cee9026/?pl_id=3097&pl_type=tag',
259         'info_dict': {
260             'id': '3097',
261         },
262         'playlist_count': 27,
263     }, {
264         'url': 'https://rutube.ru/video/10b3a03fc01d5bbcc632a2f3514e8aab/?pl_id=4252&pl_type=source',
265         'only_matching': True,
266     }]
267
268     _PAGE_TEMPLATE = 'http://rutube.ru/api/playlist/%s/%s/?page=%s&format=json'
269
270     @classmethod
271     def suitable(cls, url):
272         if not super(RutubePlaylistIE, cls).suitable(url):
273             return False
274         params = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
275         return params.get('pl_type', [None])[0] and int_or_none(params.get('pl_id', [None])[0])
276
277     def _next_page_url(self, page_num, playlist_id, item_kind):
278         return self._PAGE_TEMPLATE % (item_kind, playlist_id, page_num)
279
280     def _real_extract(self, url):
281         qs = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
282         playlist_kind = qs['pl_type'][0]
283         playlist_id = qs['pl_id'][0]
284         return self._extract_playlist(playlist_id, item_kind=playlist_kind)