]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/rutube.py
[brightcove] Encode object_str with utf-8
[yt-dlp.git] / youtube_dl / extractor / rutube.py
CommitLineData
bfd14b1b 1# encoding: utf-8
1547c8cc 2from __future__ import unicode_literals
3
bfd14b1b
JMF
4import re
5import json
1547c8cc 6import itertools
bfd14b1b
JMF
7
8from .common import InfoExtractor
9from ..utils import (
bfd14b1b 10 compat_str,
a2fb2a21 11 unified_strdate,
bfd14b1b
JMF
12 ExtractorError,
13)
14
15
16class RutubeIE(InfoExtractor):
1547c8cc 17 IE_NAME = 'rutube'
37e3b90d 18 IE_DESC = 'Rutube videos'
e3a9f32f 19 _VALID_URL = r'https?://rutube\.ru/video/(?P<id>[\da-z]{32})'
bfd14b1b
JMF
20
21 _TEST = {
1547c8cc 22 'url': 'http://rutube.ru/video/3eac3b4561676c17df9132a9a1e62e3e/',
1547c8cc 23 'info_dict': {
c72477bd
S
24 'id': '3eac3b4561676c17df9132a9a1e62e3e',
25 'ext': 'mp4',
1547c8cc 26 'title': 'Раненный кенгуру забежал в аптеку',
00ff8f92 27 'description': 'http://www.ntdtv.ru ',
28 'duration': 80,
1547c8cc 29 'uploader': 'NTDRussian',
30 'uploader_id': '29790',
00ff8f92 31 'upload_date': '20131016',
bfd14b1b 32 },
1547c8cc 33 'params': {
bfd14b1b 34 # It requires ffmpeg (m3u8 download)
1547c8cc 35 'skip_download': True,
bfd14b1b
JMF
36 },
37 }
38
bfd14b1b
JMF
39 def _real_extract(self, url):
40 mobj = re.match(self._VALID_URL, url)
e3a9f32f 41 video_id = mobj.group('id')
a2fb2a21 42
c72477bd
S
43 api_response = self._download_webpage(
44 'http://rutube.ru/api/video/%s/?format=json' % video_id,
45 video_id, 'Downloading video JSON')
a2fb2a21 46 video = json.loads(api_response)
47
c72477bd
S
48 api_response = self._download_webpage(
49 'http://rutube.ru/api/play/trackinfo/%s/?format=json' % video_id,
50 video_id, 'Downloading trackinfo JSON')
a2fb2a21 51 trackinfo = json.loads(api_response)
52
bfd14b1b
JMF
53 # Some videos don't have the author field
54 author = trackinfo.get('author') or {}
55 m3u8_url = trackinfo['video_balancer'].get('m3u8')
56 if m3u8_url is None:
1547c8cc 57 raise ExtractorError('Couldn\'t find m3u8 manifest url')
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'],
bfd14b1b
JMF
65 'url': m3u8_url,
66 'ext': 'mp4',
a2fb2a21 67 'thumbnail': video['thumbnail_url'],
bfd14b1b
JMF
68 'uploader': author.get('name'),
69 'uploader_id': compat_str(author['id']) if author else None,
a2fb2a21 70 'upload_date': unified_strdate(video['created_ts']),
71 'age_limit': 18 if video['is_adult'] else 0,
bfd14b1b 72 }
1547c8cc 73
74
75class RutubeChannelIE(InfoExtractor):
76 IE_NAME = 'rutube:channel'
37e3b90d 77 IE_DESC = 'Rutube channels'
1547c8cc 78 _VALID_URL = r'http://rutube\.ru/tags/video/(?P<id>\d+)'
79
80 _PAGE_TEMPLATE = 'http://rutube.ru/api/tags/video/%s/?page=%s&format=json'
81
82 def _extract_videos(self, channel_id, channel_title=None):
83 entries = []
84 for pagenum in itertools.count(1):
37e3b90d
PH
85 api_response = self._download_webpage(
86 self._PAGE_TEMPLATE % (channel_id, pagenum),
87 channel_id, 'Downloading page %s' % pagenum)
a2fb2a21 88 page = json.loads(api_response)
1547c8cc 89 results = page['results']
37e3b90d
PH
90 if not results:
91 break
a2fb2a21 92 entries.extend(self.url_result(result['video_url'], 'Rutube') for result in results)
37e3b90d
PH
93 if not page['has_next']:
94 break
1547c8cc 95 return self.playlist_result(entries, channel_id, channel_title)
96
97 def _real_extract(self, url):
98 mobj = re.match(self._VALID_URL, url)
99 channel_id = mobj.group('id')
100 return self._extract_videos(channel_id)
101
102
103class RutubeMovieIE(RutubeChannelIE):
104 IE_NAME = 'rutube:movie'
37e3b90d 105 IE_DESC = 'Rutube movies'
1547c8cc 106 _VALID_URL = r'http://rutube\.ru/metainfo/tv/(?P<id>\d+)'
107
108 _MOVIE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/?format=json'
109 _PAGE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/video?page=%s&format=json'
110
111 def _real_extract(self, url):
112 mobj = re.match(self._VALID_URL, url)
113 movie_id = mobj.group('id')
37e3b90d
PH
114 api_response = self._download_webpage(
115 self._MOVIE_TEMPLATE % movie_id, movie_id,
116 'Downloading movie JSON')
a2fb2a21 117 movie = json.loads(api_response)
1547c8cc 118 movie_name = movie['name']
e3a9f32f 119 return self._extract_videos(movie_id, movie_name)
120
121
122class RutubePersonIE(RutubeChannelIE):
123 IE_NAME = 'rutube:person'
124 IE_DESC = 'Rutube person videos'
125 _VALID_URL = r'http://rutube\.ru/video/person/(?P<id>\d+)'
126
37e3b90d 127 _PAGE_TEMPLATE = 'http://rutube.ru/api/video/person/%s/?page=%s&format=json'