]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/dplay.py
d/p/remove-autoupdate-mechanism.patch: Update patch with metadata at the top.
[youtubedl] / youtube_dl / extractor / dplay.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import re
6 import time
7
8 from .common import InfoExtractor
9 from ..compat import (
10 compat_HTTPError,
11 compat_str,
12 compat_urlparse,
13 )
14 from ..utils import (
15 determine_ext,
16 ExtractorError,
17 float_or_none,
18 int_or_none,
19 remove_end,
20 try_get,
21 unified_strdate,
22 unified_timestamp,
23 update_url_query,
24 USER_AGENTS,
25 )
26
27
28 class DPlayIE(InfoExtractor):
29 _VALID_URL = r'https?://(?P<domain>www\.(?P<host>dplay\.(?P<country>dk|se|no)))/(?:video(?:er|s)/)?(?P<id>[^/]+/[^/?#]+)'
30
31 _TESTS = [{
32 # non geo restricted, via secure api, unsigned download hls URL
33 'url': 'http://www.dplay.se/nugammalt-77-handelser-som-format-sverige/season-1-svensken-lar-sig-njuta-av-livet/',
34 'info_dict': {
35 'id': '3172',
36 'display_id': 'nugammalt-77-handelser-som-format-sverige/season-1-svensken-lar-sig-njuta-av-livet',
37 'ext': 'mp4',
38 'title': 'Svensken lär sig njuta av livet',
39 'description': 'md5:d3819c9bccffd0fe458ca42451dd50d8',
40 'duration': 2650,
41 'timestamp': 1365454320,
42 'upload_date': '20130408',
43 'creator': 'Kanal 5 (Home)',
44 'series': 'Nugammalt - 77 händelser som format Sverige',
45 'season_number': 1,
46 'episode_number': 1,
47 'age_limit': 0,
48 },
49 }, {
50 # geo restricted, via secure api, unsigned download hls URL
51 'url': 'http://www.dplay.dk/mig-og-min-mor/season-6-episode-12/',
52 'info_dict': {
53 'id': '70816',
54 'display_id': 'mig-og-min-mor/season-6-episode-12',
55 'ext': 'mp4',
56 'title': 'Episode 12',
57 'description': 'md5:9c86e51a93f8a4401fc9641ef9894c90',
58 'duration': 2563,
59 'timestamp': 1429696800,
60 'upload_date': '20150422',
61 'creator': 'Kanal 4 (Home)',
62 'series': 'Mig og min mor',
63 'season_number': 6,
64 'episode_number': 12,
65 'age_limit': 0,
66 },
67 }, {
68 # geo restricted, via direct unsigned hls URL
69 'url': 'http://www.dplay.no/pga-tour/season-1-hoydepunkter-18-21-februar/',
70 'only_matching': True,
71 }, {
72 # disco-api
73 'url': 'https://www.dplay.no/videoer/i-kongens-klr/sesong-1-episode-7',
74 'info_dict': {
75 'id': '40206',
76 'display_id': 'i-kongens-klr/sesong-1-episode-7',
77 'ext': 'mp4',
78 'title': 'Episode 7',
79 'description': 'md5:e3e1411b2b9aebeea36a6ec5d50c60cf',
80 'duration': 2611.16,
81 'timestamp': 1516726800,
82 'upload_date': '20180123',
83 'series': 'I kongens klær',
84 'season_number': 1,
85 'episode_number': 7,
86 },
87 'params': {
88 'format': 'bestvideo',
89 'skip_download': True,
90 },
91 }, {
92
93 'url': 'https://www.dplay.dk/videoer/singleliv/season-5-episode-3',
94 'only_matching': True,
95 }, {
96 'url': 'https://www.dplay.se/videos/sofias-anglar/sofias-anglar-1001',
97 'only_matching': True,
98 }]
99
100 def _real_extract(self, url):
101 mobj = re.match(self._VALID_URL, url)
102 display_id = mobj.group('id')
103 domain = mobj.group('domain')
104
105 self._initialize_geo_bypass([mobj.group('country').upper()])
106
107 webpage = self._download_webpage(url, display_id)
108
109 video_id = self._search_regex(
110 r'data-video-id=["\'](\d+)', webpage, 'video id', default=None)
111
112 if not video_id:
113 host = mobj.group('host')
114 disco_base = 'https://disco-api.%s' % host
115 self._download_json(
116 '%s/token' % disco_base, display_id, 'Downloading token',
117 query={
118 'realm': host.replace('.', ''),
119 })
120 video = self._download_json(
121 '%s/content/videos/%s' % (disco_base, display_id), display_id,
122 headers={
123 'Referer': url,
124 'x-disco-client': 'WEB:UNKNOWN:dplay-client:0.0.1',
125 }, query={
126 'include': 'show'
127 })
128 video_id = video['data']['id']
129 info = video['data']['attributes']
130 title = info['name']
131 formats = []
132 for format_id, format_dict in self._download_json(
133 '%s/playback/videoPlaybackInfo/%s' % (disco_base, video_id),
134 display_id)['data']['attributes']['streaming'].items():
135 if not isinstance(format_dict, dict):
136 continue
137 format_url = format_dict.get('url')
138 if not format_url:
139 continue
140 ext = determine_ext(format_url)
141 if format_id == 'dash' or ext == 'mpd':
142 formats.extend(self._extract_mpd_formats(
143 format_url, display_id, mpd_id='dash', fatal=False))
144 elif format_id == 'hls' or ext == 'm3u8':
145 formats.extend(self._extract_m3u8_formats(
146 format_url, display_id, 'mp4',
147 entry_protocol='m3u8_native', m3u8_id='hls',
148 fatal=False))
149 else:
150 formats.append({
151 'url': format_url,
152 'format_id': format_id,
153 })
154 self._sort_formats(formats)
155
156 series = None
157 try:
158 included = video.get('included')
159 if isinstance(included, list):
160 show = next(e for e in included if e.get('type') == 'show')
161 series = try_get(
162 show, lambda x: x['attributes']['name'], compat_str)
163 except StopIteration:
164 pass
165
166 return {
167 'id': video_id,
168 'display_id': display_id,
169 'title': title,
170 'description': info.get('description'),
171 'duration': float_or_none(
172 info.get('videoDuration'), scale=1000),
173 'timestamp': unified_timestamp(info.get('publishStart')),
174 'series': series,
175 'season_number': int_or_none(info.get('seasonNumber')),
176 'episode_number': int_or_none(info.get('episodeNumber')),
177 'age_limit': int_or_none(info.get('minimum_age')),
178 'formats': formats,
179 }
180
181 info = self._download_json(
182 'http://%s/api/v2/ajax/videos?video_id=%s' % (domain, video_id),
183 video_id)['data'][0]
184
185 title = info['title']
186
187 PROTOCOLS = ('hls', 'hds')
188 formats = []
189
190 def extract_formats(protocol, manifest_url):
191 if protocol == 'hls':
192 m3u8_formats = self._extract_m3u8_formats(
193 manifest_url, video_id, ext='mp4',
194 entry_protocol='m3u8_native', m3u8_id=protocol, fatal=False)
195 # Sometimes final URLs inside m3u8 are unsigned, let's fix this
196 # ourselves. Also fragments' URLs are only served signed for
197 # Safari user agent.
198 query = compat_urlparse.parse_qs(compat_urlparse.urlparse(manifest_url).query)
199 for m3u8_format in m3u8_formats:
200 m3u8_format.update({
201 'url': update_url_query(m3u8_format['url'], query),
202 'http_headers': {
203 'User-Agent': USER_AGENTS['Safari'],
204 },
205 })
206 formats.extend(m3u8_formats)
207 elif protocol == 'hds':
208 formats.extend(self._extract_f4m_formats(
209 manifest_url + '&hdcore=3.8.0&plugin=flowplayer-3.8.0.0',
210 video_id, f4m_id=protocol, fatal=False))
211
212 domain_tld = domain.split('.')[-1]
213 if domain_tld in ('se', 'dk', 'no'):
214 for protocol in PROTOCOLS:
215 # Providing dsc-geo allows to bypass geo restriction in some cases
216 self._set_cookie(
217 'secure.dplay.%s' % domain_tld, 'dsc-geo',
218 json.dumps({
219 'countryCode': domain_tld.upper(),
220 'expiry': (time.time() + 20 * 60) * 1000,
221 }))
222 stream = self._download_json(
223 'https://secure.dplay.%s/secure/api/v2/user/authorization/stream/%s?stream_type=%s'
224 % (domain_tld, video_id, protocol), video_id,
225 'Downloading %s stream JSON' % protocol, fatal=False)
226 if stream and stream.get(protocol):
227 extract_formats(protocol, stream[protocol])
228
229 # The last resort is to try direct unsigned hls/hds URLs from info dictionary.
230 # Sometimes this does work even when secure API with dsc-geo has failed (e.g.
231 # http://www.dplay.no/pga-tour/season-1-hoydepunkter-18-21-februar/).
232 if not formats:
233 for protocol in PROTOCOLS:
234 if info.get(protocol):
235 extract_formats(protocol, info[protocol])
236
237 self._sort_formats(formats)
238
239 subtitles = {}
240 for lang in ('se', 'sv', 'da', 'nl', 'no'):
241 for format_id in ('web_vtt', 'vtt', 'srt'):
242 subtitle_url = info.get('subtitles_%s_%s' % (lang, format_id))
243 if subtitle_url:
244 subtitles.setdefault(lang, []).append({'url': subtitle_url})
245
246 return {
247 'id': video_id,
248 'display_id': display_id,
249 'title': title,
250 'description': info.get('video_metadata_longDescription'),
251 'duration': int_or_none(info.get('video_metadata_length'), scale=1000),
252 'timestamp': int_or_none(info.get('video_publish_date')),
253 'creator': info.get('video_metadata_homeChannel'),
254 'series': info.get('video_metadata_show'),
255 'season_number': int_or_none(info.get('season')),
256 'episode_number': int_or_none(info.get('episode')),
257 'age_limit': int_or_none(info.get('minimum_age')),
258 'formats': formats,
259 'subtitles': subtitles,
260 }
261
262
263 class DPlayItIE(InfoExtractor):
264 _VALID_URL = r'https?://it\.dplay\.com/[^/]+/[^/]+/(?P<id>[^/?#]+)'
265 _GEO_COUNTRIES = ['IT']
266 _TEST = {
267 'url': 'http://it.dplay.com/nove/biografie-imbarazzanti/luigi-di-maio-la-psicosi-di-stanislawskij/',
268 'md5': '2b808ffb00fc47b884a172ca5d13053c',
269 'info_dict': {
270 'id': '6918',
271 'display_id': 'luigi-di-maio-la-psicosi-di-stanislawskij',
272 'ext': 'mp4',
273 'title': 'Biografie imbarazzanti: Luigi Di Maio: la psicosi di Stanislawskij',
274 'description': 'md5:3c7a4303aef85868f867a26f5cc14813',
275 'thumbnail': r're:^https?://.*\.jpe?g',
276 'upload_date': '20160524',
277 'series': 'Biografie imbarazzanti',
278 'season_number': 1,
279 'episode': 'Luigi Di Maio: la psicosi di Stanislawskij',
280 'episode_number': 1,
281 },
282 }
283
284 def _real_extract(self, url):
285 display_id = self._match_id(url)
286
287 webpage = self._download_webpage(url, display_id)
288
289 title = remove_end(self._og_search_title(webpage), ' | Dplay')
290
291 video_id = None
292
293 info = self._search_regex(
294 r'playback_json\s*:\s*JSON\.parse\s*\(\s*("(?:\\.|[^"\\])+?")',
295 webpage, 'playback JSON', default=None)
296 if info:
297 for _ in range(2):
298 info = self._parse_json(info, display_id, fatal=False)
299 if not info:
300 break
301 else:
302 video_id = try_get(info, lambda x: x['data']['id'])
303
304 if not info:
305 info_url = self._search_regex(
306 r'url\s*[:=]\s*["\']((?:https?:)?//[^/]+/playback/videoPlaybackInfo/\d+)',
307 webpage, 'info url')
308
309 video_id = info_url.rpartition('/')[-1]
310
311 try:
312 info = self._download_json(
313 info_url, display_id, headers={
314 'Authorization': 'Bearer %s' % self._get_cookies(url).get(
315 'dplayit_token').value,
316 'Referer': url,
317 })
318 except ExtractorError as e:
319 if isinstance(e.cause, compat_HTTPError) and e.cause.code in (400, 403):
320 info = self._parse_json(e.cause.read().decode('utf-8'), display_id)
321 error = info['errors'][0]
322 if error.get('code') == 'access.denied.geoblocked':
323 self.raise_geo_restricted(
324 msg=error.get('detail'), countries=self._GEO_COUNTRIES)
325 raise ExtractorError(info['errors'][0]['detail'], expected=True)
326 raise
327
328 hls_url = info['data']['attributes']['streaming']['hls']['url']
329
330 formats = self._extract_m3u8_formats(
331 hls_url, display_id, ext='mp4', entry_protocol='m3u8_native',
332 m3u8_id='hls')
333
334 series = self._html_search_regex(
335 r'(?s)<h1[^>]+class=["\'].*?\bshow_title\b.*?["\'][^>]*>(.+?)</h1>',
336 webpage, 'series', fatal=False)
337 episode = self._search_regex(
338 r'<p[^>]+class=["\'].*?\bdesc_ep\b.*?["\'][^>]*>\s*<br/>\s*<b>([^<]+)',
339 webpage, 'episode', fatal=False)
340
341 mobj = re.search(
342 r'(?s)<span[^>]+class=["\']dates["\'][^>]*>.+?\bS\.(?P<season_number>\d+)\s+E\.(?P<episode_number>\d+)\s*-\s*(?P<upload_date>\d{2}/\d{2}/\d{4})',
343 webpage)
344 if mobj:
345 season_number = int(mobj.group('season_number'))
346 episode_number = int(mobj.group('episode_number'))
347 upload_date = unified_strdate(mobj.group('upload_date'))
348 else:
349 season_number = episode_number = upload_date = None
350
351 return {
352 'id': compat_str(video_id or display_id),
353 'display_id': display_id,
354 'title': title,
355 'description': self._og_search_description(webpage),
356 'thumbnail': self._og_search_thumbnail(webpage),
357 'series': series,
358 'season_number': season_number,
359 'episode': episode,
360 'episode_number': episode_number,
361 'upload_date': upload_date,
362 'formats': formats,
363 }