]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/viewster.py
52dd95e2fe19041f01e49b1a4a3f9566c1f0e60b
[youtubedl] / youtube_dl / extractor / viewster.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8 compat_HTTPError,
9 compat_urllib_parse_unquote,
10 )
11 from ..utils import (
12 determine_ext,
13 ExtractorError,
14 int_or_none,
15 parse_iso8601,
16 sanitized_Request,
17 HEADRequest,
18 url_basename,
19 )
20
21
22 class ViewsterIE(InfoExtractor):
23 _VALID_URL = r'https?://(?:www\.)?viewster\.com/(?:serie|movie)/(?P<id>\d+-\d+-\d+)'
24 _TESTS = [{
25 # movie, Type=Movie
26 'url': 'http://www.viewster.com/movie/1140-11855-000/the-listening-project/',
27 'md5': 'e642d1b27fcf3a4ffa79f194f5adde36',
28 'info_dict': {
29 'id': '1140-11855-000',
30 'ext': 'mp4',
31 'title': 'The listening Project',
32 'description': 'md5:bac720244afd1a8ea279864e67baa071',
33 'timestamp': 1214870400,
34 'upload_date': '20080701',
35 'duration': 4680,
36 },
37 }, {
38 # series episode, Type=Episode
39 'url': 'http://www.viewster.com/serie/1284-19427-001/the-world-and-a-wall/',
40 'md5': '9243079a8531809efe1b089db102c069',
41 'info_dict': {
42 'id': '1284-19427-001',
43 'ext': 'mp4',
44 'title': 'The World and a Wall',
45 'description': 'md5:24814cf74d3453fdf5bfef9716d073e3',
46 'timestamp': 1428192000,
47 'upload_date': '20150405',
48 'duration': 1500,
49 },
50 }, {
51 # serie, Type=Serie
52 'url': 'http://www.viewster.com/serie/1303-19426-000/',
53 'info_dict': {
54 'id': '1303-19426-000',
55 'title': 'Is It Wrong to Try to Pick up Girls in a Dungeon?',
56 'description': 'md5:eeda9bef25b0d524b3a29a97804c2f11',
57 },
58 'playlist_count': 13,
59 }, {
60 # unfinished serie, no Type
61 'url': 'http://www.viewster.com/serie/1284-19427-000/baby-steps-season-2/',
62 'info_dict': {
63 'id': '1284-19427-000',
64 'title': 'Baby Steps—Season 2',
65 'description': 'md5:e7097a8fc97151e25f085c9eb7a1cdb1',
66 },
67 'playlist_mincount': 16,
68 }, {
69 # geo restricted series
70 'url': 'https://www.viewster.com/serie/1280-18794-002/',
71 'only_matching': True,
72 }, {
73 # geo restricted video
74 'url': 'https://www.viewster.com/serie/1280-18794-002/what-is-extraterritoriality-lawo/',
75 'only_matching': True,
76 }]
77
78 _ACCEPT_HEADER = 'application/json, text/javascript, */*; q=0.01'
79
80 def _download_json(self, url, video_id, note='Downloading JSON metadata', fatal=True, query={}):
81 request = sanitized_Request(url)
82 request.add_header('Accept', self._ACCEPT_HEADER)
83 request.add_header('Auth-token', self._AUTH_TOKEN)
84 return super(ViewsterIE, self)._download_json(request, video_id, note, fatal=fatal, query=query)
85
86 def _real_extract(self, url):
87 video_id = self._match_id(url)
88 # Get 'api_token' cookie
89 self._request_webpage(HEADRequest('http://www.viewster.com/'), video_id)
90 cookies = self._get_cookies('http://www.viewster.com/')
91 self._AUTH_TOKEN = compat_urllib_parse_unquote(cookies['api_token'].value)
92
93 info = self._download_json(
94 'https://public-api.viewster.com/search/%s' % video_id,
95 video_id, 'Downloading entry JSON')
96
97 entry_id = info.get('Id') or info['id']
98
99 # unfinished serie has no Type
100 if info.get('Type') in ('Serie', None):
101 try:
102 episodes = self._download_json(
103 'https://public-api.viewster.com/series/%s/episodes' % entry_id,
104 video_id, 'Downloading series JSON')
105 except ExtractorError as e:
106 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
107 self.raise_geo_restricted()
108 else:
109 raise
110 entries = [
111 self.url_result(
112 'http://www.viewster.com/movie/%s' % episode['OriginId'], 'Viewster')
113 for episode in episodes]
114 title = (info.get('Title') or info['Synopsis']['Title']).strip()
115 description = info.get('Synopsis', {}).get('Detailed')
116 return self.playlist_result(entries, video_id, title, description)
117
118 formats = []
119 for language_set in info.get('LanguageSets', []):
120 manifest_url = None
121 m3u8_formats = []
122 audio = language_set.get('Audio') or ''
123 subtitle = language_set.get('Subtitle') or ''
124 base_format_id = audio
125 if subtitle:
126 base_format_id += '-%s' % subtitle
127
128 def concat(suffix, sep='-'):
129 return (base_format_id + '%s%s' % (sep, suffix)) if base_format_id else suffix
130
131 for media_type in ('application/f4m+xml', 'application/x-mpegURL', 'video/mp4'):
132 media = self._download_json(
133 'https://public-api.viewster.com/movies/%s/video' % entry_id,
134 video_id, 'Downloading %s JSON' % concat(media_type, ' '), fatal=False, query={
135 'mediaType': media_type,
136 'language': audio,
137 'subtitle': subtitle,
138 })
139 if not media:
140 continue
141 video_url = media.get('Uri')
142 if not video_url:
143 continue
144 ext = determine_ext(video_url)
145 if ext == 'f4m':
146 manifest_url = video_url
147 video_url += '&' if '?' in video_url else '?'
148 video_url += 'hdcore=3.2.0&plugin=flowplayer-3.2.0.1'
149 formats.extend(self._extract_f4m_formats(
150 video_url, video_id, f4m_id=concat('hds')))
151 elif ext == 'm3u8':
152 manifest_url = video_url
153 m3u8_formats = self._extract_m3u8_formats(
154 video_url, video_id, 'mp4', m3u8_id=concat('hls'),
155 fatal=False) # m3u8 sometimes fail
156 if m3u8_formats:
157 formats.extend(m3u8_formats)
158 else:
159 qualities_basename = self._search_regex(
160 r'/([^/]+)\.csmil/',
161 manifest_url, 'qualities basename', default=None)
162 if not qualities_basename:
163 continue
164 QUALITIES_RE = r'((,\d+k)+,?)'
165 qualities = self._search_regex(
166 QUALITIES_RE, qualities_basename,
167 'qualities', default=None)
168 if not qualities:
169 continue
170 qualities = list(map(lambda q: int(q[:-1]), qualities.strip(',').split(',')))
171 qualities.sort()
172 http_template = re.sub(QUALITIES_RE, r'%dk', qualities_basename)
173 http_url_basename = url_basename(video_url)
174 if m3u8_formats:
175 self._sort_formats(m3u8_formats)
176 m3u8_formats = list(filter(
177 lambda f: f.get('vcodec') != 'none' and f.get('resolution') != 'multiple',
178 m3u8_formats))
179 if len(qualities) == len(m3u8_formats):
180 for q, m3u8_format in zip(qualities, m3u8_formats):
181 f = m3u8_format.copy()
182 f.update({
183 'url': video_url.replace(http_url_basename, http_template % q),
184 'format_id': f['format_id'].replace('hls', 'http'),
185 'protocol': 'http',
186 })
187 formats.append(f)
188 else:
189 for q in qualities:
190 formats.append({
191 'url': video_url.replace(http_url_basename, http_template % q),
192 'ext': 'mp4',
193 'format_id': 'http-%d' % q,
194 'tbr': q,
195 })
196
197 if not formats and not info.get('VODSettings'):
198 self.raise_geo_restricted()
199
200 self._sort_formats(formats)
201
202 synopsis = info.get('Synopsis') or {}
203 # Prefer title outside synopsis since it's less messy
204 title = (info.get('Title') or synopsis['Title']).strip()
205 description = synopsis.get('Detailed') or (info.get('Synopsis') or {}).get('Short')
206 duration = int_or_none(info.get('Duration'))
207 timestamp = parse_iso8601(info.get('ReleaseDate'))
208
209 return {
210 'id': video_id,
211 'title': title,
212 'description': description,
213 'timestamp': timestamp,
214 'duration': duration,
215 'formats': formats,
216 }