2 from __future__ import unicode_literals
7 from .common import InfoExtractor
15 class IviIE(InfoExtractor):
18 _VALID_URL = r'https?://(?:www\.)?ivi\.(?:ru|tv)/(?:watch/(?:[^/]+/)?|video/player\?.*?videoId=)(?P<id>\d+)'
20 _GEO_COUNTRIES = ['RU']
21 _LIGHT_KEY = b'\xf1\x02\x32\xb7\xbc\x5c\x7a\xe8\xf7\x96\xc1\x33\x2b\x27\xa1\x8c'
22 _LIGHT_URL = 'https://api.ivi.ru/light/'
27 'url': 'http://www.ivi.ru/watch/53141',
28 'md5': '6ff5be2254e796ed346251d117196cf4',
32 'title': 'Иван Васильевич меняет профессию',
33 'description': 'md5:b924063ea1677c8fe343d8a72ac2195f',
35 'thumbnail': r're:^https?://.*\.jpg$',
37 'skip': 'Only works from Russia',
41 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa/9549',
42 'md5': '221f56b35e3ed815fde2df71032f4b3e',
46 'title': 'Двое из ларца - Дело Гольдберга (1 часть)',
47 'series': 'Двое из ларца',
50 'episode': 'Дело Гольдберга (1 часть)',
53 'thumbnail': r're:^https?://.*\.jpg$',
55 'skip': 'Only works from Russia',
58 # with MP4-HD720 format
59 'url': 'http://www.ivi.ru/watch/146500',
60 'md5': 'd63d35cdbfa1ea61a5eafec7cc523e1e',
65 'description': 'md5:ffca9372399976a2d260a407cc74cce6',
67 'thumbnail': r're:^https?://.*\.jpg$',
69 'skip': 'Only works from Russia',
72 'url': 'https://www.ivi.tv/watch/33560/',
73 'only_matching': True,
79 'MP4-low-mobile', 'MP4-mobile', 'FLV-lo', 'MP4-lo', 'FLV-hi', 'MP4-hi',
80 'MP4-SHQ', 'MP4-HD720', 'MP4-HD1080')
82 def _real_extract(self, url):
83 video_id = self._match_id(url)
86 'method': 'da.content.get',
90 'referrer': 'http://www.ivi.ru/watch/%s' % video_id,
97 from Crypto.Cipher import Blowfish
98 from Crypto.Hash import CMAC
100 timestamp = self._download_json(
101 self._LIGHT_URL, video_id,
102 'Downloading timestamp JSON', data=json.dumps({
103 'method': 'da.timestamp.get',
105 }).encode())['result']
110 'sign': CMAC.new(self._LIGHT_KEY, timestamp.encode() + data, Blowfish).hexdigest(),
116 video_json = self._download_json(
117 self._LIGHT_URL, video_id,
118 'Downloading video JSON', data=data, query=query)
120 error = video_json.get('error')
122 origin = error.get('origin')
123 message = error.get('message') or error.get('user_message')
124 extractor_msg = 'Unable to download video %s'
125 if origin == 'NotAllowedForLocation':
126 self.raise_geo_restricted(message, self._GEO_COUNTRIES)
127 elif origin == 'NoRedisValidData':
128 extractor_msg = 'Video %s does not exist'
130 if 'недоступен для просмотра на площадке s183' in message:
131 raise ExtractorError(
132 'pycryptodome not found. Please install it.',
134 extractor_msg += ': ' + message
135 raise ExtractorError(extractor_msg % video_id, expected=True)
137 result = video_json['result']
138 title = result['title']
140 quality = qualities(self._KNOWN_FORMATS)
143 for f in result.get('files', []):
145 content_format = f.get('content_format')
146 if not f_url or '-MDRM-' in content_format or '-FPS-' in content_format:
150 'format_id': content_format,
151 'quality': quality(content_format),
152 'filesize': int_or_none(f.get('size_in_bytes')),
154 self._sort_formats(formats)
156 compilation = result.get('compilation')
157 episode = title if compilation else None
159 title = '%s - %s' % (compilation, title) if compilation is not None else title
162 'url': preview['url'],
163 'id': preview.get('content_format'),
164 } for preview in result.get('preview', []) if preview.get('url')]
166 webpage = self._download_webpage(url, video_id)
168 season = self._search_regex(
169 r'<li[^>]+class="season active"[^>]*><a[^>]+>([^<]+)',
170 webpage, 'season', default=None)
171 season_number = int_or_none(self._search_regex(
172 r'<li[^>]+class="season active"[^>]*><a[^>]+data-season(?:-index)?="(\d+)"',
173 webpage, 'season number', default=None))
175 episode_number = int_or_none(self._search_regex(
176 r'[^>]+itemprop="episode"[^>]*>\s*<meta[^>]+itemprop="episodeNumber"[^>]+content="(\d+)',
177 webpage, 'episode number', default=None))
179 description = self._og_search_description(webpage, default=None) or self._html_search_meta(
180 'description', webpage, 'description', default=None)
185 'series': compilation,
187 'season_number': season_number,
189 'episode_number': episode_number,
190 'thumbnails': thumbnails,
191 'description': description,
192 'duration': int_or_none(result.get('duration')),
197 class IviCompilationIE(InfoExtractor):
198 IE_DESC = 'ivi.ru compilations'
199 IE_NAME = 'ivi:compilation'
200 _VALID_URL = r'https?://(?:www\.)?ivi\.ru/watch/(?!\d+)(?P<compilationid>[a-z\d_-]+)(?:/season(?P<seasonid>\d+))?$'
202 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa',
204 'id': 'dvoe_iz_lartsa',
205 'title': 'Двое из ларца (2006 - 2008)',
207 'playlist_mincount': 24,
209 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa/season1',
211 'id': 'dvoe_iz_lartsa/season1',
212 'title': 'Двое из ларца (2006 - 2008) 1 сезон',
214 'playlist_mincount': 12,
217 def _extract_entries(self, html, compilation_id):
220 'http://www.ivi.ru/watch/%s/%s' % (compilation_id, serie), IviIE.ie_key())
221 for serie in re.findall(
222 r'<a href="/watch/%s/(\d+)"[^>]+data-id="\1"' % compilation_id, html)]
224 def _real_extract(self, url):
225 mobj = re.match(self._VALID_URL, url)
226 compilation_id = mobj.group('compilationid')
227 season_id = mobj.group('seasonid')
229 if season_id is not None: # Season link
230 season_page = self._download_webpage(
231 url, compilation_id, 'Downloading season %s web page' % season_id)
232 playlist_id = '%s/season%s' % (compilation_id, season_id)
233 playlist_title = self._html_search_meta('title', season_page, 'title')
234 entries = self._extract_entries(season_page, compilation_id)
235 else: # Compilation link
236 compilation_page = self._download_webpage(url, compilation_id, 'Downloading compilation web page')
237 playlist_id = compilation_id
238 playlist_title = self._html_search_meta('title', compilation_page, 'title')
239 seasons = re.findall(
240 r'<a href="/watch/%s/season(\d+)' % compilation_id, compilation_page)
241 if not seasons: # No seasons in this compilation
242 entries = self._extract_entries(compilation_page, compilation_id)
245 for season_id in seasons:
246 season_page = self._download_webpage(
247 'http://www.ivi.ru/watch/%s/season%s' % (compilation_id, season_id),
248 compilation_id, 'Downloading season %s web page' % season_id)
249 entries.extend(self._extract_entries(season_page, compilation_id))
251 return self.playlist_result(entries, playlist_id, playlist_title)