]> jfr.im git - yt-dlp.git/blame - youtube_dlc/extractor/dplay.py
[skip travis] renaming
[yt-dlp.git] / youtube_dlc / extractor / dplay.py
CommitLineData
940b606a 1# coding: utf-8
4cd759f7
JMF
2from __future__ import unicode_literals
3
940b606a 4import re
4cd759f7
JMF
5
6from .common import InfoExtractor
0b98f3a7 7from ..compat import compat_HTTPError
5448b781 8from ..utils import (
864a4576 9 determine_ext,
0cf2352e 10 ExtractorError,
864a4576 11 float_or_none,
5448b781 12 int_or_none,
864a4576 13 unified_timestamp,
5448b781 14)
940b606a 15
4cd759f7
JMF
16
17class DPlayIE(InfoExtractor):
0b98f3a7
RA
18 _VALID_URL = r'''(?x)https?://
19 (?P<domain>
20 (?:www\.)?(?P<host>dplay\.(?P<country>dk|fi|jp|se|no))|
21 (?P<subdomain_country>es|it)\.dplay\.com
22 )/[^/]+/(?P<id>[^/]+/[^/?#]+)'''
95050537 23
940b606a 24 _TESTS = [{
5448b781 25 # non geo restricted, via secure api, unsigned download hls URL
0b98f3a7 26 'url': 'https://www.dplay.se/videos/nugammalt-77-handelser-som-format-sverige/nugammalt-77-handelser-som-format-sverige-101',
940b606a 27 'info_dict': {
0b98f3a7
RA
28 'id': '13628',
29 'display_id': 'nugammalt-77-handelser-som-format-sverige/nugammalt-77-handelser-som-format-sverige-101',
5448b781 30 'ext': 'mp4',
940b606a
S
31 'title': 'Svensken lär sig njuta av livet',
32 'description': 'md5:d3819c9bccffd0fe458ca42451dd50d8',
0b98f3a7
RA
33 'duration': 2649.856,
34 'timestamp': 1365453720,
940b606a 35 'upload_date': '20130408',
0b98f3a7 36 'creator': 'Kanal 5',
940b606a
S
37 'series': 'Nugammalt - 77 händelser som format Sverige',
38 'season_number': 1,
39 'episode_number': 1,
0b98f3a7
RA
40 },
41 'params': {
42 'format': 'bestvideo',
43 'skip_download': True,
95050537 44 },
940b606a 45 }, {
5448b781 46 # geo restricted, via secure api, unsigned download hls URL
0b98f3a7 47 'url': 'http://www.dplay.dk/videoer/ted-bundy-mind-of-a-monster/ted-bundy-mind-of-a-monster',
940b606a 48 'info_dict': {
0b98f3a7
RA
49 'id': '104465',
50 'display_id': 'ted-bundy-mind-of-a-monster/ted-bundy-mind-of-a-monster',
5448b781 51 'ext': 'mp4',
0b98f3a7
RA
52 'title': 'Ted Bundy: Mind Of A Monster',
53 'description': 'md5:8b780f6f18de4dae631668b8a9637995',
54 'duration': 5290.027,
55 'timestamp': 1570694400,
56 'upload_date': '20191010',
57 'creator': 'ID - Investigation Discovery',
58 'series': 'Ted Bundy: Mind Of A Monster',
59 'season_number': 1,
60 'episode_number': 1,
61 },
62 'params': {
63 'format': 'bestvideo',
64 'skip_download': True,
940b606a 65 },
864a4576
S
66 }, {
67 # disco-api
68 'url': 'https://www.dplay.no/videoer/i-kongens-klr/sesong-1-episode-7',
69 'info_dict': {
70 'id': '40206',
71 'display_id': 'i-kongens-klr/sesong-1-episode-7',
72 'ext': 'mp4',
73 'title': 'Episode 7',
74 'description': 'md5:e3e1411b2b9aebeea36a6ec5d50c60cf',
75 'duration': 2611.16,
76 'timestamp': 1516726800,
77 'upload_date': '20180123',
78 'series': 'I kongens klær',
79 'season_number': 1,
80 'episode_number': 7,
81 },
82 'params': {
83 'format': 'bestvideo',
84 'skip_download': True,
85 },
0b98f3a7 86 'skip': 'Available for Premium users',
a0ee342b 87 }, {
0b98f3a7
RA
88 'url': 'http://it.dplay.com/nove/biografie-imbarazzanti/luigi-di-maio-la-psicosi-di-stanislawskij/',
89 'md5': '2b808ffb00fc47b884a172ca5d13053c',
90 'info_dict': {
91 'id': '6918',
92 'display_id': 'biografie-imbarazzanti/luigi-di-maio-la-psicosi-di-stanislawskij',
93 'ext': 'mp4',
94 'title': 'Luigi Di Maio: la psicosi di Stanislawskij',
95 'description': 'md5:3c7a4303aef85868f867a26f5cc14813',
96 'thumbnail': r're:^https?://.*\.jpe?g',
97 'upload_date': '20160524',
98 'timestamp': 1464076800,
99 'series': 'Biografie imbarazzanti',
100 'season_number': 1,
101 'episode': 'Episode 1',
102 'episode_number': 1,
103 },
104 }, {
105 'url': 'https://es.dplay.com/dmax/la-fiebre-del-oro/temporada-8-episodio-1/',
106 'info_dict': {
107 'id': '21652',
108 'display_id': 'la-fiebre-del-oro/temporada-8-episodio-1',
109 'ext': 'mp4',
110 'title': 'Episodio 1',
111 'description': 'md5:b9dcff2071086e003737485210675f69',
112 'thumbnail': r're:^https?://.*\.png',
113 'upload_date': '20180709',
114 'timestamp': 1531173540,
115 'series': 'La fiebre del oro',
116 'season_number': 8,
117 'episode': 'Episode 1',
118 'episode_number': 1,
119 },
120 'params': {
121 'skip_download': True,
122 },
123 }, {
124 'url': 'https://www.dplay.fi/videot/shifting-gears-with-aaron-kaufman/episode-16',
a0ee342b 125 'only_matching': True,
d6b15291 126 }, {
0b98f3a7 127 'url': 'https://www.dplay.jp/video/gold-rush/24086',
d6b15291 128 'only_matching': True,
940b606a 129 }]
4cd759f7 130
0b98f3a7
RA
131 def _get_disco_api_info(self, url, display_id, disco_host, realm, country):
132 geo_countries = [country.upper()]
133 self._initialize_geo_bypass({
134 'countries': geo_countries,
135 })
136 disco_base = 'https://%s/' % disco_host
aa560616 137 token = self._download_json(
0b98f3a7 138 disco_base + 'token', display_id, 'Downloading token',
aa560616
RA
139 query={
140 'realm': realm,
141 })['data']['attributes']['token']
142 headers = {
143 'Referer': url,
144 'Authorization': 'Bearer ' + token,
145 }
146 video = self._download_json(
0b98f3a7 147 disco_base + 'content/videos/' + display_id, display_id,
aa560616 148 headers=headers, query={
2318629b
RA
149 'fields[channel]': 'name',
150 'fields[image]': 'height,src,width',
151 'fields[show]': 'name',
152 'fields[tag]': 'name',
153 'fields[video]': 'description,episodeNumber,name,publishStart,seasonNumber,videoDuration',
0b98f3a7 154 'include': 'images,primaryChannel,show,tags'
aa560616
RA
155 })
156 video_id = video['data']['id']
157 info = video['data']['attributes']
0b98f3a7 158 title = info['name'].strip()
aa560616 159 formats = []
0b98f3a7
RA
160 try:
161 streaming = self._download_json(
162 disco_base + 'playback/videoPlaybackInfo/' + video_id,
163 display_id, headers=headers)['data']['attributes']['streaming']
164 except ExtractorError as e:
165 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
166 info = self._parse_json(e.cause.read().decode('utf-8'), display_id)
167 error = info['errors'][0]
168 error_code = error.get('code')
169 if error_code == 'access.denied.geoblocked':
170 self.raise_geo_restricted(countries=geo_countries)
171 elif error_code == 'access.denied.missingpackage':
172 self.raise_login_required()
173 raise ExtractorError(info['errors'][0]['detail'], expected=True)
174 raise
175 for format_id, format_dict in streaming.items():
aa560616
RA
176 if not isinstance(format_dict, dict):
177 continue
178 format_url = format_dict.get('url')
179 if not format_url:
180 continue
181 ext = determine_ext(format_url)
182 if format_id == 'dash' or ext == 'mpd':
183 formats.extend(self._extract_mpd_formats(
184 format_url, display_id, mpd_id='dash', fatal=False))
185 elif format_id == 'hls' or ext == 'm3u8':
186 formats.extend(self._extract_m3u8_formats(
187 format_url, display_id, 'mp4',
188 entry_protocol='m3u8_native', m3u8_id='hls',
189 fatal=False))
190 else:
191 formats.append({
192 'url': format_url,
193 'format_id': format_id,
194 })
195 self._sort_formats(formats)
196
0b98f3a7
RA
197 creator = series = None
198 tags = []
199 thumbnails = []
200 included = video.get('included') or []
201 if isinstance(included, list):
202 for e in included:
203 attributes = e.get('attributes')
204 if not attributes:
205 continue
206 e_type = e.get('type')
207 if e_type == 'channel':
208 creator = attributes.get('name')
209 elif e_type == 'image':
210 src = attributes.get('src')
211 if src:
212 thumbnails.append({
213 'url': src,
214 'width': int_or_none(attributes.get('width')),
215 'height': int_or_none(attributes.get('height')),
216 })
217 if e_type == 'show':
218 series = attributes.get('name')
219 elif e_type == 'tag':
220 name = attributes.get('name')
221 if name:
222 tags.append(name)
aa560616
RA
223
224 return {
225 'id': video_id,
226 'display_id': display_id,
227 'title': title,
228 'description': info.get('description'),
0b98f3a7 229 'duration': float_or_none(info.get('videoDuration'), 1000),
aa560616
RA
230 'timestamp': unified_timestamp(info.get('publishStart')),
231 'series': series,
232 'season_number': int_or_none(info.get('seasonNumber')),
233 'episode_number': int_or_none(info.get('episodeNumber')),
0b98f3a7
RA
234 'creator': creator,
235 'tags': tags,
236 'thumbnails': thumbnails,
aa560616
RA
237 'formats': formats,
238 }
239
4cd759f7 240 def _real_extract(self, url):
940b606a
S
241 mobj = re.match(self._VALID_URL, url)
242 display_id = mobj.group('id')
0b98f3a7
RA
243 domain = mobj.group('domain').lstrip('www.')
244 country = mobj.group('country') or mobj.group('subdomain_country')
245 host = 'disco-api.' + domain if domain.startswith('dplay.') else 'eu2-prod.disco-api.com'
246 return self._get_disco_api_info(
247 url, display_id, host, 'dplay' + country, country)