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