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