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