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