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