]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/rutube.py
[extractor/common] Use baseURL from f4m manifest for recursive manifest extraction
[yt-dlp.git] / youtube_dl / extractor / rutube.py
CommitLineData
bfd14b1b 1# encoding: utf-8
1547c8cc 2from __future__ import unicode_literals
3
bfd14b1b 4import re
1547c8cc 5import itertools
bfd14b1b
JMF
6
7from .common import InfoExtractor
1cc79574 8from ..compat import (
bfd14b1b 9 compat_str,
1cc79574
PH
10)
11from ..utils import (
bfd14b1b 12 ExtractorError,
1cc79574 13 unified_strdate,
bfd14b1b
JMF
14)
15
16
17class 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
54 m3u8_url = options['video_balancer'].get('m3u8')
bfd14b1b 55 if m3u8_url is None:
1547c8cc 56 raise ExtractorError('Couldn\'t find m3u8 manifest url')
05177b34 57 formats = self._extract_m3u8_formats(m3u8_url, video_id, ext='mp4')
bfd14b1b
JMF
58
59 return {
a2fb2a21 60 'id': video['id'],
61 'title': video['title'],
62 'description': video['description'],
63 'duration': video['duration'],
64 'view_count': video['hits'],
05177b34 65 'formats': formats,
a2fb2a21 66 'thumbnail': video['thumbnail_url'],
bfd14b1b
JMF
67 'uploader': author.get('name'),
68 'uploader_id': compat_str(author['id']) if author else None,
a2fb2a21 69 'upload_date': unified_strdate(video['created_ts']),
70 'age_limit': 18 if video['is_adult'] else 0,
bfd14b1b 71 }
1547c8cc 72
73
7a1818c9
PH
74class RutubeEmbedIE(InfoExtractor):
75 IE_NAME = 'rutube:embed'
76 IE_DESC = 'Rutube embedded videos'
f52183a8 77 _VALID_URL = 'https?://rutube\.ru/(?:video|play)/embed/(?P<id>[0-9]+)'
7a1818c9 78
f52183a8 79 _TESTS = [{
7a1818c9
PH
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 },
f52183a8
S
93 }, {
94 'url': 'http://rutube.ru/play/embed/8083783',
95 'only_matching': True,
96 }]
7a1818c9
PH
97
98 def _real_extract(self, url):
99 embed_id = self._match_id(url)
100 webpage = self._download_webpage(url, embed_id)
101
102 canonical_url = self._html_search_regex(
103 r'<link\s+rel="canonical"\s+href="([^"]+?)"', webpage,
104 'Canonical URL')
105 return self.url_result(canonical_url, 'Rutube')
106
107
1547c8cc 108class RutubeChannelIE(InfoExtractor):
109 IE_NAME = 'rutube:channel'
37e3b90d 110 IE_DESC = 'Rutube channels'
1547c8cc 111 _VALID_URL = r'http://rutube\.ru/tags/video/(?P<id>\d+)'
22a6f150
PH
112 _TESTS = [{
113 'url': 'http://rutube.ru/tags/video/1800/',
114 'info_dict': {
115 'id': '1800',
116 },
117 'playlist_mincount': 68,
118 }]
1547c8cc 119
120 _PAGE_TEMPLATE = 'http://rutube.ru/api/tags/video/%s/?page=%s&format=json'
121
122 def _extract_videos(self, channel_id, channel_title=None):
123 entries = []
124 for pagenum in itertools.count(1):
18c95c1a 125 page = self._download_json(
37e3b90d
PH
126 self._PAGE_TEMPLATE % (channel_id, pagenum),
127 channel_id, 'Downloading page %s' % pagenum)
1547c8cc 128 results = page['results']
37e3b90d
PH
129 if not results:
130 break
a2fb2a21 131 entries.extend(self.url_result(result['video_url'], 'Rutube') for result in results)
37e3b90d
PH
132 if not page['has_next']:
133 break
1547c8cc 134 return self.playlist_result(entries, channel_id, channel_title)
135
136 def _real_extract(self, url):
137 mobj = re.match(self._VALID_URL, url)
138 channel_id = mobj.group('id')
139 return self._extract_videos(channel_id)
140
141
142class RutubeMovieIE(RutubeChannelIE):
143 IE_NAME = 'rutube:movie'
37e3b90d 144 IE_DESC = 'Rutube movies'
1547c8cc 145 _VALID_URL = r'http://rutube\.ru/metainfo/tv/(?P<id>\d+)'
22a6f150 146 _TESTS = []
1547c8cc 147
148 _MOVIE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/?format=json'
149 _PAGE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/video?page=%s&format=json'
150
151 def _real_extract(self, url):
1cc79574 152 movie_id = self._match_id(url)
18c95c1a 153 movie = self._download_json(
37e3b90d
PH
154 self._MOVIE_TEMPLATE % movie_id, movie_id,
155 'Downloading movie JSON')
1547c8cc 156 movie_name = movie['name']
e3a9f32f 157 return self._extract_videos(movie_id, movie_name)
158
159
160class RutubePersonIE(RutubeChannelIE):
161 IE_NAME = 'rutube:person'
162 IE_DESC = 'Rutube person videos'
163 _VALID_URL = r'http://rutube\.ru/video/person/(?P<id>\d+)'
22a6f150
PH
164 _TESTS = [{
165 'url': 'http://rutube.ru/video/person/313878/',
166 'info_dict': {
167 'id': '313878',
168 },
169 'playlist_mincount': 37,
170 }]
e3a9f32f 171
37e3b90d 172 _PAGE_TEMPLATE = 'http://rutube.ru/api/video/person/%s/?page=%s&format=json'