]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/ivi.py
New upstream version 2018.11.07
[youtubedl] / 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
22 _TESTS = [
23 # Single movie
24 {
25 'url': 'http://www.ivi.ru/watch/53141',
26 'md5': '6ff5be2254e796ed346251d117196cf4',
27 'info_dict': {
28 'id': '53141',
29 'ext': 'mp4',
30 'title': 'Иван Васильевич меняет профессию',
31 'description': 'md5:b924063ea1677c8fe343d8a72ac2195f',
32 'duration': 5498,
33 'thumbnail': r're:^https?://.*\.jpg$',
34 },
35 'skip': 'Only works from Russia',
36 },
37 # Serial's series
38 {
39 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa/9549',
40 'md5': '221f56b35e3ed815fde2df71032f4b3e',
41 'info_dict': {
42 'id': '9549',
43 'ext': 'mp4',
44 'title': 'Двое из ларца - Дело Гольдберга (1 часть)',
45 'series': 'Двое из ларца',
46 'season': 'Сезон 1',
47 'season_number': 1,
48 'episode': 'Дело Гольдберга (1 часть)',
49 'episode_number': 1,
50 'duration': 2655,
51 'thumbnail': r're:^https?://.*\.jpg$',
52 },
53 'skip': 'Only works from Russia',
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,
65 'thumbnail': r're:^https?://.*\.jpg$',
66 },
67 'skip': 'Only works from Russia',
68 },
69 {
70 'url': 'https://www.ivi.tv/watch/33560/',
71 'only_matching': True,
72 },
73 ]
74
75 # Sorted by quality
76 _KNOWN_FORMATS = (
77 'MP4-low-mobile', 'MP4-mobile', 'FLV-lo', 'MP4-lo', 'FLV-hi', 'MP4-hi',
78 'MP4-SHQ', 'MP4-HD720', 'MP4-HD1080')
79
80 def _real_extract(self, url):
81 video_id = self._match_id(url)
82
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
90 }
91 ]
92 }
93
94 video_json = self._download_json(
95 'http://api.digitalaccess.ru/api/json/', video_id,
96 'Downloading video JSON', data=json.dumps(data))
97
98 if 'error' in video_json:
99 error = video_json['error']
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':
105 raise ExtractorError('Video %s does not exist' % video_id, expected=True)
106 raise ExtractorError(
107 'Unable to download video %s: %s' % (video_id, error['message']),
108 expected=True)
109
110 result = video_json['result']
111
112 quality = qualities(self._KNOWN_FORMATS)
113
114 formats = [{
115 'url': x['url'],
116 'format_id': x.get('content_format'),
117 'quality': quality(x.get('content_format')),
118 } for x in result['files'] if x.get('url')]
119
120 self._sort_formats(formats)
121
122 title = result['title']
123
124 duration = int_or_none(result.get('duration'))
125 compilation = result.get('compilation')
126 episode = title if compilation else None
127
128 title = '%s - %s' % (compilation, title) if compilation is not None else title
129
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
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
144 episode_number = int_or_none(self._search_regex(
145 r'[^>]+itemprop="episode"[^>]*>\s*<meta[^>]+itemprop="episodeNumber"[^>]+content="(\d+)',
146 webpage, 'episode number', default=None))
147
148 description = self._og_search_description(webpage, default=None) or self._html_search_meta(
149 'description', webpage, 'description', default=None)
150
151 return {
152 'id': video_id,
153 'title': title,
154 'series': compilation,
155 'season': season,
156 'season_number': season_number,
157 'episode': episode,
158 'episode_number': episode_number,
159 'thumbnails': thumbnails,
160 'description': description,
161 'duration': duration,
162 'formats': formats,
163 }
164
165
166 class IviCompilationIE(InfoExtractor):
167 IE_DESC = 'ivi.ru compilations'
168 IE_NAME = 'ivi:compilation'
169 _VALID_URL = r'https?://(?:www\.)?ivi\.ru/watch/(?!\d+)(?P<compilationid>[a-z\d_-]+)(?:/season(?P<seasonid>\d+))?$'
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 }]
185
186 def _extract_entries(self, html, compilation_id):
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)]
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
198 if season_id is not None: # Season link
199 season_page = self._download_webpage(
200 url, compilation_id, 'Downloading season %s web page' % season_id)
201 playlist_id = '%s/season%s' % (compilation_id, season_id)
202 playlist_title = self._html_search_meta('title', season_page, 'title')
203 entries = self._extract_entries(season_page, compilation_id)
204 else: # Compilation link
205 compilation_page = self._download_webpage(url, compilation_id, 'Downloading compilation web page')
206 playlist_id = compilation_id
207 playlist_title = self._html_search_meta('title', compilation_page, 'title')
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
211 entries = self._extract_entries(compilation_page, compilation_id)
212 else:
213 entries = []
214 for season_id in seasons:
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)
218 entries.extend(self._extract_entries(season_page, compilation_id))
219
220 return self.playlist_result(entries, playlist_id, playlist_title)