]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/rutube.py
debian/copyright: Update my copyright years.
[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 ExtractorError,
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/(?P<id>[\da-z]{32})'
21
22 _TEST = {
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 },
34 'params': {
35 # It requires ffmpeg (m3u8 download)
36 'skip_download': True,
37 },
38 }
39
40 def _real_extract(self, url):
41 video_id = self._match_id(url)
42 video = self._download_json(
43 'http://rutube.ru/api/video/%s/?format=json' % video_id,
44 video_id, 'Downloading video JSON')
45
46 # Some videos don't have the author field
47 author = video.get('author') or {}
48
49 options = self._download_json(
50 'http://rutube.ru/api/play/options/%s/?format=json' % video_id,
51 video_id, 'Downloading options JSON')
52
53 m3u8_url = options['video_balancer'].get('m3u8')
54 if m3u8_url is None:
55 raise ExtractorError('Couldn\'t find m3u8 manifest url')
56 formats = self._extract_m3u8_formats(m3u8_url, video_id, ext='mp4')
57
58 return {
59 'id': video['id'],
60 'title': video['title'],
61 'description': video['description'],
62 'duration': video['duration'],
63 'view_count': video['hits'],
64 'formats': formats,
65 'thumbnail': video['thumbnail_url'],
66 'uploader': author.get('name'),
67 'uploader_id': compat_str(author['id']) if author else None,
68 'upload_date': unified_strdate(video['created_ts']),
69 'age_limit': 18 if video['is_adult'] else 0,
70 }
71
72
73 class RutubeEmbedIE(InfoExtractor):
74 IE_NAME = 'rutube:embed'
75 IE_DESC = 'Rutube embedded videos'
76 _VALID_URL = 'https?://rutube\.ru/video/embed/(?P<id>[0-9]+)'
77
78 _TEST = {
79 'url': 'http://rutube.ru/video/embed/6722881?vk_puid37=&vk_puid38=',
80 'info_dict': {
81 'id': 'a10e53b86e8f349080f718582ce4c661',
82 'ext': 'mp4',
83 'upload_date': '20131223',
84 'uploader_id': '297833',
85 'description': 'Видео группы ★http://vk.com/foxkidsreset★ музей Fox Kids и Jetix<br/><br/> восстановлено и сделано в шикоформате subziro89 http://vk.com/subziro89',
86 'uploader': 'subziro89 ILya',
87 'title': 'Мистический городок Эйри в Индиан 5 серия озвучка subziro89',
88 },
89 'params': {
90 'skip_download': 'Requires ffmpeg',
91 },
92 }
93
94 def _real_extract(self, url):
95 embed_id = self._match_id(url)
96 webpage = self._download_webpage(url, embed_id)
97
98 canonical_url = self._html_search_regex(
99 r'<link\s+rel="canonical"\s+href="([^"]+?)"', webpage,
100 'Canonical URL')
101 return self.url_result(canonical_url, 'Rutube')
102
103
104 class RutubeChannelIE(InfoExtractor):
105 IE_NAME = 'rutube:channel'
106 IE_DESC = 'Rutube channels'
107 _VALID_URL = r'http://rutube\.ru/tags/video/(?P<id>\d+)'
108 _TESTS = [{
109 'url': 'http://rutube.ru/tags/video/1800/',
110 'info_dict': {
111 'id': '1800',
112 },
113 'playlist_mincount': 68,
114 }]
115
116 _PAGE_TEMPLATE = 'http://rutube.ru/api/tags/video/%s/?page=%s&format=json'
117
118 def _extract_videos(self, channel_id, channel_title=None):
119 entries = []
120 for pagenum in itertools.count(1):
121 page = self._download_json(
122 self._PAGE_TEMPLATE % (channel_id, pagenum),
123 channel_id, 'Downloading page %s' % pagenum)
124 results = page['results']
125 if not results:
126 break
127 entries.extend(self.url_result(result['video_url'], 'Rutube') for result in results)
128 if not page['has_next']:
129 break
130 return self.playlist_result(entries, channel_id, channel_title)
131
132 def _real_extract(self, url):
133 mobj = re.match(self._VALID_URL, url)
134 channel_id = mobj.group('id')
135 return self._extract_videos(channel_id)
136
137
138 class RutubeMovieIE(RutubeChannelIE):
139 IE_NAME = 'rutube:movie'
140 IE_DESC = 'Rutube movies'
141 _VALID_URL = r'http://rutube\.ru/metainfo/tv/(?P<id>\d+)'
142 _TESTS = []
143
144 _MOVIE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/?format=json'
145 _PAGE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/video?page=%s&format=json'
146
147 def _real_extract(self, url):
148 movie_id = self._match_id(url)
149 movie = self._download_json(
150 self._MOVIE_TEMPLATE % movie_id, movie_id,
151 'Downloading movie JSON')
152 movie_name = movie['name']
153 return self._extract_videos(movie_id, movie_name)
154
155
156 class RutubePersonIE(RutubeChannelIE):
157 IE_NAME = 'rutube:person'
158 IE_DESC = 'Rutube person videos'
159 _VALID_URL = r'http://rutube\.ru/video/person/(?P<id>\d+)'
160 _TESTS = [{
161 'url': 'http://rutube.ru/video/person/313878/',
162 'info_dict': {
163 'id': '313878',
164 },
165 'playlist_mincount': 37,
166 }]
167
168 _PAGE_TEMPLATE = 'http://rutube.ru/api/video/person/%s/?page=%s&format=json'