3 from .common
import InfoExtractor
18 class RutubeBaseIE(InfoExtractor
):
19 def _download_api_info(self
, video_id
, query
=None):
22 query
['format'] = 'json'
23 return self
._download
_json
(
24 'http://rutube.ru/api/video/%s/' % video_id
,
25 video_id
, 'Downloading video JSON',
26 'Unable to download video JSON', query
=query
)
29 def _extract_info(video
, video_id
=None, require_title
=True):
30 title
= video
['title'] if require_title
else video
.get('title')
32 age_limit
= video
.get('is_adult')
33 if age_limit
is not None:
34 age_limit
= 18 if age_limit
is True else 0
36 uploader_id
= try_get(video
, lambda x
: x
['author']['id'])
37 category
= try_get(video
, lambda x
: x
['category']['name'])
40 'id': video
.get('id') or video_id
if video_id
else video
['id'],
42 'description': video
.get('description'),
43 'thumbnail': video
.get('thumbnail_url'),
44 'duration': int_or_none(video
.get('duration')),
45 'uploader': try_get(video
, lambda x
: x
['author']['name']),
46 'uploader_id': compat_str(uploader_id
) if uploader_id
else None,
47 'timestamp': unified_timestamp(video
.get('created_ts')),
48 'category': [category
] if category
else None,
49 'age_limit': age_limit
,
50 'view_count': int_or_none(video
.get('hits')),
51 'comment_count': int_or_none(video
.get('comments_count')),
52 'is_live': bool_or_none(video
.get('is_livestream')),
55 def _download_and_extract_info(self
, video_id
, query
=None):
56 return self
._extract
_info
(
57 self
._download
_api
_info
(video_id
, query
=query
), video_id
)
59 def _download_api_options(self
, video_id
, query
=None):
62 query
['format'] = 'json'
63 return self
._download
_json
(
64 'http://rutube.ru/api/play/options/%s/' % video_id
,
65 video_id
, 'Downloading options JSON',
66 'Unable to download options JSON',
67 headers
=self
.geo_verification_headers(), query
=query
)
69 def _extract_formats(self
, options
, video_id
):
71 for format_id
, format_url
in options
['video_balancer'].items():
72 ext
= determine_ext(format_url
)
74 formats
.extend(self
._extract
_m
3u8_formats
(
75 format_url
, video_id
, 'mp4', m3u8_id
=format_id
, fatal
=False))
77 formats
.extend(self
._extract
_f
4m
_formats
(
78 format_url
, video_id
, f4m_id
=format_id
, fatal
=False))
82 'format_id': format_id
,
84 self
._sort
_formats
(formats
)
87 def _download_and_extract_formats(self
, video_id
, query
=None):
88 return self
._extract
_formats
(
89 self
._download
_api
_options
(video_id
, query
=query
), video_id
)
92 class RutubeIE(RutubeBaseIE
):
94 IE_DESC
= 'Rutube videos'
95 _VALID_URL
= r
'https?://rutube\.ru/(?:video|(?:play/)?embed)/(?P<id>[\da-z]{32})'
96 _EMBED_REGEX
= [r
'<iframe[^>]+?src=(["\'])(?P
<url
>(?
:https?
:)?
//rutube\
.ru
/(?
:play
/)?embed
/[\da
-z
]{32}
.*?
)\
1']
99 'url
': 'http
://rutube
.ru
/video
/3eac3b4561676c17df9132a9a1e62e3e
/',
100 'md5
': '1d24f180fac7a02f3900712e5a5764d6
',
102 'id': '3eac3b4561676c17df9132a9a1e62e3e
',
104 'title
': 'Раненный кенгуру забежал в аптеку
',
105 'description
': 'http
://www
.ntdtv
.ru
',
107 'uploader
': 'NTDRussian
',
108 'uploader_id
': '29790',
109 'timestamp
': 1381943602,
110 'upload_date
': '20131016',
114 'url
': 'http
://rutube
.ru
/play
/embed
/a10e53b86e8f349080f718582ce4c661
',
115 'only_matching
': True,
117 'url
': 'http
://rutube
.ru
/embed
/a10e53b86e8f349080f718582ce4c661
',
118 'only_matching
': True,
120 'url
': 'http
://rutube
.ru
/video
/3eac3b4561676c17df9132a9a1e62e3e
/?pl_id
=4252',
121 'only_matching
': True,
123 'url
': 'https
://rutube
.ru
/video
/10b3a03fc01d5bbcc632a2f3514e8aab
/?pl_type
=source
',
124 'only_matching
': True,
128 def suitable(cls, url):
129 return False if RutubePlaylistIE.suitable(url) else super(RutubeIE, cls).suitable(url)
131 def _real_extract(self, url):
132 video_id = self._match_id(url)
133 info = self._download_and_extract_info(video_id)
134 info['formats
'] = self._download_and_extract_formats(video_id)
138 class RutubeEmbedIE(RutubeBaseIE):
139 IE_NAME = 'rutube
:embed
'
140 IE_DESC = 'Rutube embedded videos
'
141 _VALID_URL = r'https?
://rutube\
.ru
/(?
:video|play
)/embed
/(?P
<id>[0-9]+)'
144 'url
': 'http
://rutube
.ru
/video
/embed
/6722881?vk_puid37
=&vk_puid38
=',
146 'id': 'a10e53b86e8f349080f718582ce4c661
',
148 'timestamp
': 1387830582,
149 'upload_date
': '20131223',
150 'uploader_id
': '297833',
151 'description
': 'Видео группы ★http
://vk
.com
/foxkidsreset★ музей Fox Kids и Jetix
<br
/><br
/> восстановлено и сделано в шикоформате subziro89 http
://vk
.com
/subziro89
',
152 'uploader
': 'subziro89 ILya
',
153 'title
': 'Мистический городок Эйри в Индиан
5 серия озвучка subziro89
',
156 'skip_download
': True,
159 'url
': 'http
://rutube
.ru
/play
/embed
/8083783',
160 'only_matching
': True,
163 'url
': 'https
://rutube
.ru
/play
/embed
/10631925?p
=IbAigKqWd1do4mjaM5XLIQ
',
164 'only_matching
': True,
167 def _real_extract(self, url):
168 embed_id = self._match_id(url)
169 # Query may contain private videos token and should be passed to API
170 # requests (see #19163)
171 query = parse_qs(url)
172 options = self._download_api_options(embed_id, query)
173 video_id = options['effective_video
']
174 formats = self._extract_formats(options, video_id)
175 info = self._download_and_extract_info(video_id, query)
177 'extractor_key
': 'Rutube
',
183 class RutubePlaylistBaseIE(RutubeBaseIE):
184 def _next_page_url(self, page_num, playlist_id, *args, **kwargs):
185 return self._PAGE_TEMPLATE % (playlist_id, page_num)
187 def _entries(self, playlist_id, *args, **kwargs):
189 for pagenum in itertools.count(1):
190 page = self._download_json(
191 next_page_url or self._next_page_url(
192 pagenum, playlist_id, *args, **kwargs),
193 playlist_id, 'Downloading page
%s' % pagenum)
195 results = page.get('results
')
196 if not results or not isinstance(results, list):
199 for result in results:
200 video_url = url_or_none(result.get('video_url
'))
203 entry = self._extract_info(result, require_title=False)
207 'ie_key
': RutubeIE.ie_key(),
211 next_page_url = page.get('next
')
212 if not next_page_url or not page.get('has_next
'):
215 def _extract_playlist(self, playlist_id, *args, **kwargs):
216 return self.playlist_result(
217 self._entries(playlist_id, *args, **kwargs),
218 playlist_id, kwargs.get('playlist_name
'))
220 def _real_extract(self, url):
221 return self._extract_playlist(self._match_id(url))
224 class RutubeTagsIE(RutubePlaylistBaseIE):
225 IE_NAME = 'rutube
:tags
'
226 IE_DESC = 'Rutube tags
'
227 _VALID_URL = r'https?
://rutube\
.ru
/tags
/video
/(?P
<id>\d
+)'
229 'url
': 'http
://rutube
.ru
/tags
/video
/1800/',
233 'playlist_mincount
': 68,
236 _PAGE_TEMPLATE = 'http
://rutube
.ru
/api
/tags
/video
/%s/?page
=%s&format
=json
'
239 class RutubeMovieIE(RutubePlaylistBaseIE):
240 IE_NAME = 'rutube
:movie
'
241 IE_DESC = 'Rutube movies
'
242 _VALID_URL = r'https?
://rutube\
.ru
/metainfo
/tv
/(?P
<id>\d
+)'
244 _MOVIE_TEMPLATE = 'http
://rutube
.ru
/api
/metainfo
/tv
/%s/?format
=json
'
245 _PAGE_TEMPLATE = 'http
://rutube
.ru
/api
/metainfo
/tv
/%s/video?page
=%s&format
=json
'
247 def _real_extract(self, url):
248 movie_id = self._match_id(url)
249 movie = self._download_json(
250 self._MOVIE_TEMPLATE % movie_id, movie_id,
251 'Downloading movie JSON
')
252 return self._extract_playlist(
253 movie_id, playlist_name=movie.get('name
'))
256 class RutubePersonIE(RutubePlaylistBaseIE):
257 IE_NAME = 'rutube
:person
'
258 IE_DESC = 'Rutube person videos
'
259 _VALID_URL = r'https?
://rutube\
.ru
/video
/person
/(?P
<id>\d
+)'
261 'url
': 'http
://rutube
.ru
/video
/person
/313878/',
265 'playlist_mincount
': 37,
268 _PAGE_TEMPLATE = 'http
://rutube
.ru
/api
/video
/person
/%s/?page
=%s&format
=json
'
271 class RutubePlaylistIE(RutubePlaylistBaseIE):
272 IE_NAME = 'rutube
:playlist
'
273 IE_DESC = 'Rutube playlists
'
274 _VALID_URL = r'https?
://rutube\
.ru
/(?
:video|
(?
:play
/)?embed
)/[\da
-z
]{32}
/\?.*?
\bpl
_id
=(?P
<id>\d
+)'
276 'url
': 'https
://rutube
.ru
/video
/cecd58ed7d531fc0f3d795d51cee9026
/?pl_id
=3097&pl_type
=tag
',
280 'playlist_count
': 27,
282 'url
': 'https
://rutube
.ru
/video
/10b3a03fc01d5bbcc632a2f3514e8aab
/?pl_id
=4252&pl_type
=source
',
283 'only_matching
': True,
286 _PAGE_TEMPLATE = 'http
://rutube
.ru
/api
/playlist
/%s/%s/?page
=%s&format
=json
'
289 def suitable(cls, url):
290 from ..utils import int_or_none, parse_qs
292 if not super(RutubePlaylistIE, cls).suitable(url):
294 params = parse_qs(url)
295 return params.get('pl_type
', [None])[0] and int_or_none(params.get('pl_id
', [None])[0])
297 def _next_page_url(self, page_num, playlist_id, item_kind):
298 return self._PAGE_TEMPLATE % (item_kind, playlist_id, page_num)
300 def _real_extract(self, url):
302 playlist_kind = qs['pl_type
'][0]
303 playlist_id = qs['pl_id
'][0]
304 return self._extract_playlist(playlist_id, item_kind=playlist_kind)
307 class RutubeChannelIE(RutubePlaylistBaseIE):
308 IE_NAME = 'rutube
:channel
'
309 IE_DESC = 'Rutube channel
'
310 _VALID_URL = r'https?
://rutube\
.ru
/channel
/(?P
<id>\d
+)/videos
'
312 'url
': 'https
://rutube
.ru
/channel
/639184/videos
/',
316 'playlist_mincount
': 133,
319 _PAGE_TEMPLATE = 'http
://rutube
.ru
/api
/video
/person
/%s/?page
=%s&format
=json
'