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