4 from .common
import InfoExtractor
5 from ..dependencies
import Cryptodome
6 from ..utils
import ExtractorError
, int_or_none
, qualities
9 class IviIE(InfoExtractor
):
12 _VALID_URL
= r
'https?://(?:www\.)?ivi\.(?:ru|tv)/(?:watch/(?:[^/]+/)?|video/player\?.*?videoId=)(?P<id>\d+)'
13 _EMBED_REGEX
= [r
'<embed[^>]+?src=(["\'])(?P
<url
>https?
://(?
:www\
.)?ivi\
.ru
/video
/player
.+?
)\
1']
15 _GEO_COUNTRIES = ['RU
']
16 _LIGHT_KEY = b'\xf1\x02\x32\xb7\xbc\x5c\x7a\xe8\xf7\x96\xc1\x33\x2b\x27\xa1\x8c'
17 _LIGHT_URL = 'https
://api
.ivi
.ru
/light
/'
22 'url
': 'http
://www
.ivi
.ru
/watch
/53141',
23 'md5
': '6ff5be2254e796ed346251d117196cf4
',
27 'title
': 'Иван Васильевич меняет профессию
',
28 'description
': 'md5
:b924063ea1677c8fe343d8a72ac2195f
',
30 'thumbnail
': r're
:^https?
://.*\
.jpg$
',
32 'skip
': 'Only works
from Russia
',
36 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa/9549',
37 'md5': '221f56b35e3ed815fde2df71032f4b3e',
41 'title': 'Двое из ларца - Дело Гольдберга (1 часть)',
42 'series': 'Двое из ларца',
45 'episode': 'Дело Гольдберга (1 часть)',
48 'thumbnail': r
're:^https?://.*\.jpg$',
50 'skip': 'Only works from Russia',
53 # with MP4-HD720 format
54 'url': 'http://www.ivi.ru/watch/146500',
55 'md5': 'd63d35cdbfa1ea61a5eafec7cc523e1e',
60 'description': 'md5:ffca9372399976a2d260a407cc74cce6',
62 'thumbnail': r
're:^https?://.*\.jpg$',
64 'skip': 'Only works from Russia',
67 'url': 'https://www.ivi.tv/watch/33560/',
68 'only_matching': True,
74 'MP4-low-mobile', 'MP4-mobile', 'FLV-lo', 'MP4-lo', 'FLV-hi', 'MP4-hi',
75 'MP4-SHQ', 'MP4-HD720', 'MP4-HD1080')
77 def _real_extract(self
, url
):
78 video_id
= self
._match
_id
(url
)
81 'method': 'da.content.get',
85 'referrer': f
'http://www.ivi.ru/watch/{video_id}',
86 'contentid': video_id
,
91 for site
in (353, 183):
92 content_data
= (data
% site
).encode()
94 if not Cryptodome
.CMAC
:
97 timestamp
= (self
._download
_json
(
98 self
._LIGHT
_URL
, video_id
,
99 'Downloading timestamp JSON', data
=json
.dumps({
100 'method': 'da.timestamp.get',
102 }).encode(), fatal
=False) or {}).get('result')
108 'sign': Cryptodome
.CMAC
.new(self
._LIGHT
_KEY
, timestamp
.encode() + content_data
,
109 Cryptodome
.Blowfish
).hexdigest(),
114 video_json
= self
._download
_json
(
115 self
._LIGHT
_URL
, video_id
,
116 'Downloading video JSON', data
=content_data
, query
=query
)
118 error
= video_json
.get('error')
120 origin
= error
.get('origin')
121 message
= error
.get('message') or error
.get('user_message')
122 extractor_msg
= 'Unable to download video %s'
123 if origin
== 'NotAllowedForLocation':
124 self
.raise_geo_restricted(message
, self
._GEO
_COUNTRIES
)
125 elif origin
== 'NoRedisValidData':
126 extractor_msg
= 'Video %s does not exist'
129 elif not Cryptodome
.CMAC
:
130 raise ExtractorError('pycryptodomex not found. Please install', expected
=True)
132 extractor_msg
+= ': ' + message
133 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')
148 if (not self
.get_param('allow_unplayable_formats')
149 and ('-MDRM-' in content_format
or '-FPS-' in content_format
)):
153 'format_id': content_format
,
154 'quality': quality(content_format
),
155 'filesize': int_or_none(f
.get('size_in_bytes')),
158 compilation
= result
.get('compilation')
159 episode
= title
if compilation
else None
161 title
= f
'{compilation} - {title}' if compilation
is not None else title
164 'url': preview
['url'],
165 'id': preview
.get('content_format'),
166 } for preview
in result
.get('preview', []) if preview
.get('url')]
168 webpage
= self
._download
_webpage
(url
, video_id
)
170 season
= self
._search
_regex
(
171 r
'<li[^>]+class="season active"[^>]*><a[^>]+>([^<]+)',
172 webpage
, 'season', default
=None)
173 season_number
= int_or_none(self
._search
_regex
(
174 r
'<li[^>]+class="season active"[^>]*><a[^>]+data-season(?:-index)?="(\d+)"',
175 webpage
, 'season number', default
=None))
177 episode_number
= int_or_none(self
._search
_regex
(
178 r
'[^>]+itemprop="episode"[^>]*>\s*<meta[^>]+itemprop="episodeNumber"[^>]+content="(\d+)',
179 webpage
, 'episode number', default
=None))
181 description
= self
._og
_search
_description
(webpage
, default
=None) or self
._html
_search
_meta
(
182 'description', webpage
, 'description', default
=None)
187 'series': compilation
,
189 'season_number': season_number
,
191 'episode_number': episode_number
,
192 'thumbnails': thumbnails
,
193 'description': description
,
194 'duration': int_or_none(result
.get('duration')),
199 class IviCompilationIE(InfoExtractor
):
200 IE_DESC
= 'ivi.ru compilations'
201 IE_NAME
= 'ivi:compilation'
202 _VALID_URL
= r
'https?://(?:www\.)?ivi\.ru/watch/(?!\d+)(?P<compilationid>[a-z\d_-]+)(?:/season(?P<seasonid>\d+))?$'
204 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa',
206 'id': 'dvoe_iz_lartsa',
207 'title': 'Двое из ларца (2006 - 2008)',
209 'playlist_mincount': 24,
211 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa/season1',
213 'id': 'dvoe_iz_lartsa/season1',
214 'title': 'Двое из ларца (2006 - 2008) 1 сезон',
216 'playlist_mincount': 12,
219 def _extract_entries(self
, html
, compilation_id
):
222 f
'http://www.ivi.ru/watch/{compilation_id}/{serie}', IviIE
.ie_key())
223 for serie
in re
.findall(
224 rf
'<a\b[^>]+\bhref=["\']/watch/{compilation_id}/(\d+)["\']', html
)]
226 def _real_extract(self
, url
):
227 mobj
= self
._match
_valid
_url
(url
)
228 compilation_id
= mobj
.group('compilationid')
229 season_id
= mobj
.group('seasonid')
231 if season_id
is not None: # Season link
232 season_page
= self
._download
_webpage
(
233 url
, compilation_id
, f
'Downloading season {season_id} web page')
234 playlist_id
= f
'{compilation_id}/season{season_id}'
235 playlist_title
= self
._html
_search
_meta
('title', season_page
, 'title')
236 entries
= self
._extract
_entries
(season_page
, compilation_id
)
237 else: # Compilation link
238 compilation_page
= self
._download
_webpage
(url
, compilation_id
, 'Downloading compilation web page')
239 playlist_id
= compilation_id
240 playlist_title
= self
._html
_search
_meta
('title', compilation_page
, 'title')
241 seasons
= re
.findall(
242 rf
'<a href="/watch/{compilation_id}/season(\d+)', compilation_page
)
243 if not seasons
: # No seasons in this compilation
244 entries
= self
._extract
_entries
(compilation_page
, compilation_id
)
247 for season_id
in seasons
:
248 season_page
= self
._download
_webpage
(
249 f
'http://www.ivi.ru/watch/{compilation_id}/season{season_id}',
250 compilation_id
, f
'Downloading season {season_id} web page')
251 entries
.extend(self
._extract
_entries
(season_page
, compilation_id
))
253 return self
.playlist_result(entries
, playlist_id
, playlist_title
)