]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/ivi.py
[crunchyroll] Improve extraction failsafeness (closes #17991)
[yt-dlp.git] / youtube_dl / extractor / ivi.py
CommitLineData
cf143c4d 1# coding: utf-8
ceb2b7d2 2from __future__ import unicode_literals
77aa6b32 3
4import re
5import json
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'
63be3b89 18 _VALID_URL = r'https?://(?:www\.)?ivi\.ru/(?:watch/(?:[^/]+/)?|video/player\?.*?videoId=)(?P<id>\d+)'
42dcdbe1
S
19 _GEO_BYPASS = False
20 _GEO_COUNTRIES = ['RU']
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',
b74e86f4 68 }
77aa6b32 69 ]
ceb2b7d2 70
77aa6b32 71 # Sorted by quality
cf143c4d
S
72 _KNOWN_FORMATS = (
73 'MP4-low-mobile', 'MP4-mobile', 'FLV-lo', 'MP4-lo', 'FLV-hi', 'MP4-hi',
74 'MP4-SHQ', 'MP4-HD720', 'MP4-HD1080')
77aa6b32 75
76 def _real_extract(self, url):
63be3b89 77 video_id = self._match_id(url)
77aa6b32 78
63be3b89
S
79 data = {
80 'method': 'da.content.get',
81 'params': [
82 video_id, {
83 'site': 's183',
84 'referrer': 'http://www.ivi.ru/watch/%s' % video_id,
85 'contentid': video_id
77aa6b32 86 }
63be3b89
S
87 ]
88 }
77aa6b32 89
ab3176af 90 video_json = self._download_json(
cf143c4d
S
91 'http://api.digitalaccess.ru/api/json/', video_id,
92 'Downloading video JSON', data=json.dumps(data))
77aa6b32 93
ceb2b7d2 94 if 'error' in video_json:
95 error = video_json['error']
42dcdbe1
S
96 origin = error['origin']
97 if origin == 'NotAllowedForLocation':
98 self.raise_geo_restricted(
99 msg=error['message'], countries=self._GEO_COUNTRIES)
100 elif origin == 'NoRedisValidData':
ceb2b7d2 101 raise ExtractorError('Video %s does not exist' % video_id, expected=True)
63be3b89
S
102 raise ExtractorError(
103 'Unable to download video %s: %s' % (video_id, error['message']),
104 expected=True)
77aa6b32 105
ceb2b7d2 106 result = video_json['result']
77aa6b32 107
cf143c4d
S
108 quality = qualities(self._KNOWN_FORMATS)
109
bf5b0a1b 110 formats = [{
ceb2b7d2 111 'url': x['url'],
cf143c4d
S
112 'format_id': x.get('content_format'),
113 'quality': quality(x.get('content_format')),
114 } for x in result['files'] if x.get('url')]
bf5b0a1b
PH
115
116 self._sort_formats(formats)
117
ceb2b7d2 118 title = result['title']
77aa6b32 119
ab3176af
S
120 duration = int_or_none(result.get('duration'))
121 compilation = result.get('compilation')
122 episode = title if compilation else None
123
5f6a1245 124 title = '%s - %s' % (compilation, title) if compilation is not None else title
77aa6b32 125
ab3176af
S
126 thumbnails = [{
127 'url': preview['url'],
128 'id': preview.get('content_format'),
129 } for preview in result.get('preview', []) if preview.get('url')]
130
131 webpage = self._download_webpage(url, video_id)
132
1463c5b9
S
133 season = self._search_regex(
134 r'<li[^>]+class="season active"[^>]*><a[^>]+>([^<]+)',
135 webpage, 'season', default=None)
136 season_number = int_or_none(self._search_regex(
137 r'<li[^>]+class="season active"[^>]*><a[^>]+data-season(?:-index)?="(\d+)"',
138 webpage, 'season number', default=None))
139
ab3176af 140 episode_number = int_or_none(self._search_regex(
3d897cc7 141 r'[^>]+itemprop="episode"[^>]*>\s*<meta[^>]+itemprop="episodeNumber"[^>]+content="(\d+)',
ab3176af 142 webpage, 'episode number', default=None))
77aa6b32 143
ab3176af
S
144 description = self._og_search_description(webpage, default=None) or self._html_search_meta(
145 'description', webpage, 'description', default=None)
77aa6b32 146
147 return {
148 'id': video_id,
149 'title': title,
ab3176af 150 'series': compilation,
1463c5b9
S
151 'season': season,
152 'season_number': season_number,
ab3176af
S
153 'episode': episode,
154 'episode_number': episode_number,
155 'thumbnails': thumbnails,
77aa6b32 156 'description': description,
157 'duration': duration,
77aa6b32 158 'formats': formats,
159 }
160
161
162class IviCompilationIE(InfoExtractor):
ceb2b7d2 163 IE_DESC = 'ivi.ru compilations'
164 IE_NAME = 'ivi:compilation'
84dd7031 165 _VALID_URL = r'https?://(?:www\.)?ivi\.ru/watch/(?!\d+)(?P<compilationid>[a-z\d_-]+)(?:/season(?P<seasonid>\d+))?$'
22a6f150
PH
166 _TESTS = [{
167 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa',
168 'info_dict': {
169 'id': 'dvoe_iz_lartsa',
170 'title': 'Двое из ларца (2006 - 2008)',
171 },
172 'playlist_mincount': 24,
173 }, {
174 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa/season1',
175 'info_dict': {
176 'id': 'dvoe_iz_lartsa/season1',
177 'title': 'Двое из ларца (2006 - 2008) 1 сезон',
178 },
179 'playlist_mincount': 12,
180 }]
77aa6b32 181
182 def _extract_entries(self, html, compilation_id):
c6270b2e
S
183 return [
184 self.url_result(
185 'http://www.ivi.ru/watch/%s/%s' % (compilation_id, serie), IviIE.ie_key())
186 for serie in re.findall(
187 r'<a href="/watch/%s/(\d+)"[^>]+data-id="\1"' % compilation_id, html)]
77aa6b32 188
189 def _real_extract(self, url):
190 mobj = re.match(self._VALID_URL, url)
191 compilation_id = mobj.group('compilationid')
192 season_id = mobj.group('seasonid')
193
5f6a1245 194 if season_id is not None: # Season link
c6270b2e
S
195 season_page = self._download_webpage(
196 url, compilation_id, 'Downloading season %s web page' % season_id)
77aa6b32 197 playlist_id = '%s/season%s' % (compilation_id, season_id)
ceb2b7d2 198 playlist_title = self._html_search_meta('title', season_page, 'title')
77aa6b32 199 entries = self._extract_entries(season_page, compilation_id)
5f6a1245 200 else: # Compilation link
ceb2b7d2 201 compilation_page = self._download_webpage(url, compilation_id, 'Downloading compilation web page')
77aa6b32 202 playlist_id = compilation_id
ceb2b7d2 203 playlist_title = self._html_search_meta('title', compilation_page, 'title')
c6270b2e
S
204 seasons = re.findall(
205 r'<a href="/watch/%s/season(\d+)' % compilation_id, compilation_page)
206 if not seasons: # No seasons in this compilation
77aa6b32 207 entries = self._extract_entries(compilation_page, compilation_id)
208 else:
209 entries = []
210 for season_id in seasons:
ceb2b7d2 211 season_page = self._download_webpage(
212 'http://www.ivi.ru/watch/%s/season%s' % (compilation_id, season_id),
213 compilation_id, 'Downloading season %s web page' % season_id)
77aa6b32 214 entries.extend(self._extract_entries(season_page, compilation_id))
215
5f6a1245 216 return self.playlist_result(entries, playlist_id, playlist_title)