]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/ivi.py
Update changelog.
[youtubedl] / youtube_dl / extractor / ivi.py
1 # encoding: 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 sanitized_Request,
12 )
13
14
15 class IviIE(InfoExtractor):
16 IE_DESC = 'ivi.ru'
17 IE_NAME = 'ivi'
18 _VALID_URL = r'https?://(?:www\.)?ivi\.ru/(?:watch/(?:[^/]+/)?|video/player\?.*?videoId=)(?P<id>\d+)'
19
20 _TESTS = [
21 # Single movie
22 {
23 'url': 'http://www.ivi.ru/watch/53141',
24 'md5': '6ff5be2254e796ed346251d117196cf4',
25 'info_dict': {
26 'id': '53141',
27 'ext': 'mp4',
28 'title': 'Иван Васильевич меняет профессию',
29 'description': 'md5:b924063ea1677c8fe343d8a72ac2195f',
30 'duration': 5498,
31 'thumbnail': 're:^https?://.*\.jpg$',
32 },
33 'skip': 'Only works from Russia',
34 },
35 # Serial's series
36 {
37 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa/9549',
38 'md5': '221f56b35e3ed815fde2df71032f4b3e',
39 'info_dict': {
40 'id': '9549',
41 'ext': 'mp4',
42 'title': 'Двое из ларца - Дело Гольдберга (1 часть)',
43 'series': 'Двое из ларца',
44 'season': 'Сезон 1',
45 'season_number': 1,
46 'episode': 'Дело Гольдберга (1 часть)',
47 'episode_number': 1,
48 'duration': 2655,
49 'thumbnail': 're:^https?://.*\.jpg$',
50 },
51 'skip': 'Only works from Russia',
52 }
53 ]
54
55 # Sorted by quality
56 _KNOWN_FORMATS = ['MP4-low-mobile', 'MP4-mobile', 'FLV-lo', 'MP4-lo', 'FLV-hi', 'MP4-hi', 'MP4-SHQ']
57
58 def _real_extract(self, url):
59 video_id = self._match_id(url)
60
61 data = {
62 'method': 'da.content.get',
63 'params': [
64 video_id, {
65 'site': 's183',
66 'referrer': 'http://www.ivi.ru/watch/%s' % video_id,
67 'contentid': video_id
68 }
69 ]
70 }
71
72 request = sanitized_Request(
73 'http://api.digitalaccess.ru/api/json/', json.dumps(data))
74 video_json = self._download_json(
75 request, video_id, 'Downloading video JSON')
76
77 if 'error' in video_json:
78 error = video_json['error']
79 if error['origin'] == 'NoRedisValidData':
80 raise ExtractorError('Video %s does not exist' % video_id, expected=True)
81 raise ExtractorError(
82 'Unable to download video %s: %s' % (video_id, error['message']),
83 expected=True)
84
85 result = video_json['result']
86
87 formats = [{
88 'url': x['url'],
89 'format_id': x['content_format'],
90 'preference': self._KNOWN_FORMATS.index(x['content_format']),
91 } for x in result['files'] if x['content_format'] in self._KNOWN_FORMATS]
92
93 self._sort_formats(formats)
94
95 title = result['title']
96
97 duration = int_or_none(result.get('duration'))
98 compilation = result.get('compilation')
99 episode = title if compilation else None
100
101 title = '%s - %s' % (compilation, title) if compilation is not None else title
102
103 thumbnails = [{
104 'url': preview['url'],
105 'id': preview.get('content_format'),
106 } for preview in result.get('preview', []) if preview.get('url')]
107
108 webpage = self._download_webpage(url, video_id)
109
110 season = self._search_regex(
111 r'<li[^>]+class="season active"[^>]*><a[^>]+>([^<]+)',
112 webpage, 'season', default=None)
113 season_number = int_or_none(self._search_regex(
114 r'<li[^>]+class="season active"[^>]*><a[^>]+data-season(?:-index)?="(\d+)"',
115 webpage, 'season number', default=None))
116
117 episode_number = int_or_none(self._search_regex(
118 r'<meta[^>]+itemprop="episode"[^>]*>\s*<meta[^>]+itemprop="episodeNumber"[^>]+content="(\d+)',
119 webpage, 'episode number', default=None))
120
121 description = self._og_search_description(webpage, default=None) or self._html_search_meta(
122 'description', webpage, 'description', default=None)
123
124 return {
125 'id': video_id,
126 'title': title,
127 'series': compilation,
128 'season': season,
129 'season_number': season_number,
130 'episode': episode,
131 'episode_number': episode_number,
132 'thumbnails': thumbnails,
133 'description': description,
134 'duration': duration,
135 'formats': formats,
136 }
137
138
139 class IviCompilationIE(InfoExtractor):
140 IE_DESC = 'ivi.ru compilations'
141 IE_NAME = 'ivi:compilation'
142 _VALID_URL = r'https?://(?:www\.)?ivi\.ru/watch/(?!\d+)(?P<compilationid>[a-z\d_-]+)(?:/season(?P<seasonid>\d+))?$'
143 _TESTS = [{
144 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa',
145 'info_dict': {
146 'id': 'dvoe_iz_lartsa',
147 'title': 'Двое из ларца (2006 - 2008)',
148 },
149 'playlist_mincount': 24,
150 }, {
151 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa/season1',
152 'info_dict': {
153 'id': 'dvoe_iz_lartsa/season1',
154 'title': 'Двое из ларца (2006 - 2008) 1 сезон',
155 },
156 'playlist_mincount': 12,
157 }]
158
159 def _extract_entries(self, html, compilation_id):
160 return [
161 self.url_result(
162 'http://www.ivi.ru/watch/%s/%s' % (compilation_id, serie), IviIE.ie_key())
163 for serie in re.findall(
164 r'<a href="/watch/%s/(\d+)"[^>]+data-id="\1"' % compilation_id, html)]
165
166 def _real_extract(self, url):
167 mobj = re.match(self._VALID_URL, url)
168 compilation_id = mobj.group('compilationid')
169 season_id = mobj.group('seasonid')
170
171 if season_id is not None: # Season link
172 season_page = self._download_webpage(
173 url, compilation_id, 'Downloading season %s web page' % season_id)
174 playlist_id = '%s/season%s' % (compilation_id, season_id)
175 playlist_title = self._html_search_meta('title', season_page, 'title')
176 entries = self._extract_entries(season_page, compilation_id)
177 else: # Compilation link
178 compilation_page = self._download_webpage(url, compilation_id, 'Downloading compilation web page')
179 playlist_id = compilation_id
180 playlist_title = self._html_search_meta('title', compilation_page, 'title')
181 seasons = re.findall(
182 r'<a href="/watch/%s/season(\d+)' % compilation_id, compilation_page)
183 if not seasons: # No seasons in this compilation
184 entries = self._extract_entries(compilation_page, compilation_id)
185 else:
186 entries = []
187 for season_id in seasons:
188 season_page = self._download_webpage(
189 'http://www.ivi.ru/watch/%s/season%s' % (compilation_id, season_id),
190 compilation_id, 'Downloading season %s web page' % season_id)
191 entries.extend(self._extract_entries(season_page, compilation_id))
192
193 return self.playlist_result(entries, playlist_id, playlist_title)