]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/ivi.py
[extractors] Use new framework for existing embeds (#4307)
[yt-dlp.git] / yt_dlp / extractor / ivi.py
CommitLineData
77aa6b32 1import json
80a51fc2 2import re
77aa6b32 3
4from .common import InfoExtractor
1cc79574 5from ..utils import (
77aa6b32 6 ExtractorError,
ab3176af 7 int_or_none,
cf143c4d 8 qualities,
77aa6b32 9)
10
11
12class IviIE(InfoExtractor):
ceb2b7d2 13 IE_DESC = 'ivi.ru'
14 IE_NAME = 'ivi'
022218f2 15 _VALID_URL = r'https?://(?:www\.)?ivi\.(?:ru|tv)/(?:watch/(?:[^/]+/)?|video/player\?.*?videoId=)(?P<id>\d+)'
bfd973ec 16 _EMBED_REGEX = [r'<embed[^>]+?src=(["\'])(?P<url>https?://(?:www\.)?ivi\.ru/video/player.+?)\1']
42dcdbe1
S
17 _GEO_BYPASS = False
18 _GEO_COUNTRIES = ['RU']
656c2001
RA
19 _LIGHT_KEY = b'\xf1\x02\x32\xb7\xbc\x5c\x7a\xe8\xf7\x96\xc1\x33\x2b\x27\xa1\x8c'
20 _LIGHT_URL = 'https://api.ivi.ru/light/'
77aa6b32 21
22 _TESTS = [
23 # Single movie
24 {
ceb2b7d2 25 'url': 'http://www.ivi.ru/watch/53141',
ceb2b7d2 26 'md5': '6ff5be2254e796ed346251d117196cf4',
27 'info_dict': {
84dd7031
S
28 'id': '53141',
29 'ext': 'mp4',
ceb2b7d2 30 'title': 'Иван Васильевич меняет профессию',
31 'description': 'md5:b924063ea1677c8fe343d8a72ac2195f',
32 'duration': 5498,
ec85ded8 33 'thumbnail': r're:^https?://.*\.jpg$',
77aa6b32 34 },
ceb2b7d2 35 'skip': 'Only works from Russia',
77aa6b32 36 },
dfb1b146 37 # Serial's series
77aa6b32 38 {
6ebb46c1
S
39 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa/9549',
40 'md5': '221f56b35e3ed815fde2df71032f4b3e',
ceb2b7d2 41 'info_dict': {
6ebb46c1 42 'id': '9549',
84dd7031 43 'ext': 'mp4',
ab3176af
S
44 'title': 'Двое из ларца - Дело Гольдберга (1 часть)',
45 'series': 'Двое из ларца',
1463c5b9
S
46 'season': 'Сезон 1',
47 'season_number': 1,
ab3176af
S
48 'episode': 'Дело Гольдберга (1 часть)',
49 'episode_number': 1,
6ebb46c1 50 'duration': 2655,
ec85ded8 51 'thumbnail': r're:^https?://.*\.jpg$',
77aa6b32 52 },
ceb2b7d2 53 'skip': 'Only works from Russia',
cf143c4d
S
54 },
55 {
56 # with MP4-HD720 format
57 'url': 'http://www.ivi.ru/watch/146500',
58 'md5': 'd63d35cdbfa1ea61a5eafec7cc523e1e',
59 'info_dict': {
60 'id': '146500',
61 'ext': 'mp4',
62 'title': 'Кукла',
63 'description': 'md5:ffca9372399976a2d260a407cc74cce6',
64 'duration': 5599,
ec85ded8 65 'thumbnail': r're:^https?://.*\.jpg$',
cf143c4d
S
66 },
67 'skip': 'Only works from Russia',
022218f2
S
68 },
69 {
70 'url': 'https://www.ivi.tv/watch/33560/',
71 'only_matching': True,
72 },
77aa6b32 73 ]
ceb2b7d2 74
77aa6b32 75 # Sorted by quality
cf143c4d
S
76 _KNOWN_FORMATS = (
77 'MP4-low-mobile', 'MP4-mobile', 'FLV-lo', 'MP4-lo', 'FLV-hi', 'MP4-hi',
78 'MP4-SHQ', 'MP4-HD720', 'MP4-HD1080')
77aa6b32 79
80 def _real_extract(self, url):
63be3b89 81 video_id = self._match_id(url)
77aa6b32 82
656c2001 83 data = json.dumps({
63be3b89
S
84 'method': 'da.content.get',
85 'params': [
86 video_id, {
1bba88ef 87 'site': 's%d',
63be3b89
S
88 'referrer': 'http://www.ivi.ru/watch/%s' % video_id,
89 'contentid': video_id
77aa6b32 90 }
63be3b89 91 ]
f8015c15 92 })
77aa6b32 93
76d9eca4 94 for site in (353, 183):
f8015c15 95 content_data = (data % site).encode()
76d9eca4
RA
96 if site == 353:
97 try:
98 from Cryptodome.Cipher import Blowfish
99 from Cryptodome.Hash import CMAC
edf65256 100 pycryptodome_found = True
76d9eca4 101 except ImportError:
edf65256 102 try:
103 from Crypto.Cipher import Blowfish
104 from Crypto.Hash import CMAC
105 pycryptodome_found = True
106 except ImportError:
107 pycryptodome_found = False
108 continue
76d9eca4
RA
109
110 timestamp = (self._download_json(
111 self._LIGHT_URL, video_id,
112 'Downloading timestamp JSON', data=json.dumps({
113 'method': 'da.timestamp.get',
114 'params': []
115 }).encode(), fatal=False) or {}).get('result')
116 if not timestamp:
117 continue
118
119 query = {
120 'ts': timestamp,
121 'sign': CMAC.new(self._LIGHT_KEY, timestamp.encode() + content_data, Blowfish).hexdigest(),
122 }
123 else:
124 query = {}
1bba88ef 125
76d9eca4 126 video_json = self._download_json(
1bba88ef 127 self._LIGHT_URL, video_id,
76d9eca4
RA
128 'Downloading video JSON', data=content_data, query=query)
129
130 error = video_json.get('error')
131 if error:
132 origin = error.get('origin')
133 message = error.get('message') or error.get('user_message')
134 extractor_msg = 'Unable to download video %s'
135 if origin == 'NotAllowedForLocation':
136 self.raise_geo_restricted(message, self._GEO_COUNTRIES)
137 elif origin == 'NoRedisValidData':
138 extractor_msg = 'Video %s does not exist'
139 elif site == 353:
140 continue
edf65256 141 elif not pycryptodome_found:
49e7e9c3 142 raise ExtractorError('pycryptodomex not found. Please install', expected=True)
76d9eca4
RA
143 elif message:
144 extractor_msg += ': ' + message
145 raise ExtractorError(extractor_msg % video_id, expected=True)
146 else:
147 break
77aa6b32 148
ceb2b7d2 149 result = video_json['result']
656c2001 150 title = result['title']
77aa6b32 151
cf143c4d
S
152 quality = qualities(self._KNOWN_FORMATS)
153
656c2001
RA
154 formats = []
155 for f in result.get('files', []):
156 f_url = f.get('url')
157 content_format = f.get('content_format')
06869367 158 if not f_url:
159 continue
a06916d9 160 if (not self.get_param('allow_unplayable_formats')
06869367 161 and ('-MDRM-' in content_format or '-FPS-' in content_format)):
656c2001
RA
162 continue
163 formats.append({
164 'url': f_url,
165 'format_id': content_format,
166 'quality': quality(content_format),
167 'filesize': int_or_none(f.get('size_in_bytes')),
168 })
bf5b0a1b
PH
169 self._sort_formats(formats)
170
ab3176af
S
171 compilation = result.get('compilation')
172 episode = title if compilation else None
173
5f6a1245 174 title = '%s - %s' % (compilation, title) if compilation is not None else title
77aa6b32 175
ab3176af
S
176 thumbnails = [{
177 'url': preview['url'],
178 'id': preview.get('content_format'),
179 } for preview in result.get('preview', []) if preview.get('url')]
180
181 webpage = self._download_webpage(url, video_id)
182
1463c5b9
S
183 season = self._search_regex(
184 r'<li[^>]+class="season active"[^>]*><a[^>]+>([^<]+)',
185 webpage, 'season', default=None)
186 season_number = int_or_none(self._search_regex(
187 r'<li[^>]+class="season active"[^>]*><a[^>]+data-season(?:-index)?="(\d+)"',
188 webpage, 'season number', default=None))
189
ab3176af 190 episode_number = int_or_none(self._search_regex(
3d897cc7 191 r'[^>]+itemprop="episode"[^>]*>\s*<meta[^>]+itemprop="episodeNumber"[^>]+content="(\d+)',
ab3176af 192 webpage, 'episode number', default=None))
77aa6b32 193
ab3176af
S
194 description = self._og_search_description(webpage, default=None) or self._html_search_meta(
195 'description', webpage, 'description', default=None)
77aa6b32 196
197 return {
198 'id': video_id,
199 'title': title,
ab3176af 200 'series': compilation,
1463c5b9
S
201 'season': season,
202 'season_number': season_number,
ab3176af
S
203 'episode': episode,
204 'episode_number': episode_number,
205 'thumbnails': thumbnails,
77aa6b32 206 'description': description,
656c2001 207 'duration': int_or_none(result.get('duration')),
77aa6b32 208 'formats': formats,
209 }
210
211
212class IviCompilationIE(InfoExtractor):
ceb2b7d2 213 IE_DESC = 'ivi.ru compilations'
214 IE_NAME = 'ivi:compilation'
84dd7031 215 _VALID_URL = r'https?://(?:www\.)?ivi\.ru/watch/(?!\d+)(?P<compilationid>[a-z\d_-]+)(?:/season(?P<seasonid>\d+))?$'
22a6f150
PH
216 _TESTS = [{
217 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa',
218 'info_dict': {
219 'id': 'dvoe_iz_lartsa',
220 'title': 'Двое из ларца (2006 - 2008)',
221 },
222 'playlist_mincount': 24,
223 }, {
224 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa/season1',
225 'info_dict': {
226 'id': 'dvoe_iz_lartsa/season1',
227 'title': 'Двое из ларца (2006 - 2008) 1 сезон',
228 },
229 'playlist_mincount': 12,
230 }]
77aa6b32 231
232 def _extract_entries(self, html, compilation_id):
c6270b2e
S
233 return [
234 self.url_result(
235 'http://www.ivi.ru/watch/%s/%s' % (compilation_id, serie), IviIE.ie_key())
236 for serie in re.findall(
d9a2f867 237 r'<a\b[^>]+\bhref=["\']/watch/%s/(\d+)["\']' % compilation_id, html)]
77aa6b32 238
239 def _real_extract(self, url):
5ad28e7f 240 mobj = self._match_valid_url(url)
77aa6b32 241 compilation_id = mobj.group('compilationid')
242 season_id = mobj.group('seasonid')
243
5f6a1245 244 if season_id is not None: # Season link
c6270b2e
S
245 season_page = self._download_webpage(
246 url, compilation_id, 'Downloading season %s web page' % season_id)
77aa6b32 247 playlist_id = '%s/season%s' % (compilation_id, season_id)
ceb2b7d2 248 playlist_title = self._html_search_meta('title', season_page, 'title')
77aa6b32 249 entries = self._extract_entries(season_page, compilation_id)
5f6a1245 250 else: # Compilation link
ceb2b7d2 251 compilation_page = self._download_webpage(url, compilation_id, 'Downloading compilation web page')
77aa6b32 252 playlist_id = compilation_id
ceb2b7d2 253 playlist_title = self._html_search_meta('title', compilation_page, 'title')
c6270b2e
S
254 seasons = re.findall(
255 r'<a href="/watch/%s/season(\d+)' % compilation_id, compilation_page)
256 if not seasons: # No seasons in this compilation
77aa6b32 257 entries = self._extract_entries(compilation_page, compilation_id)
258 else:
259 entries = []
260 for season_id in seasons:
ceb2b7d2 261 season_page = self._download_webpage(
262 'http://www.ivi.ru/watch/%s/season%s' % (compilation_id, season_id),
263 compilation_id, 'Downloading season %s web page' % season_id)
77aa6b32 264 entries.extend(self._extract_entries(season_page, compilation_id))
265
5f6a1245 266 return self.playlist_result(entries, playlist_id, playlist_title)