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,
96 for site in (353, 183):
97 content_data = data % site
100 from Cryptodome.Cipher import Blowfish
101 from Cryptodome.Hash import CMAC
102 pycryptodomex_found = True
104 pycryptodomex_found = False
107 timestamp = (self._download_json(
108 self._LIGHT_URL, video_id,
109 'Downloading timestamp JSON', data=json.dumps({
110 'method': 'da.timestamp.get',
112 }).encode(), fatal=False) or {}).get('result')
118 'sign': CMAC.new(self._LIGHT_KEY, timestamp.encode() + content_data, Blowfish).hexdigest(),
123 video_json = self._download_json(
124 self._LIGHT_URL, video_id,
125 'Downloading video JSON', data=content_data, query=query)
127 error = video_json.get('error')
129 origin = error.get('origin')
130 message = error.get('message') or error.get('user_message')
131 extractor_msg = 'Unable to download video %s'
132 if origin == 'NotAllowedForLocation':
133 self.raise_geo_restricted(message, self._GEO_COUNTRIES)
134 elif origin == 'NoRedisValidData':
135 extractor_msg = 'Video %s does not exist'
138 elif not pycryptodomex_found:
139 raise ExtractorError(
140 'pycryptodome not found. Please install it.',
143 extractor_msg += ': ' + message
144 raise ExtractorError(extractor_msg % video_id, expected=True)
148 result = video_json['result']
149 title = result['title']
151 quality = qualities(self._KNOWN_FORMATS)
154 for f in result.get('files', []):
156 content_format = f.get('content_format')
157 if not f_url or '-MDRM-' in content_format or '-FPS-' in content_format:
161 'format_id': content_format,
162 'quality': quality(content_format),
163 'filesize': int_or_none(f.get('size_in_bytes')),
165 self._sort_formats(formats)
167 compilation = result.get('compilation')
168 episode = title if compilation else None
170 title = '%s - %s' % (compilation, title) if compilation is not None else title
173 'url': preview['url'],
174 'id': preview.get('content_format'),
175 } for preview in result.get('preview', []) if preview.get('url')]
177 webpage = self._download_webpage(url, video_id)
179 season = self._search_regex(
180 r'<li[^>]+class="season active"[^>]*><a[^>]+>([^<]+)',
181 webpage, 'season', default=None)
182 season_number = int_or_none(self._search_regex(
183 r'<li[^>]+class="season active"[^>]*><a[^>]+data-season(?:-index)?="(\d+)"',
184 webpage, 'season number', default=None))
186 episode_number = int_or_none(self._search_regex(
187 r'[^>]+itemprop="episode"[^>]*>\s*<meta[^>]+itemprop="episodeNumber"[^>]+content="(\d+)',
188 webpage, 'episode number', default=None))
190 description = self._og_search_description(webpage, default=None) or self._html_search_meta(
191 'description', webpage, 'description', default=None)
196 'series': compilation,
198 'season_number': season_number,
200 'episode_number': episode_number,
201 'thumbnails': thumbnails,
202 'description': description,
203 'duration': int_or_none(result.get('duration')),
208 class IviCompilationIE(InfoExtractor):
209 IE_DESC = 'ivi.ru compilations'
210 IE_NAME = 'ivi:compilation'
211 _VALID_URL = r'https?://(?:www\.)?ivi\.ru/watch/(?!\d+)(?P<compilationid>[a-z\d_-]+)(?:/season(?P<seasonid>\d+))?$'
213 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa',
215 'id': 'dvoe_iz_lartsa',
216 'title': 'Двое из ларца (2006 - 2008)',
218 'playlist_mincount': 24,
220 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa/season1',
222 'id': 'dvoe_iz_lartsa/season1',
223 'title': 'Двое из ларца (2006 - 2008) 1 сезон',
225 'playlist_mincount': 12,
228 def _extract_entries(self, html, compilation_id):
231 'http://www.ivi.ru/watch/%s/%s' % (compilation_id, serie), IviIE.ie_key())
232 for serie in re.findall(
233 r'<a href="/watch/%s/(\d+)"[^>]+data-id="\1"' % compilation_id, html)]
235 def _real_extract(self, url):
236 mobj = re.match(self._VALID_URL, url)
237 compilation_id = mobj.group('compilationid')
238 season_id = mobj.group('seasonid')
240 if season_id is not None: # Season link
241 season_page = self._download_webpage(
242 url, compilation_id, 'Downloading season %s web page' % season_id)
243 playlist_id = '%s/season%s' % (compilation_id, season_id)
244 playlist_title = self._html_search_meta('title', season_page, 'title')
245 entries = self._extract_entries(season_page, compilation_id)
246 else: # Compilation link
247 compilation_page = self._download_webpage(url, compilation_id, 'Downloading compilation web page')
248 playlist_id = compilation_id
249 playlist_title = self._html_search_meta('title', compilation_page, 'title')
250 seasons = re.findall(
251 r'<a href="/watch/%s/season(\d+)' % compilation_id, compilation_page)
252 if not seasons: # No seasons in this compilation
253 entries = self._extract_entries(compilation_page, compilation_id)
256 for season_id in seasons:
257 season_page = self._download_webpage(
258 'http://www.ivi.ru/watch/%s/season%s' % (compilation_id, season_id),
259 compilation_id, 'Downloading season %s web page' % season_id)
260 entries.extend(self._extract_entries(season_page, compilation_id))
262 return self.playlist_result(entries, playlist_id, playlist_title)