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