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