]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/ivi.py
[ivi] Fix extraction and modernize
[yt-dlp.git] / youtube_dl / extractor / ivi.py
CommitLineData
77aa6b32 1# encoding: 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,
5c2266df 11 sanitized_Request,
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,
ab3176af 31 'thumbnail': 're:^https?://.*\.jpg$',
77aa6b32 32 },
ceb2b7d2 33 'skip': 'Only works from Russia',
77aa6b32 34 },
35 # Serial's serie
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': 'Двое из ларца',
44 'episode': 'Дело Гольдберга (1 часть)',
45 'episode_number': 1,
6ebb46c1 46 'duration': 2655,
ab3176af 47 'thumbnail': 're:^https?://.*\.jpg$',
77aa6b32 48 },
ceb2b7d2 49 'skip': 'Only works from Russia',
b74e86f4 50 }
77aa6b32 51 ]
ceb2b7d2 52
77aa6b32 53 # Sorted by quality
ab3176af 54 _KNOWN_FORMATS = ['MP4-low-mobile', 'MP4-mobile', 'FLV-lo', 'MP4-lo', 'FLV-hi', 'MP4-hi', 'MP4-SHQ']
77aa6b32 55
56 def _real_extract(self, url):
63be3b89 57 video_id = self._match_id(url)
77aa6b32 58
63be3b89
S
59 data = {
60 'method': 'da.content.get',
61 'params': [
62 video_id, {
63 'site': 's183',
64 'referrer': 'http://www.ivi.ru/watch/%s' % video_id,
65 'contentid': video_id
77aa6b32 66 }
63be3b89
S
67 ]
68 }
77aa6b32 69
ab3176af
S
70 request = sanitized_Request(
71 'http://api.digitalaccess.ru/api/json/', json.dumps(data))
72 video_json = self._download_json(
63be3b89 73 request, video_id, 'Downloading video JSON')
77aa6b32 74
ceb2b7d2 75 if 'error' in video_json:
76 error = video_json['error']
77 if error['origin'] == 'NoRedisValidData':
78 raise ExtractorError('Video %s does not exist' % video_id, expected=True)
63be3b89
S
79 raise ExtractorError(
80 'Unable to download video %s: %s' % (video_id, error['message']),
81 expected=True)
77aa6b32 82
ceb2b7d2 83 result = video_json['result']
77aa6b32 84
bf5b0a1b 85 formats = [{
ceb2b7d2 86 'url': x['url'],
87 'format_id': x['content_format'],
ab3176af
S
88 'preference': self._KNOWN_FORMATS.index(x['content_format']),
89 } for x in result['files'] if x['content_format'] in self._KNOWN_FORMATS]
bf5b0a1b
PH
90
91 self._sort_formats(formats)
92
ceb2b7d2 93 title = result['title']
77aa6b32 94
ab3176af
S
95 duration = int_or_none(result.get('duration'))
96 compilation = result.get('compilation')
97 episode = title if compilation else None
98
5f6a1245 99 title = '%s - %s' % (compilation, title) if compilation is not None else title
77aa6b32 100
ab3176af
S
101 thumbnails = [{
102 'url': preview['url'],
103 'id': preview.get('content_format'),
104 } for preview in result.get('preview', []) if preview.get('url')]
105
106 webpage = self._download_webpage(url, video_id)
107
108 episode_number = int_or_none(self._search_regex(
109 r'<meta[^>]+itemprop="episode"[^>]*>\s*<meta[^>]+itemprop="episodeNumber"[^>]+content="(\d+)',
110 webpage, 'episode number', default=None))
77aa6b32 111
ab3176af
S
112 description = self._og_search_description(webpage, default=None) or self._html_search_meta(
113 'description', webpage, 'description', default=None)
77aa6b32 114
115 return {
116 'id': video_id,
117 'title': title,
ab3176af
S
118 'series': compilation,
119 'episode': episode,
120 'episode_number': episode_number,
121 'thumbnails': thumbnails,
77aa6b32 122 'description': description,
123 'duration': duration,
77aa6b32 124 'formats': formats,
125 }
126
127
128class IviCompilationIE(InfoExtractor):
ceb2b7d2 129 IE_DESC = 'ivi.ru compilations'
130 IE_NAME = 'ivi:compilation'
84dd7031 131 _VALID_URL = r'https?://(?:www\.)?ivi\.ru/watch/(?!\d+)(?P<compilationid>[a-z\d_-]+)(?:/season(?P<seasonid>\d+))?$'
22a6f150
PH
132 _TESTS = [{
133 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa',
134 'info_dict': {
135 'id': 'dvoe_iz_lartsa',
136 'title': 'Двое из ларца (2006 - 2008)',
137 },
138 'playlist_mincount': 24,
139 }, {
140 'url': 'http://www.ivi.ru/watch/dvoe_iz_lartsa/season1',
141 'info_dict': {
142 'id': 'dvoe_iz_lartsa/season1',
143 'title': 'Двое из ларца (2006 - 2008) 1 сезон',
144 },
145 'playlist_mincount': 12,
146 }]
77aa6b32 147
148 def _extract_entries(self, html, compilation_id):
149 return [self.url_result('http://www.ivi.ru/watch/%s/%s' % (compilation_id, serie), 'Ivi')
150 for serie in re.findall(r'<strong><a href="/watch/%s/(\d+)">(?:[^<]+)</a></strong>' % compilation_id, html)]
151
152 def _real_extract(self, url):
153 mobj = re.match(self._VALID_URL, url)
154 compilation_id = mobj.group('compilationid')
155 season_id = mobj.group('seasonid')
156
5f6a1245 157 if season_id is not None: # Season link
ceb2b7d2 158 season_page = self._download_webpage(url, compilation_id, 'Downloading season %s web page' % season_id)
77aa6b32 159 playlist_id = '%s/season%s' % (compilation_id, season_id)
ceb2b7d2 160 playlist_title = self._html_search_meta('title', season_page, 'title')
77aa6b32 161 entries = self._extract_entries(season_page, compilation_id)
5f6a1245 162 else: # Compilation link
ceb2b7d2 163 compilation_page = self._download_webpage(url, compilation_id, 'Downloading compilation web page')
77aa6b32 164 playlist_id = compilation_id
ceb2b7d2 165 playlist_title = self._html_search_meta('title', compilation_page, 'title')
77aa6b32 166 seasons = re.findall(r'<a href="/watch/%s/season(\d+)">[^<]+</a>' % compilation_id, compilation_page)
5f6a1245 167 if len(seasons) == 0: # No seasons in this compilation
77aa6b32 168 entries = self._extract_entries(compilation_page, compilation_id)
169 else:
170 entries = []
171 for season_id in seasons:
ceb2b7d2 172 season_page = self._download_webpage(
173 'http://www.ivi.ru/watch/%s/season%s' % (compilation_id, season_id),
174 compilation_id, 'Downloading season %s web page' % season_id)
77aa6b32 175 entries.extend(self._extract_entries(season_page, compilation_id))
176
5f6a1245 177 return self.playlist_result(entries, playlist_id, playlist_title)