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