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