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