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