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