]>
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 | @staticmethod | |
29 | def _extract_info(video, video_id=None, require_title=True): | |
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 { | |
40 | 'id': video.get('id') or video_id if video_id else video['id'], | |
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')), | |
52 | 'is_live': bool_or_none(video.get('is_livestream')), | |
53 | } | |
54 | ||
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 | return formats | |
85 | ||
86 | def _download_and_extract_formats(self, video_id, query=None): | |
87 | return self._extract_formats( | |
88 | self._download_api_options(video_id, query=query), video_id) | |
89 | ||
90 | ||
91 | class RutubeIE(RutubeBaseIE): | |
92 | IE_NAME = 'rutube' | |
93 | IE_DESC = 'Rutube videos' | |
94 | _VALID_URL = r'https?://rutube\.ru/(?:video|(?:play/)?embed)/(?P<id>[\da-z]{32})' | |
95 | _EMBED_REGEX = [r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//rutube\.ru/(?:play/)?embed/[\da-z]{32}.*?)\1'] | |
96 | ||
97 | _TESTS = [{ | |
98 | 'url': 'http://rutube.ru/video/3eac3b4561676c17df9132a9a1e62e3e/', | |
99 | 'md5': '1d24f180fac7a02f3900712e5a5764d6', | |
100 | 'info_dict': { | |
101 | 'id': '3eac3b4561676c17df9132a9a1e62e3e', | |
102 | 'ext': 'mp4', | |
103 | 'title': 'Раненный кенгуру забежал в аптеку', | |
104 | 'description': 'http://www.ntdtv.ru ', | |
105 | 'duration': 81, | |
106 | 'uploader': 'NTDRussian', | |
107 | 'uploader_id': '29790', | |
108 | 'timestamp': 1381943602, | |
109 | 'upload_date': '20131016', | |
110 | 'age_limit': 0, | |
111 | }, | |
112 | }, { | |
113 | 'url': 'http://rutube.ru/play/embed/a10e53b86e8f349080f718582ce4c661', | |
114 | 'only_matching': True, | |
115 | }, { | |
116 | 'url': 'http://rutube.ru/embed/a10e53b86e8f349080f718582ce4c661', | |
117 | 'only_matching': True, | |
118 | }, { | |
119 | 'url': 'http://rutube.ru/video/3eac3b4561676c17df9132a9a1e62e3e/?pl_id=4252', | |
120 | 'only_matching': True, | |
121 | }, { | |
122 | 'url': 'https://rutube.ru/video/10b3a03fc01d5bbcc632a2f3514e8aab/?pl_type=source', | |
123 | 'only_matching': True, | |
124 | }] | |
125 | ||
126 | @classmethod | |
127 | def suitable(cls, url): | |
128 | return False if RutubePlaylistIE.suitable(url) else super(RutubeIE, cls).suitable(url) | |
129 | ||
130 | def _real_extract(self, url): | |
131 | video_id = self._match_id(url) | |
132 | info = self._download_and_extract_info(video_id) | |
133 | info['formats'] = self._download_and_extract_formats(video_id) | |
134 | return info | |
135 | ||
136 | ||
137 | class RutubeEmbedIE(RutubeBaseIE): | |
138 | IE_NAME = 'rutube:embed' | |
139 | IE_DESC = 'Rutube embedded videos' | |
140 | _VALID_URL = r'https?://rutube\.ru/(?:video|play)/embed/(?P<id>[0-9]+)' | |
141 | ||
142 | _TESTS = [{ | |
143 | 'url': 'http://rutube.ru/video/embed/6722881?vk_puid37=&vk_puid38=', | |
144 | 'info_dict': { | |
145 | 'id': 'a10e53b86e8f349080f718582ce4c661', | |
146 | 'ext': 'mp4', | |
147 | 'timestamp': 1387830582, | |
148 | 'upload_date': '20131223', | |
149 | 'uploader_id': '297833', | |
150 | 'description': 'Видео группы ★http://vk.com/foxkidsreset★ музей Fox Kids и Jetix<br/><br/> восстановлено и сделано в шикоформате subziro89 http://vk.com/subziro89', | |
151 | 'uploader': 'subziro89 ILya', | |
152 | 'title': 'Мистический городок Эйри в Индиан 5 серия озвучка subziro89', | |
153 | }, | |
154 | 'params': { | |
155 | 'skip_download': True, | |
156 | }, | |
157 | }, { | |
158 | 'url': 'http://rutube.ru/play/embed/8083783', | |
159 | 'only_matching': True, | |
160 | }, { | |
161 | # private video | |
162 | 'url': 'https://rutube.ru/play/embed/10631925?p=IbAigKqWd1do4mjaM5XLIQ', | |
163 | 'only_matching': True, | |
164 | }] | |
165 | ||
166 | def _real_extract(self, url): | |
167 | embed_id = self._match_id(url) | |
168 | # Query may contain private videos token and should be passed to API | |
169 | # requests (see #19163) | |
170 | query = parse_qs(url) | |
171 | options = self._download_api_options(embed_id, query) | |
172 | video_id = options['effective_video'] | |
173 | formats = self._extract_formats(options, video_id) | |
174 | info = self._download_and_extract_info(video_id, query) | |
175 | info.update({ | |
176 | 'extractor_key': 'Rutube', | |
177 | 'formats': formats, | |
178 | }) | |
179 | return info | |
180 | ||
181 | ||
182 | class RutubePlaylistBaseIE(RutubeBaseIE): | |
183 | def _next_page_url(self, page_num, playlist_id, *args, **kwargs): | |
184 | return self._PAGE_TEMPLATE % (playlist_id, page_num) | |
185 | ||
186 | def _entries(self, playlist_id, *args, **kwargs): | |
187 | next_page_url = None | |
188 | for pagenum in itertools.count(1): | |
189 | page = self._download_json( | |
190 | next_page_url or self._next_page_url( | |
191 | pagenum, playlist_id, *args, **kwargs), | |
192 | playlist_id, 'Downloading page %s' % pagenum) | |
193 | ||
194 | results = page.get('results') | |
195 | if not results or not isinstance(results, list): | |
196 | break | |
197 | ||
198 | for result in results: | |
199 | video_url = url_or_none(result.get('video_url')) | |
200 | if not video_url: | |
201 | continue | |
202 | entry = self._extract_info(result, require_title=False) | |
203 | entry.update({ | |
204 | '_type': 'url', | |
205 | 'url': video_url, | |
206 | 'ie_key': RutubeIE.ie_key(), | |
207 | }) | |
208 | yield entry | |
209 | ||
210 | next_page_url = page.get('next') | |
211 | if not next_page_url or not page.get('has_next'): | |
212 | break | |
213 | ||
214 | def _extract_playlist(self, playlist_id, *args, **kwargs): | |
215 | return self.playlist_result( | |
216 | self._entries(playlist_id, *args, **kwargs), | |
217 | playlist_id, kwargs.get('playlist_name')) | |
218 | ||
219 | def _real_extract(self, url): | |
220 | return self._extract_playlist(self._match_id(url)) | |
221 | ||
222 | ||
223 | class RutubeTagsIE(RutubePlaylistBaseIE): | |
224 | IE_NAME = 'rutube:tags' | |
225 | IE_DESC = 'Rutube tags' | |
226 | _VALID_URL = r'https?://rutube\.ru/tags/video/(?P<id>\d+)' | |
227 | _TESTS = [{ | |
228 | 'url': 'http://rutube.ru/tags/video/1800/', | |
229 | 'info_dict': { | |
230 | 'id': '1800', | |
231 | }, | |
232 | 'playlist_mincount': 68, | |
233 | }] | |
234 | ||
235 | _PAGE_TEMPLATE = 'http://rutube.ru/api/tags/video/%s/?page=%s&format=json' | |
236 | ||
237 | ||
238 | class RutubeMovieIE(RutubePlaylistBaseIE): | |
239 | IE_NAME = 'rutube:movie' | |
240 | IE_DESC = 'Rutube movies' | |
241 | _VALID_URL = r'https?://rutube\.ru/metainfo/tv/(?P<id>\d+)' | |
242 | ||
243 | _MOVIE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/?format=json' | |
244 | _PAGE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/video?page=%s&format=json' | |
245 | ||
246 | def _real_extract(self, url): | |
247 | movie_id = self._match_id(url) | |
248 | movie = self._download_json( | |
249 | self._MOVIE_TEMPLATE % movie_id, movie_id, | |
250 | 'Downloading movie JSON') | |
251 | return self._extract_playlist( | |
252 | movie_id, playlist_name=movie.get('name')) | |
253 | ||
254 | ||
255 | class RutubePersonIE(RutubePlaylistBaseIE): | |
256 | IE_NAME = 'rutube:person' | |
257 | IE_DESC = 'Rutube person videos' | |
258 | _VALID_URL = r'https?://rutube\.ru/video/person/(?P<id>\d+)' | |
259 | _TESTS = [{ | |
260 | 'url': 'http://rutube.ru/video/person/313878/', | |
261 | 'info_dict': { | |
262 | 'id': '313878', | |
263 | }, | |
264 | 'playlist_mincount': 37, | |
265 | }] | |
266 | ||
267 | _PAGE_TEMPLATE = 'http://rutube.ru/api/video/person/%s/?page=%s&format=json' | |
268 | ||
269 | ||
270 | class RutubePlaylistIE(RutubePlaylistBaseIE): | |
271 | IE_NAME = 'rutube:playlist' | |
272 | IE_DESC = 'Rutube playlists' | |
273 | _VALID_URL = r'https?://rutube\.ru/(?:video|(?:play/)?embed)/[\da-z]{32}/\?.*?\bpl_id=(?P<id>\d+)' | |
274 | _TESTS = [{ | |
275 | 'url': 'https://rutube.ru/video/cecd58ed7d531fc0f3d795d51cee9026/?pl_id=3097&pl_type=tag', | |
276 | 'info_dict': { | |
277 | 'id': '3097', | |
278 | }, | |
279 | 'playlist_count': 27, | |
280 | }, { | |
281 | 'url': 'https://rutube.ru/video/10b3a03fc01d5bbcc632a2f3514e8aab/?pl_id=4252&pl_type=source', | |
282 | 'only_matching': True, | |
283 | }] | |
284 | ||
285 | _PAGE_TEMPLATE = 'http://rutube.ru/api/playlist/%s/%s/?page=%s&format=json' | |
286 | ||
287 | @classmethod | |
288 | def suitable(cls, url): | |
289 | from ..utils import int_or_none, parse_qs | |
290 | ||
291 | if not super(RutubePlaylistIE, cls).suitable(url): | |
292 | return False | |
293 | params = parse_qs(url) | |
294 | return params.get('pl_type', [None])[0] and int_or_none(params.get('pl_id', [None])[0]) | |
295 | ||
296 | def _next_page_url(self, page_num, playlist_id, item_kind): | |
297 | return self._PAGE_TEMPLATE % (item_kind, playlist_id, page_num) | |
298 | ||
299 | def _real_extract(self, url): | |
300 | qs = parse_qs(url) | |
301 | playlist_kind = qs['pl_type'][0] | |
302 | playlist_id = qs['pl_id'][0] | |
303 | return self._extract_playlist(playlist_id, item_kind=playlist_kind) | |
304 | ||
305 | ||
306 | class RutubeChannelIE(RutubePlaylistBaseIE): | |
307 | IE_NAME = 'rutube:channel' | |
308 | IE_DESC = 'Rutube channel' | |
309 | _VALID_URL = r'https?://rutube\.ru/channel/(?P<id>\d+)/videos' | |
310 | _TESTS = [{ | |
311 | 'url': 'https://rutube.ru/channel/639184/videos/', | |
312 | 'info_dict': { | |
313 | 'id': '639184', | |
314 | }, | |
315 | 'playlist_mincount': 133, | |
316 | }] | |
317 | ||
318 | _PAGE_TEMPLATE = 'http://rutube.ru/api/video/person/%s/?page=%s&format=json' |