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