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