]>
Commit | Line | Data |
---|---|---|
77aa6b32 | 1 | # encoding: utf-8 |
ceb2b7d2 | 2 | from __future__ import unicode_literals |
77aa6b32 | 3 | |
4 | import re | |
5 | import json | |
6 | ||
7 | from .common import InfoExtractor | |
1cc79574 | 8 | from ..utils import ( |
77aa6b32 | 9 | ExtractorError, |
ab3176af | 10 | int_or_none, |
5c2266df | 11 | sanitized_Request, |
77aa6b32 | 12 | ) |
13 | ||
14 | ||
15 | class IviIE(InfoExtractor): | |
ceb2b7d2 | 16 | IE_DESC = 'ivi.ru' |
17 | IE_NAME = 'ivi' | |
63be3b89 | 18 | _VALID_URL = r'https?://(?:www\.)?ivi\.ru/(?:watch/(?:[^/]+/)?|video/player\?.*?videoId=)(?P<id>\d+)' |
77aa6b32 | 19 | |
20 | _TESTS = [ | |
21 | # Single movie | |
22 | { | |
ceb2b7d2 | 23 | 'url': 'http://www.ivi.ru/watch/53141', |
ceb2b7d2 | 24 | 'md5': '6ff5be2254e796ed346251d117196cf4', |
25 | 'info_dict': { | |
84dd7031 S |
26 | 'id': '53141', |
27 | 'ext': 'mp4', | |
ceb2b7d2 | 28 | 'title': 'Иван Васильевич меняет профессию', |
29 | 'description': 'md5:b924063ea1677c8fe343d8a72ac2195f', | |
30 | 'duration': 5498, | |
ab3176af | 31 | 'thumbnail': 're:^https?://.*\.jpg$', |
77aa6b32 | 32 | }, |
ceb2b7d2 | 33 | 'skip': 'Only works from Russia', |
77aa6b32 | 34 | }, |
dfb1b146 | 35 | # Serial's series |
77aa6b32 | 36 | { |
6ebb46c1 S |
37 | 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa/9549', |
38 | 'md5': '221f56b35e3ed815fde2df71032f4b3e', | |
ceb2b7d2 | 39 | 'info_dict': { |
6ebb46c1 | 40 | 'id': '9549', |
84dd7031 | 41 | 'ext': 'mp4', |
ab3176af S |
42 | 'title': 'Двое из ларца - Дело Гольдберга (1 часть)', |
43 | 'series': 'Двое из ларца', | |
1463c5b9 S |
44 | 'season': 'Сезон 1', |
45 | 'season_number': 1, | |
ab3176af S |
46 | 'episode': 'Дело Гольдберга (1 часть)', |
47 | 'episode_number': 1, | |
6ebb46c1 | 48 | 'duration': 2655, |
ab3176af | 49 | 'thumbnail': 're:^https?://.*\.jpg$', |
77aa6b32 | 50 | }, |
ceb2b7d2 | 51 | 'skip': 'Only works from Russia', |
b74e86f4 | 52 | } |
77aa6b32 | 53 | ] |
ceb2b7d2 | 54 | |
77aa6b32 | 55 | # Sorted by quality |
ab3176af | 56 | _KNOWN_FORMATS = ['MP4-low-mobile', 'MP4-mobile', 'FLV-lo', 'MP4-lo', 'FLV-hi', 'MP4-hi', 'MP4-SHQ'] |
77aa6b32 | 57 | |
58 | def _real_extract(self, url): | |
63be3b89 | 59 | video_id = self._match_id(url) |
77aa6b32 | 60 | |
63be3b89 S |
61 | data = { |
62 | 'method': 'da.content.get', | |
63 | 'params': [ | |
64 | video_id, { | |
65 | 'site': 's183', | |
66 | 'referrer': 'http://www.ivi.ru/watch/%s' % video_id, | |
67 | 'contentid': video_id | |
77aa6b32 | 68 | } |
63be3b89 S |
69 | ] |
70 | } | |
77aa6b32 | 71 | |
ab3176af S |
72 | request = sanitized_Request( |
73 | 'http://api.digitalaccess.ru/api/json/', json.dumps(data)) | |
74 | video_json = self._download_json( | |
63be3b89 | 75 | request, video_id, 'Downloading video JSON') |
77aa6b32 | 76 | |
ceb2b7d2 | 77 | if 'error' in video_json: |
78 | error = video_json['error'] | |
79 | if error['origin'] == 'NoRedisValidData': | |
80 | raise ExtractorError('Video %s does not exist' % video_id, expected=True) | |
63be3b89 S |
81 | raise ExtractorError( |
82 | 'Unable to download video %s: %s' % (video_id, error['message']), | |
83 | expected=True) | |
77aa6b32 | 84 | |
ceb2b7d2 | 85 | result = video_json['result'] |
77aa6b32 | 86 | |
bf5b0a1b | 87 | formats = [{ |
ceb2b7d2 | 88 | 'url': x['url'], |
89 | 'format_id': x['content_format'], | |
ab3176af S |
90 | 'preference': self._KNOWN_FORMATS.index(x['content_format']), |
91 | } for x in result['files'] if x['content_format'] in self._KNOWN_FORMATS] | |
bf5b0a1b PH |
92 | |
93 | self._sort_formats(formats) | |
94 | ||
ceb2b7d2 | 95 | title = result['title'] |
77aa6b32 | 96 | |
ab3176af S |
97 | duration = int_or_none(result.get('duration')) |
98 | compilation = result.get('compilation') | |
99 | episode = title if compilation else None | |
100 | ||
5f6a1245 | 101 | title = '%s - %s' % (compilation, title) if compilation is not None else title |
77aa6b32 | 102 | |
ab3176af S |
103 | thumbnails = [{ |
104 | 'url': preview['url'], | |
105 | 'id': preview.get('content_format'), | |
106 | } for preview in result.get('preview', []) if preview.get('url')] | |
107 | ||
108 | webpage = self._download_webpage(url, video_id) | |
109 | ||
1463c5b9 S |
110 | season = self._search_regex( |
111 | r'<li[^>]+class="season active"[^>]*><a[^>]+>([^<]+)', | |
112 | webpage, 'season', default=None) | |
113 | season_number = int_or_none(self._search_regex( | |
114 | r'<li[^>]+class="season active"[^>]*><a[^>]+data-season(?:-index)?="(\d+)"', | |
115 | webpage, 'season number', default=None)) | |
116 | ||
ab3176af S |
117 | episode_number = int_or_none(self._search_regex( |
118 | r'<meta[^>]+itemprop="episode"[^>]*>\s*<meta[^>]+itemprop="episodeNumber"[^>]+content="(\d+)', | |
119 | webpage, 'episode number', default=None)) | |
77aa6b32 | 120 | |
ab3176af S |
121 | description = self._og_search_description(webpage, default=None) or self._html_search_meta( |
122 | 'description', webpage, 'description', default=None) | |
77aa6b32 | 123 | |
124 | return { | |
125 | 'id': video_id, | |
126 | 'title': title, | |
ab3176af | 127 | 'series': compilation, |
1463c5b9 S |
128 | 'season': season, |
129 | 'season_number': season_number, | |
ab3176af S |
130 | 'episode': episode, |
131 | 'episode_number': episode_number, | |
132 | 'thumbnails': thumbnails, | |
77aa6b32 | 133 | 'description': description, |
134 | 'duration': duration, | |
77aa6b32 | 135 | 'formats': formats, |
136 | } | |
137 | ||
138 | ||
139 | class IviCompilationIE(InfoExtractor): | |
ceb2b7d2 | 140 | IE_DESC = 'ivi.ru compilations' |
141 | IE_NAME = 'ivi:compilation' | |
84dd7031 | 142 | _VALID_URL = r'https?://(?:www\.)?ivi\.ru/watch/(?!\d+)(?P<compilationid>[a-z\d_-]+)(?:/season(?P<seasonid>\d+))?$' |
22a6f150 PH |
143 | _TESTS = [{ |
144 | 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa', | |
145 | 'info_dict': { | |
146 | 'id': 'dvoe_iz_lartsa', | |
147 | 'title': 'Двое из ларца (2006 - 2008)', | |
148 | }, | |
149 | 'playlist_mincount': 24, | |
150 | }, { | |
151 | 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa/season1', | |
152 | 'info_dict': { | |
153 | 'id': 'dvoe_iz_lartsa/season1', | |
154 | 'title': 'Двое из ларца (2006 - 2008) 1 сезон', | |
155 | }, | |
156 | 'playlist_mincount': 12, | |
157 | }] | |
77aa6b32 | 158 | |
159 | def _extract_entries(self, html, compilation_id): | |
c6270b2e S |
160 | return [ |
161 | self.url_result( | |
162 | 'http://www.ivi.ru/watch/%s/%s' % (compilation_id, serie), IviIE.ie_key()) | |
163 | for serie in re.findall( | |
164 | r'<a href="/watch/%s/(\d+)"[^>]+data-id="\1"' % compilation_id, html)] | |
77aa6b32 | 165 | |
166 | def _real_extract(self, url): | |
167 | mobj = re.match(self._VALID_URL, url) | |
168 | compilation_id = mobj.group('compilationid') | |
169 | season_id = mobj.group('seasonid') | |
170 | ||
5f6a1245 | 171 | if season_id is not None: # Season link |
c6270b2e S |
172 | season_page = self._download_webpage( |
173 | url, compilation_id, 'Downloading season %s web page' % season_id) | |
77aa6b32 | 174 | playlist_id = '%s/season%s' % (compilation_id, season_id) |
ceb2b7d2 | 175 | playlist_title = self._html_search_meta('title', season_page, 'title') |
77aa6b32 | 176 | entries = self._extract_entries(season_page, compilation_id) |
5f6a1245 | 177 | else: # Compilation link |
ceb2b7d2 | 178 | compilation_page = self._download_webpage(url, compilation_id, 'Downloading compilation web page') |
77aa6b32 | 179 | playlist_id = compilation_id |
ceb2b7d2 | 180 | playlist_title = self._html_search_meta('title', compilation_page, 'title') |
c6270b2e S |
181 | seasons = re.findall( |
182 | r'<a href="/watch/%s/season(\d+)' % compilation_id, compilation_page) | |
183 | if not seasons: # No seasons in this compilation | |
77aa6b32 | 184 | entries = self._extract_entries(compilation_page, compilation_id) |
185 | else: | |
186 | entries = [] | |
187 | for season_id in seasons: | |
ceb2b7d2 | 188 | season_page = self._download_webpage( |
189 | 'http://www.ivi.ru/watch/%s/season%s' % (compilation_id, season_id), | |
190 | compilation_id, 'Downloading season %s web page' % season_id) | |
77aa6b32 | 191 | entries.extend(self._extract_entries(season_page, compilation_id)) |
192 | ||
5f6a1245 | 193 | return self.playlist_result(entries, playlist_id, playlist_title) |