]>
Commit | Line | Data |
---|---|---|
1 | import itertools | |
2 | ||
3 | from .common import InfoExtractor | |
4 | from ..compat import ( | |
5 | compat_str, | |
6 | ) | |
7 | from ..utils import ( | |
8 | determine_ext, | |
9 | bool_or_none, | |
10 | int_or_none, | |
11 | parse_qs, | |
12 | try_get, | |
13 | unified_timestamp, | |
14 | url_or_none, | |
15 | ) | |
16 | ||
17 | ||
18 | class RutubeBaseIE(InfoExtractor): | |
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 | def _extract_info(self, video, video_id=None, require_title=True): | |
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']) | |
37 | description = video.get('description') | |
38 | duration = int_or_none(video.get('duration')) | |
39 | ||
40 | return { | |
41 | 'id': video.get('id') or video_id if video_id else video['id'], | |
42 | 'title': title, | |
43 | 'description': description, | |
44 | 'thumbnail': video.get('thumbnail_url'), | |
45 | 'duration': duration, | |
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')), | |
49 | 'category': [category] if category else None, | |
50 | 'age_limit': age_limit, | |
51 | 'view_count': int_or_none(video.get('hits')), | |
52 | 'comment_count': int_or_none(video.get('comments_count')), | |
53 | 'is_live': bool_or_none(video.get('is_livestream')), | |
54 | 'chapters': self._extract_chapters_from_description(description, duration), | |
55 | } | |
56 | ||
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 | }) | |
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 | ||
92 | ||
93 | class RutubeIE(RutubeBaseIE): | |
94 | IE_NAME = 'rutube' | |
95 | IE_DESC = 'Rutube videos' | |
96 | _VALID_URL = r'https?://rutube\.ru/(?:video(?:/private)?|(?:play/)?embed)/(?P<id>[\da-z]{32})' | |
97 | _EMBED_REGEX = [r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//rutube\.ru/(?:play/)?embed/[\da-z]{32}.*?)\1'] | |
98 | ||
99 | _TESTS = [{ | |
100 | 'url': 'http://rutube.ru/video/3eac3b4561676c17df9132a9a1e62e3e/', | |
101 | 'md5': 'e33ac625efca66aba86cbec9851f2692', | |
102 | 'info_dict': { | |
103 | 'id': '3eac3b4561676c17df9132a9a1e62e3e', | |
104 | 'ext': 'mp4', | |
105 | 'title': 'Раненный кенгуру забежал в аптеку', | |
106 | 'description': 'http://www.ntdtv.ru ', | |
107 | 'duration': 81, | |
108 | 'uploader': 'NTDRussian', | |
109 | 'uploader_id': '29790', | |
110 | 'timestamp': 1381943602, | |
111 | 'upload_date': '20131016', | |
112 | 'age_limit': 0, | |
113 | 'view_count': int, | |
114 | 'thumbnail': 'http://pic.rutubelist.ru/video/d2/a0/d2a0aec998494a396deafc7ba2c82add.jpg', | |
115 | 'category': ['Новости и СМИ'], | |
116 | 'chapters': [], | |
117 | }, | |
118 | 'expected_warnings': ['Unable to download f4m'], | |
119 | }, { | |
120 | 'url': 'http://rutube.ru/play/embed/a10e53b86e8f349080f718582ce4c661', | |
121 | 'only_matching': True, | |
122 | }, { | |
123 | 'url': 'http://rutube.ru/embed/a10e53b86e8f349080f718582ce4c661', | |
124 | 'only_matching': True, | |
125 | }, { | |
126 | 'url': 'http://rutube.ru/video/3eac3b4561676c17df9132a9a1e62e3e/?pl_id=4252', | |
127 | 'only_matching': True, | |
128 | }, { | |
129 | 'url': 'https://rutube.ru/video/10b3a03fc01d5bbcc632a2f3514e8aab/?pl_type=source', | |
130 | 'only_matching': True, | |
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', | |
147 | 'category': ['Видеоигры'], | |
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', | |
157 | 'category': ['Бизнес и предпринимательство'], | |
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': 'Стас Быков', | |
168 | }, | |
169 | 'expected_warnings': ['Unable to download f4m'], | |
170 | }] | |
171 | ||
172 | @classmethod | |
173 | def suitable(cls, url): | |
174 | return False if RutubePlaylistIE.suitable(url) else super(RutubeIE, cls).suitable(url) | |
175 | ||
176 | def _real_extract(self, url): | |
177 | video_id = self._match_id(url) | |
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) | |
181 | return info | |
182 | ||
183 | ||
184 | class RutubeEmbedIE(RutubeBaseIE): | |
185 | IE_NAME = 'rutube:embed' | |
186 | IE_DESC = 'Rutube embedded videos' | |
187 | _VALID_URL = r'https?://rutube\.ru/(?:video|play)/embed/(?P<id>[0-9]+)' | |
188 | ||
189 | _TESTS = [{ | |
190 | 'url': 'http://rutube.ru/video/embed/6722881?vk_puid37=&vk_puid38=', | |
191 | 'info_dict': { | |
192 | 'id': 'a10e53b86e8f349080f718582ce4c661', | |
193 | 'ext': 'mp4', | |
194 | 'timestamp': 1387830582, | |
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': { | |
202 | 'skip_download': True, | |
203 | }, | |
204 | }, { | |
205 | 'url': 'http://rutube.ru/play/embed/8083783', | |
206 | 'only_matching': True, | |
207 | }, { | |
208 | # private video | |
209 | 'url': 'https://rutube.ru/play/embed/10631925?p=IbAigKqWd1do4mjaM5XLIQ', | |
210 | 'only_matching': True, | |
211 | }] | |
212 | ||
213 | def _real_extract(self, url): | |
214 | embed_id = self._match_id(url) | |
215 | # Query may contain private videos token and should be passed to API | |
216 | # requests (see #19163) | |
217 | query = parse_qs(url) | |
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 | |
227 | ||
228 | ||
229 | class 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: | |
246 | video_url = url_or_none(result.get('video_url')) | |
247 | if not video_url: | |
248 | continue | |
249 | entry = self._extract_info(result, require_title=False) | |
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 | ||
270 | class RutubeTagsIE(RutubePlaylistBaseIE): | |
271 | IE_NAME = 'rutube:tags' | |
272 | IE_DESC = 'Rutube tags' | |
273 | _VALID_URL = r'https?://rutube\.ru/tags/video/(?P<id>\d+)' | |
274 | _TESTS = [{ | |
275 | 'url': 'http://rutube.ru/tags/video/1800/', | |
276 | 'info_dict': { | |
277 | 'id': '1800', | |
278 | }, | |
279 | 'playlist_mincount': 68, | |
280 | }] | |
281 | ||
282 | _PAGE_TEMPLATE = 'http://rutube.ru/api/tags/video/%s/?page=%s&format=json' | |
283 | ||
284 | ||
285 | class RutubeMovieIE(RutubePlaylistBaseIE): | |
286 | IE_NAME = 'rutube:movie' | |
287 | IE_DESC = 'Rutube movies' | |
288 | _VALID_URL = r'https?://rutube\.ru/metainfo/tv/(?P<id>\d+)' | |
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): | |
294 | movie_id = self._match_id(url) | |
295 | movie = self._download_json( | |
296 | self._MOVIE_TEMPLATE % movie_id, movie_id, | |
297 | 'Downloading movie JSON') | |
298 | return self._extract_playlist( | |
299 | movie_id, playlist_name=movie.get('name')) | |
300 | ||
301 | ||
302 | class RutubePersonIE(RutubePlaylistBaseIE): | |
303 | IE_NAME = 'rutube:person' | |
304 | IE_DESC = 'Rutube person videos' | |
305 | _VALID_URL = r'https?://rutube\.ru/video/person/(?P<id>\d+)' | |
306 | _TESTS = [{ | |
307 | 'url': 'http://rutube.ru/video/person/313878/', | |
308 | 'info_dict': { | |
309 | 'id': '313878', | |
310 | }, | |
311 | 'playlist_mincount': 37, | |
312 | }] | |
313 | ||
314 | _PAGE_TEMPLATE = 'http://rutube.ru/api/video/person/%s/?page=%s&format=json' | |
315 | ||
316 | ||
317 | class RutubePlaylistIE(RutubePlaylistBaseIE): | |
318 | IE_NAME = 'rutube:playlist' | |
319 | IE_DESC = 'Rutube playlists' | |
320 | _VALID_URL = r'https?://rutube\.ru/(?:video|(?:play/)?embed)/[\da-z]{32}/\?.*?\bpl_id=(?P<id>\d+)' | |
321 | _TESTS = [{ | |
322 | 'url': 'https://rutube.ru/video/cecd58ed7d531fc0f3d795d51cee9026/?pl_id=3097&pl_type=tag', | |
323 | 'info_dict': { | |
324 | 'id': '3097', | |
325 | }, | |
326 | 'playlist_count': 27, | |
327 | }, { | |
328 | 'url': 'https://rutube.ru/video/10b3a03fc01d5bbcc632a2f3514e8aab/?pl_id=4252&pl_type=source', | |
329 | 'only_matching': True, | |
330 | }] | |
331 | ||
332 | _PAGE_TEMPLATE = 'http://rutube.ru/api/playlist/%s/%s/?page=%s&format=json' | |
333 | ||
334 | @classmethod | |
335 | def suitable(cls, url): | |
336 | from ..utils import int_or_none, parse_qs | |
337 | ||
338 | if not super(RutubePlaylistIE, cls).suitable(url): | |
339 | return False | |
340 | params = parse_qs(url) | |
341 | return params.get('pl_type', [None])[0] and int_or_none(params.get('pl_id', [None])[0]) | |
342 | ||
343 | def _next_page_url(self, page_num, playlist_id, item_kind): | |
344 | return self._PAGE_TEMPLATE % (item_kind, playlist_id, page_num) | |
345 | ||
346 | def _real_extract(self, url): | |
347 | qs = parse_qs(url) | |
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) | |
351 | ||
352 | ||
353 | class 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' |