Fix "invalid escape sequences" error on Python 3.6
[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
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': r'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': r're:^https?://.*\.jpg$',
50             },
51             'skip': 'Only works from Russia',
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,
63                 'thumbnail': r're:^https?://.*\.jpg$',
64             },
65             'skip': 'Only works from Russia',
66         }
67     ]
68
69     # Sorted by quality
70     _KNOWN_FORMATS = (
71         'MP4-low-mobile', 'MP4-mobile', 'FLV-lo', 'MP4-lo', 'FLV-hi', 'MP4-hi',
72         'MP4-SHQ', 'MP4-HD720', 'MP4-HD1080')
73
74     def _real_extract(self, url):
75         video_id = self._match_id(url)
76
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
84                 }
85             ]
86         }
87
88         video_json = self._download_json(
89             'http://api.digitalaccess.ru/api/json/', video_id,
90             'Downloading video JSON', data=json.dumps(data))
91
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)
96             raise ExtractorError(
97                 'Unable to download video %s: %s' % (video_id, error['message']),
98                 expected=True)
99
100         result = video_json['result']
101
102         quality = qualities(self._KNOWN_FORMATS)
103
104         formats = [{
105             'url': x['url'],
106             'format_id': x.get('content_format'),
107             'quality': quality(x.get('content_format')),
108         } for x in result['files'] if x.get('url')]
109
110         self._sort_formats(formats)
111
112         title = result['title']
113
114         duration = int_or_none(result.get('duration'))
115         compilation = result.get('compilation')
116         episode = title if compilation else None
117
118         title = '%s - %s' % (compilation, title) if compilation is not None else title
119
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
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
134         episode_number = int_or_none(self._search_regex(
135             r'[^>]+itemprop="episode"[^>]*>\s*<meta[^>]+itemprop="episodeNumber"[^>]+content="(\d+)',
136             webpage, 'episode number', default=None))
137
138         description = self._og_search_description(webpage, default=None) or self._html_search_meta(
139             'description', webpage, 'description', default=None)
140
141         return {
142             'id': video_id,
143             'title': title,
144             'series': compilation,
145             'season': season,
146             'season_number': season_number,
147             'episode': episode,
148             'episode_number': episode_number,
149             'thumbnails': thumbnails,
150             'description': description,
151             'duration': duration,
152             'formats': formats,
153         }
154
155
156 class IviCompilationIE(InfoExtractor):
157     IE_DESC = 'ivi.ru compilations'
158     IE_NAME = 'ivi:compilation'
159     _VALID_URL = r'https?://(?:www\.)?ivi\.ru/watch/(?!\d+)(?P<compilationid>[a-z\d_-]+)(?:/season(?P<seasonid>\d+))?$'
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     }]
175
176     def _extract_entries(self, html, compilation_id):
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)]
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
188         if season_id is not None:  # Season link
189             season_page = self._download_webpage(
190                 url, compilation_id, 'Downloading season %s web page' % season_id)
191             playlist_id = '%s/season%s' % (compilation_id, season_id)
192             playlist_title = self._html_search_meta('title', season_page, 'title')
193             entries = self._extract_entries(season_page, compilation_id)
194         else:  # Compilation link
195             compilation_page = self._download_webpage(url, compilation_id, 'Downloading compilation web page')
196             playlist_id = compilation_id
197             playlist_title = self._html_search_meta('title', compilation_page, 'title')
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
201                 entries = self._extract_entries(compilation_page, compilation_id)
202             else:
203                 entries = []
204                 for season_id in seasons:
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)
208                     entries.extend(self._extract_entries(season_page, compilation_id))
209
210         return self.playlist_result(entries, playlist_id, playlist_title)