]>
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, |
1cc79574 PH |
10 | ) |
11 | from ..utils import ( | |
41371968 | 12 | determine_ext, |
1cc79574 | 13 | unified_strdate, |
bfd14b1b JMF |
14 | ) |
15 | ||
16 | ||
17 | class RutubeIE(InfoExtractor): | |
1547c8cc | 18 | IE_NAME = 'rutube' |
37e3b90d | 19 | IE_DESC = 'Rutube videos' |
bc82f228 | 20 | _VALID_URL = r'https?://rutube\.ru/(?:video|(?:play/)?embed)/(?P<id>[\da-z]{32})' |
bfd14b1b | 21 | |
2d3b7027 | 22 | _TESTS = [{ |
1547c8cc | 23 | 'url': 'http://rutube.ru/video/3eac3b4561676c17df9132a9a1e62e3e/', |
1547c8cc | 24 | 'info_dict': { |
c72477bd S |
25 | 'id': '3eac3b4561676c17df9132a9a1e62e3e', |
26 | 'ext': 'mp4', | |
1547c8cc | 27 | 'title': 'Раненный кенгуру забежал в аптеку', |
00ff8f92 | 28 | 'description': 'http://www.ntdtv.ru ', |
29 | 'duration': 80, | |
1547c8cc | 30 | 'uploader': 'NTDRussian', |
31 | 'uploader_id': '29790', | |
00ff8f92 | 32 | 'upload_date': '20131016', |
c8d1be77 | 33 | 'age_limit': 0, |
bfd14b1b | 34 | }, |
1547c8cc | 35 | 'params': { |
bfd14b1b | 36 | # It requires ffmpeg (m3u8 download) |
1547c8cc | 37 | 'skip_download': True, |
bfd14b1b | 38 | }, |
2d3b7027 S |
39 | }, { |
40 | 'url': 'http://rutube.ru/play/embed/a10e53b86e8f349080f718582ce4c661', | |
41 | 'only_matching': True, | |
bc82f228 S |
42 | }, { |
43 | 'url': 'http://rutube.ru/embed/a10e53b86e8f349080f718582ce4c661', | |
44 | 'only_matching': True, | |
2d3b7027 | 45 | }] |
bfd14b1b | 46 | |
eb3079b6 S |
47 | @staticmethod |
48 | def _extract_urls(webpage): | |
49 | return [mobj.group('url') for mobj in re.finditer( | |
50 | r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//rutube\.ru/embed/[\da-z]{32}.*?)\1', | |
51 | webpage)] | |
52 | ||
bfd14b1b | 53 | def _real_extract(self, url): |
1cc79574 | 54 | video_id = self._match_id(url) |
18c95c1a | 55 | video = self._download_json( |
c72477bd S |
56 | 'http://rutube.ru/api/video/%s/?format=json' % video_id, |
57 | video_id, 'Downloading video JSON') | |
18c95c1a | 58 | |
bfd14b1b | 59 | # Some videos don't have the author field |
d7f1e7c8 S |
60 | author = video.get('author') or {} |
61 | ||
62 | options = self._download_json( | |
68905742 | 63 | 'http://rutube.ru/api/play/options/%s/?format=json' % video_id, |
d7f1e7c8 S |
64 | video_id, 'Downloading options JSON') |
65 | ||
41371968 S |
66 | formats = [] |
67 | for format_id, format_url in options['video_balancer'].items(): | |
68 | ext = determine_ext(format_url) | |
41371968 | 69 | if ext == 'm3u8': |
7e5edcfd S |
70 | formats.extend(self._extract_m3u8_formats( |
71 | format_url, video_id, 'mp4', m3u8_id=format_id, fatal=False)) | |
41371968 | 72 | elif ext == 'f4m': |
7e5edcfd S |
73 | formats.extend(self._extract_f4m_formats( |
74 | format_url, video_id, f4m_id=format_id, fatal=False)) | |
41371968 S |
75 | else: |
76 | formats.append({ | |
77 | 'url': format_url, | |
78 | 'format_id': format_id, | |
79 | }) | |
80 | self._sort_formats(formats) | |
bfd14b1b JMF |
81 | |
82 | return { | |
a2fb2a21 | 83 | 'id': video['id'], |
84 | 'title': video['title'], | |
85 | 'description': video['description'], | |
86 | 'duration': video['duration'], | |
87 | 'view_count': video['hits'], | |
05177b34 | 88 | 'formats': formats, |
a2fb2a21 | 89 | 'thumbnail': video['thumbnail_url'], |
bfd14b1b JMF |
90 | 'uploader': author.get('name'), |
91 | 'uploader_id': compat_str(author['id']) if author else None, | |
a2fb2a21 | 92 | 'upload_date': unified_strdate(video['created_ts']), |
93 | 'age_limit': 18 if video['is_adult'] else 0, | |
bfd14b1b | 94 | } |
1547c8cc | 95 | |
96 | ||
7a1818c9 PH |
97 | class RutubeEmbedIE(InfoExtractor): |
98 | IE_NAME = 'rutube:embed' | |
99 | IE_DESC = 'Rutube embedded videos' | |
25042f73 | 100 | _VALID_URL = r'https?://rutube\.ru/(?:video|play)/embed/(?P<id>[0-9]+)' |
7a1818c9 | 101 | |
f52183a8 | 102 | _TESTS = [{ |
7a1818c9 PH |
103 | 'url': 'http://rutube.ru/video/embed/6722881?vk_puid37=&vk_puid38=', |
104 | 'info_dict': { | |
105 | 'id': 'a10e53b86e8f349080f718582ce4c661', | |
106 | 'ext': 'mp4', | |
107 | 'upload_date': '20131223', | |
108 | 'uploader_id': '297833', | |
109 | 'description': 'Видео группы ★http://vk.com/foxkidsreset★ музей Fox Kids и Jetix<br/><br/> восстановлено и сделано в шикоформате subziro89 http://vk.com/subziro89', | |
110 | 'uploader': 'subziro89 ILya', | |
111 | 'title': 'Мистический городок Эйри в Индиан 5 серия озвучка subziro89', | |
112 | }, | |
113 | 'params': { | |
114 | 'skip_download': 'Requires ffmpeg', | |
115 | }, | |
f52183a8 S |
116 | }, { |
117 | 'url': 'http://rutube.ru/play/embed/8083783', | |
118 | 'only_matching': True, | |
119 | }] | |
7a1818c9 PH |
120 | |
121 | def _real_extract(self, url): | |
122 | embed_id = self._match_id(url) | |
123 | webpage = self._download_webpage(url, embed_id) | |
124 | ||
125 | canonical_url = self._html_search_regex( | |
126 | r'<link\s+rel="canonical"\s+href="([^"]+?)"', webpage, | |
127 | 'Canonical URL') | |
128 | return self.url_result(canonical_url, 'Rutube') | |
129 | ||
130 | ||
1547c8cc | 131 | class RutubeChannelIE(InfoExtractor): |
132 | IE_NAME = 'rutube:channel' | |
37e3b90d | 133 | IE_DESC = 'Rutube channels' |
5886b38d | 134 | _VALID_URL = r'https?://rutube\.ru/tags/video/(?P<id>\d+)' |
22a6f150 PH |
135 | _TESTS = [{ |
136 | 'url': 'http://rutube.ru/tags/video/1800/', | |
137 | 'info_dict': { | |
138 | 'id': '1800', | |
139 | }, | |
140 | 'playlist_mincount': 68, | |
141 | }] | |
1547c8cc | 142 | |
143 | _PAGE_TEMPLATE = 'http://rutube.ru/api/tags/video/%s/?page=%s&format=json' | |
144 | ||
145 | def _extract_videos(self, channel_id, channel_title=None): | |
146 | entries = [] | |
147 | for pagenum in itertools.count(1): | |
18c95c1a | 148 | page = self._download_json( |
37e3b90d PH |
149 | self._PAGE_TEMPLATE % (channel_id, pagenum), |
150 | channel_id, 'Downloading page %s' % pagenum) | |
1547c8cc | 151 | results = page['results'] |
37e3b90d PH |
152 | if not results: |
153 | break | |
a2fb2a21 | 154 | entries.extend(self.url_result(result['video_url'], 'Rutube') for result in results) |
37e3b90d PH |
155 | if not page['has_next']: |
156 | break | |
1547c8cc | 157 | return self.playlist_result(entries, channel_id, channel_title) |
158 | ||
159 | def _real_extract(self, url): | |
160 | mobj = re.match(self._VALID_URL, url) | |
161 | channel_id = mobj.group('id') | |
162 | return self._extract_videos(channel_id) | |
163 | ||
164 | ||
165 | class RutubeMovieIE(RutubeChannelIE): | |
166 | IE_NAME = 'rutube:movie' | |
37e3b90d | 167 | IE_DESC = 'Rutube movies' |
5886b38d | 168 | _VALID_URL = r'https?://rutube\.ru/metainfo/tv/(?P<id>\d+)' |
22a6f150 | 169 | _TESTS = [] |
1547c8cc | 170 | |
171 | _MOVIE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/?format=json' | |
172 | _PAGE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/video?page=%s&format=json' | |
173 | ||
174 | def _real_extract(self, url): | |
1cc79574 | 175 | movie_id = self._match_id(url) |
18c95c1a | 176 | movie = self._download_json( |
37e3b90d PH |
177 | self._MOVIE_TEMPLATE % movie_id, movie_id, |
178 | 'Downloading movie JSON') | |
1547c8cc | 179 | movie_name = movie['name'] |
e3a9f32f | 180 | return self._extract_videos(movie_id, movie_name) |
181 | ||
182 | ||
183 | class RutubePersonIE(RutubeChannelIE): | |
184 | IE_NAME = 'rutube:person' | |
185 | IE_DESC = 'Rutube person videos' | |
5886b38d | 186 | _VALID_URL = r'https?://rutube\.ru/video/person/(?P<id>\d+)' |
22a6f150 PH |
187 | _TESTS = [{ |
188 | 'url': 'http://rutube.ru/video/person/313878/', | |
189 | 'info_dict': { | |
190 | 'id': '313878', | |
191 | }, | |
192 | 'playlist_mincount': 37, | |
193 | }] | |
e3a9f32f | 194 | |
37e3b90d | 195 | _PAGE_TEMPLATE = 'http://rutube.ru/api/video/person/%s/?page=%s&format=json' |