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