]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/rutube.py
[extractors] Use new framework for existing embeds (#4307)
[yt-dlp.git] / yt_dlp / extractor / rutube.py
CommitLineData
1547c8cc 1import itertools
bfd14b1b
JMF
2
3from .common import InfoExtractor
1cc79574 4from ..compat import (
bfd14b1b 5 compat_str,
1cc79574
PH
6)
7from ..utils import (
41371968 8 determine_ext,
c3dd44e0 9 bool_or_none,
debed8d7 10 int_or_none,
4dfbf869 11 parse_qs,
c3dd44e0
S
12 try_get,
13 unified_timestamp,
3052a30d 14 url_or_none,
bfd14b1b
JMF
15)
16
17
48b81374 18class RutubeBaseIE(InfoExtractor):
4c0e0dc9
S
19 def _download_api_info(self, video_id, query=None):
20 if not query:
21 query = {}
22 query['format'] = 'json'
23 return self._download_json(
24 'http://rutube.ru/api/video/%s/' % video_id,
25 video_id, 'Downloading video JSON',
26 'Unable to download video JSON', query=query)
27
28 @staticmethod
29 def _extract_info(video, video_id=None, require_title=True):
48b81374
S
30 title = video['title'] if require_title else video.get('title')
31
32 age_limit = video.get('is_adult')
33 if age_limit is not None:
34 age_limit = 18 if age_limit is True else 0
35
36 uploader_id = try_get(video, lambda x: x['author']['id'])
37 category = try_get(video, lambda x: x['category']['name'])
38
39 return {
4c0e0dc9 40 'id': video.get('id') or video_id if video_id else video['id'],
48b81374
S
41 'title': title,
42 'description': video.get('description'),
43 'thumbnail': video.get('thumbnail_url'),
44 'duration': int_or_none(video.get('duration')),
45 'uploader': try_get(video, lambda x: x['author']['name']),
46 'uploader_id': compat_str(uploader_id) if uploader_id else None,
47 'timestamp': unified_timestamp(video.get('created_ts')),
48 'category': [category] if category else None,
49 'age_limit': age_limit,
50 'view_count': int_or_none(video.get('hits')),
51 'comment_count': int_or_none(video.get('comments_count')),
c3dd44e0 52 'is_live': bool_or_none(video.get('is_livestream')),
48b81374
S
53 }
54
4c0e0dc9
S
55 def _download_and_extract_info(self, video_id, query=None):
56 return self._extract_info(
57 self._download_api_info(video_id, query=query), video_id)
58
59 def _download_api_options(self, video_id, query=None):
60 if not query:
61 query = {}
62 query['format'] = 'json'
63 return self._download_json(
64 'http://rutube.ru/api/play/options/%s/' % video_id,
65 video_id, 'Downloading options JSON',
66 'Unable to download options JSON',
67 headers=self.geo_verification_headers(), query=query)
68
69 def _extract_formats(self, options, video_id):
70 formats = []
71 for format_id, format_url in options['video_balancer'].items():
72 ext = determine_ext(format_url)
73 if ext == 'm3u8':
74 formats.extend(self._extract_m3u8_formats(
75 format_url, video_id, 'mp4', m3u8_id=format_id, fatal=False))
76 elif ext == 'f4m':
77 formats.extend(self._extract_f4m_formats(
78 format_url, video_id, f4m_id=format_id, fatal=False))
79 else:
80 formats.append({
81 'url': format_url,
82 'format_id': format_id,
83 })
84 self._sort_formats(formats)
85 return formats
86
87 def _download_and_extract_formats(self, video_id, query=None):
88 return self._extract_formats(
89 self._download_api_options(video_id, query=query), video_id)
90
48b81374
S
91
92class RutubeIE(RutubeBaseIE):
1547c8cc 93 IE_NAME = 'rutube'
37e3b90d 94 IE_DESC = 'Rutube videos'
bc82f228 95 _VALID_URL = r'https?://rutube\.ru/(?:video|(?:play/)?embed)/(?P<id>[\da-z]{32})'
bfd973ec 96 _EMBED_REGEX = [r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//rutube\.ru/embed/[\da-z]{32}.*?)\1']
bfd14b1b 97
2d3b7027 98 _TESTS = [{
1547c8cc 99 'url': 'http://rutube.ru/video/3eac3b4561676c17df9132a9a1e62e3e/',
4c0e0dc9 100 'md5': '1d24f180fac7a02f3900712e5a5764d6',
1547c8cc 101 'info_dict': {
c72477bd 102 'id': '3eac3b4561676c17df9132a9a1e62e3e',
4c0e0dc9 103 'ext': 'mp4',
1547c8cc 104 'title': 'Раненный кенгуру забежал в аптеку',
00ff8f92 105 'description': 'http://www.ntdtv.ru ',
4c0e0dc9 106 'duration': 81,
1547c8cc 107 'uploader': 'NTDRussian',
108 'uploader_id': '29790',
48b81374 109 'timestamp': 1381943602,
00ff8f92 110 'upload_date': '20131016',
c8d1be77 111 'age_limit': 0,
bfd14b1b 112 },
2d3b7027
S
113 }, {
114 'url': 'http://rutube.ru/play/embed/a10e53b86e8f349080f718582ce4c661',
115 'only_matching': True,
bc82f228
S
116 }, {
117 'url': 'http://rutube.ru/embed/a10e53b86e8f349080f718582ce4c661',
118 'only_matching': True,
debed8d7 119 }, {
120 'url': 'http://rutube.ru/video/3eac3b4561676c17df9132a9a1e62e3e/?pl_id=4252',
121 'only_matching': True,
48b81374
S
122 }, {
123 'url': 'https://rutube.ru/video/10b3a03fc01d5bbcc632a2f3514e8aab/?pl_type=source',
124 'only_matching': True,
2d3b7027 125 }]
bfd14b1b 126
debed8d7 127 @classmethod
128 def suitable(cls, url):
48b81374 129 return False if RutubePlaylistIE.suitable(url) else super(RutubeIE, cls).suitable(url)
debed8d7 130
bfd14b1b 131 def _real_extract(self, url):
1cc79574 132 video_id = self._match_id(url)
4c0e0dc9
S
133 info = self._download_and_extract_info(video_id)
134 info['formats'] = self._download_and_extract_formats(video_id)
48b81374 135 return info
1547c8cc 136
137
4c0e0dc9 138class RutubeEmbedIE(RutubeBaseIE):
7a1818c9
PH
139 IE_NAME = 'rutube:embed'
140 IE_DESC = 'Rutube embedded videos'
25042f73 141 _VALID_URL = r'https?://rutube\.ru/(?:video|play)/embed/(?P<id>[0-9]+)'
7a1818c9 142
f52183a8 143 _TESTS = [{
7a1818c9
PH
144 'url': 'http://rutube.ru/video/embed/6722881?vk_puid37=&vk_puid38=',
145 'info_dict': {
146 'id': 'a10e53b86e8f349080f718582ce4c661',
4c0e0dc9 147 'ext': 'mp4',
48b81374 148 'timestamp': 1387830582,
7a1818c9
PH
149 'upload_date': '20131223',
150 'uploader_id': '297833',
151 'description': 'Видео группы ★http://vk.com/foxkidsreset★ музей Fox Kids и Jetix<br/><br/> восстановлено и сделано в шикоформате subziro89 http://vk.com/subziro89',
152 'uploader': 'subziro89 ILya',
153 'title': 'Мистический городок Эйри в Индиан 5 серия озвучка subziro89',
154 },
155 'params': {
48b81374 156 'skip_download': True,
7a1818c9 157 },
f52183a8
S
158 }, {
159 'url': 'http://rutube.ru/play/embed/8083783',
160 'only_matching': True,
4c0e0dc9
S
161 }, {
162 # private video
163 'url': 'https://rutube.ru/play/embed/10631925?p=IbAigKqWd1do4mjaM5XLIQ',
164 'only_matching': True,
f52183a8 165 }]
7a1818c9
PH
166
167 def _real_extract(self, url):
168 embed_id = self._match_id(url)
4c0e0dc9
S
169 # Query may contain private videos token and should be passed to API
170 # requests (see #19163)
4dfbf869 171 query = parse_qs(url)
4c0e0dc9
S
172 options = self._download_api_options(embed_id, query)
173 video_id = options['effective_video']
174 formats = self._extract_formats(options, video_id)
175 info = self._download_and_extract_info(video_id, query)
176 info.update({
177 'extractor_key': 'Rutube',
178 'formats': formats,
179 })
180 return info
7a1818c9
PH
181
182
48b81374
S
183class RutubePlaylistBaseIE(RutubeBaseIE):
184 def _next_page_url(self, page_num, playlist_id, *args, **kwargs):
185 return self._PAGE_TEMPLATE % (playlist_id, page_num)
186
187 def _entries(self, playlist_id, *args, **kwargs):
188 next_page_url = None
189 for pagenum in itertools.count(1):
190 page = self._download_json(
191 next_page_url or self._next_page_url(
192 pagenum, playlist_id, *args, **kwargs),
193 playlist_id, 'Downloading page %s' % pagenum)
194
195 results = page.get('results')
196 if not results or not isinstance(results, list):
197 break
198
199 for result in results:
3052a30d
S
200 video_url = url_or_none(result.get('video_url'))
201 if not video_url:
48b81374 202 continue
4c0e0dc9 203 entry = self._extract_info(result, require_title=False)
48b81374
S
204 entry.update({
205 '_type': 'url',
206 'url': video_url,
207 'ie_key': RutubeIE.ie_key(),
208 })
209 yield entry
210
211 next_page_url = page.get('next')
212 if not next_page_url or not page.get('has_next'):
213 break
214
215 def _extract_playlist(self, playlist_id, *args, **kwargs):
216 return self.playlist_result(
217 self._entries(playlist_id, *args, **kwargs),
218 playlist_id, kwargs.get('playlist_name'))
219
220 def _real_extract(self, url):
221 return self._extract_playlist(self._match_id(url))
222
223
13debc86
AG
224class RutubeTagsIE(RutubePlaylistBaseIE):
225 IE_NAME = 'rutube:tags'
226 IE_DESC = 'Rutube tags'
5886b38d 227 _VALID_URL = r'https?://rutube\.ru/tags/video/(?P<id>\d+)'
22a6f150
PH
228 _TESTS = [{
229 'url': 'http://rutube.ru/tags/video/1800/',
230 'info_dict': {
231 'id': '1800',
232 },
233 'playlist_mincount': 68,
234 }]
1547c8cc 235
236 _PAGE_TEMPLATE = 'http://rutube.ru/api/tags/video/%s/?page=%s&format=json'
237
1547c8cc 238
48b81374 239class RutubeMovieIE(RutubePlaylistBaseIE):
1547c8cc 240 IE_NAME = 'rutube:movie'
37e3b90d 241 IE_DESC = 'Rutube movies'
5886b38d 242 _VALID_URL = r'https?://rutube\.ru/metainfo/tv/(?P<id>\d+)'
22a6f150 243 _TESTS = []
1547c8cc 244
245 _MOVIE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/?format=json'
246 _PAGE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/video?page=%s&format=json'
247
248 def _real_extract(self, url):
1cc79574 249 movie_id = self._match_id(url)
18c95c1a 250 movie = self._download_json(
37e3b90d
PH
251 self._MOVIE_TEMPLATE % movie_id, movie_id,
252 'Downloading movie JSON')
48b81374
S
253 return self._extract_playlist(
254 movie_id, playlist_name=movie.get('name'))
e3a9f32f 255
256
48b81374 257class RutubePersonIE(RutubePlaylistBaseIE):
e3a9f32f 258 IE_NAME = 'rutube:person'
259 IE_DESC = 'Rutube person videos'
5886b38d 260 _VALID_URL = r'https?://rutube\.ru/video/person/(?P<id>\d+)'
22a6f150
PH
261 _TESTS = [{
262 'url': 'http://rutube.ru/video/person/313878/',
263 'info_dict': {
264 'id': '313878',
265 },
266 'playlist_mincount': 37,
267 }]
e3a9f32f 268
37e3b90d 269 _PAGE_TEMPLATE = 'http://rutube.ru/api/video/person/%s/?page=%s&format=json'
debed8d7 270
271
48b81374 272class RutubePlaylistIE(RutubePlaylistBaseIE):
debed8d7 273 IE_NAME = 'rutube:playlist'
274 IE_DESC = 'Rutube playlists'
48b81374 275 _VALID_URL = r'https?://rutube\.ru/(?:video|(?:play/)?embed)/[\da-z]{32}/\?.*?\bpl_id=(?P<id>\d+)'
debed8d7 276 _TESTS = [{
48b81374 277 'url': 'https://rutube.ru/video/cecd58ed7d531fc0f3d795d51cee9026/?pl_id=3097&pl_type=tag',
debed8d7 278 'info_dict': {
48b81374 279 'id': '3097',
debed8d7 280 },
48b81374
S
281 'playlist_count': 27,
282 }, {
283 'url': 'https://rutube.ru/video/10b3a03fc01d5bbcc632a2f3514e8aab/?pl_id=4252&pl_type=source',
284 'only_matching': True,
debed8d7 285 }]
286
48b81374 287 _PAGE_TEMPLATE = 'http://rutube.ru/api/playlist/%s/%s/?page=%s&format=json'
debed8d7 288
f12a6e88
S
289 @classmethod
290 def suitable(cls, url):
3fb4e21b 291 from ..utils import int_or_none, parse_qs
292
f12a6e88
S
293 if not super(RutubePlaylistIE, cls).suitable(url):
294 return False
4dfbf869 295 params = parse_qs(url)
48b81374 296 return params.get('pl_type', [None])[0] and int_or_none(params.get('pl_id', [None])[0])
debed8d7 297
48b81374
S
298 def _next_page_url(self, page_num, playlist_id, item_kind):
299 return self._PAGE_TEMPLATE % (item_kind, playlist_id, page_num)
debed8d7 300
48b81374 301 def _real_extract(self, url):
4dfbf869 302 qs = parse_qs(url)
48b81374
S
303 playlist_kind = qs['pl_type'][0]
304 playlist_id = qs['pl_id'][0]
305 return self._extract_playlist(playlist_id, item_kind=playlist_kind)
13debc86
AG
306
307
308class RutubeChannelIE(RutubePlaylistBaseIE):
309 IE_NAME = 'rutube:channel'
310 IE_DESC = 'Rutube channel'
311 _VALID_URL = r'https?://rutube\.ru/channel/(?P<id>\d+)/videos'
312 _TESTS = [{
313 'url': 'https://rutube.ru/channel/639184/videos/',
314 'info_dict': {
315 'id': '639184',
316 },
317 'playlist_mincount': 133,
318 }]
319
320 _PAGE_TEMPLATE = 'http://rutube.ru/api/video/person/%s/?page=%s&format=json'