[ie/dropout] Fix extraction (#12102)
[yt-dlp.git] / yt_dlp / extractor / drtv.py
blobba86eb2b49a053c5ed8f499bc4e0659e4ab6a7d7
1 import json
2 import uuid
4 from .common import InfoExtractor
5 from ..utils import (
6 ExtractorError,
7 int_or_none,
8 mimetype2ext,
9 parse_iso8601,
10 try_call,
11 update_url_query,
12 url_or_none,
14 from ..utils.traversal import traverse_obj
16 SERIES_API = 'https://production-cdn.dr-massive.com/api/page?device=web_browser&item_detail_expand=all&lang=da&max_list_prefetch=3&path=%s'
19 class DRTVIE(InfoExtractor):
20 _VALID_URL = r'''(?x)
21 https?://
22 (?:
23 (?:www\.)?dr\.dk/tv/se(?:/ondemand)?/(?:[^/?#]+/)*|
24 (?:www\.)?(?:dr\.dk|dr-massive\.com)/drtv/(?:se|episode|program)/
26 (?P<id>[\da-z_-]+)
27 '''
28 _GEO_BYPASS = False
29 _GEO_COUNTRIES = ['DK']
30 IE_NAME = 'drtv'
31 _TESTS = [{
32 'url': 'https://www.dr.dk/tv/se/boern/ultra/klassen-ultra/klassen-darlig-taber-10',
33 'md5': '25e659cccc9a2ed956110a299fdf5983',
34 'info_dict': {
35 'id': 'klassen-darlig-taber-10',
36 'ext': 'mp4',
37 'title': 'Klassen - DÃ¥rlig taber (10)',
38 'description': 'md5:815fe1b7fa656ed80580f31e8b3c79aa',
39 'timestamp': 1539085800,
40 'upload_date': '20181009',
41 'duration': 606.84,
42 'series': 'Klassen',
43 'season': 'Klassen I',
44 'season_number': 1,
45 'season_id': 'urn:dr:mu:bundle:57d7e8216187a4031cfd6f6b',
46 'episode': 'Episode 10',
47 'episode_number': 10,
48 'release_year': 2016,
50 'expected_warnings': ['Unable to download f4m manifest'],
51 'skip': 'this video has been removed',
52 }, {
53 # with SignLanguage formats
54 'url': 'https://www.dr.dk/tv/se/historien-om-danmark/-/historien-om-danmark-stenalder',
55 'info_dict': {
56 'id': '00831690010',
57 'ext': 'mp4',
58 'title': 'Historien om Danmark: Stenalder',
59 'description': 'md5:8c66dcbc1669bbc6f873879880f37f2a',
60 'timestamp': 1546628400,
61 'upload_date': '20190104',
62 'duration': 3504.619,
63 'formats': 'mincount:20',
64 'release_year': 2017,
65 'season_id': 'urn:dr:mu:bundle:5afc03ad6187a4065ca5fd35',
66 'season_number': 1,
67 'season': 'Historien om Danmark',
68 'series': 'Historien om Danmark',
70 'skip': 'this video has been removed',
71 }, {
72 'url': 'https://www.dr.dk/drtv/se/frank-and-kastaniegaarden_71769',
73 'info_dict': {
74 'id': '00951930010',
75 'ext': 'mp4',
76 'title': 'Frank & Kastaniegaarden',
77 'description': 'md5:974e1780934cf3275ef10280204bccb0',
78 'release_timestamp': 1546545600,
79 'release_date': '20190103',
80 'duration': 2576,
81 'season': 'Frank & Kastaniegaarden',
82 'season_id': '67125',
83 'release_year': 2019,
84 'season_number': 2019,
85 'series': 'Frank & Kastaniegaarden',
86 'episode_number': 1,
87 'episode': 'Frank & Kastaniegaarden',
88 'thumbnail': r're:https?://.+',
90 'params': {
91 'skip_download': True,
93 }, {
94 # Foreign and Regular subtitle track
95 'url': 'https://www.dr.dk/drtv/se/spise-med-price_-pasta-selv_397445',
96 'info_dict': {
97 'id': '00212301010',
98 'ext': 'mp4',
99 'episode_number': 1,
100 'title': 'Spise med Price: Pasta Selv',
101 'alt_title': '1. Pasta Selv',
102 'release_date': '20230807',
103 'description': 'md5:2da9060524fed707810d71080b3d0cd8',
104 'duration': 1750,
105 'season': 'Spise med Price',
106 'release_timestamp': 1691438400,
107 'season_id': '397440',
108 'episode': 'Spise med Price: Pasta Selv',
109 'thumbnail': r're:https?://.+',
110 'season_number': 15,
111 'series': 'Spise med Price',
112 'release_year': 2022,
113 'subtitles': 'mincount:2',
115 'params': {
116 'skip_download': 'm3u8',
118 }, {
119 'url': 'https://www.dr.dk/drtv/episode/bonderoeven_71769',
120 'only_matching': True,
121 }, {
122 'url': 'https://dr-massive.com/drtv/se/bonderoeven_71769',
123 'only_matching': True,
124 }, {
125 'url': 'https://www.dr.dk/drtv/program/jagten_220924',
126 'only_matching': True,
129 SUBTITLE_LANGS = {
130 'DanishLanguageSubtitles': 'da',
131 'ForeignLanguageSubtitles': 'da_foreign',
132 'CombinedLanguageSubtitles': 'da_combined',
135 _TOKEN = None
137 def _real_initialize(self):
138 if self._TOKEN:
139 return
141 token_response = self._download_json(
142 'https://isl.dr-massive.com/api/authorization/anonymous-sso', None,
143 note='Downloading anonymous token', headers={
144 'content-type': 'application/json',
145 }, query={
146 'device': 'phone_android',
147 'lang': 'da',
148 'supportFallbackToken': 'true',
149 }, data=json.dumps({
150 'deviceId': str(uuid.uuid4()),
151 'scopes': ['Catalog'],
152 'optout': True,
153 }).encode())
155 self._TOKEN = traverse_obj(
156 token_response, (lambda _, x: x['type'] == 'UserAccount', 'value', {str}), get_all=False)
157 if not self._TOKEN:
158 raise ExtractorError('Unable to get anonymous token')
160 def _real_extract(self, url):
161 url_slug = self._match_id(url)
162 webpage = self._download_webpage(url, url_slug)
164 json_data = self._search_json(
165 r'window\.__data\s*=', webpage, 'data', url_slug, fatal=False) or {}
166 item = traverse_obj(
167 json_data, ('cache', 'page', ..., (None, ('entries', 0)), 'item', {dict}), get_all=False)
168 if item:
169 item_id = item.get('id')
170 else:
171 item_id = url_slug.rsplit('_', 1)[-1]
172 item = self._download_json(
173 f'https://production-cdn.dr-massive.com/api/items/{item_id}', item_id,
174 note='Attempting to download backup item data', query={
175 'device': 'web_browser',
176 'expand': 'all',
177 'ff': 'idp,ldp,rpt',
178 'geoLocation': 'dk',
179 'isDeviceAbroad': 'false',
180 'lang': 'da',
181 'segments': 'drtv,optedout',
182 'sub': 'Anonymous',
185 video_id = try_call(lambda: item['customId'].rsplit(':', 1)[-1]) or item_id
186 stream_data = self._download_json(
187 f'https://production.dr-massive.com/api/account/items/{item_id}/videos', video_id,
188 note='Downloading stream data', query={
189 'delivery': 'stream',
190 'device': 'web_browser',
191 'ff': 'idp,ldp,rpt',
192 'lang': 'da',
193 'resolution': 'HD-1080',
194 'sub': 'Anonymous',
195 }, headers={'authorization': f'Bearer {self._TOKEN}'})
197 formats = []
198 subtitles = {}
199 for stream in traverse_obj(stream_data, (lambda _, x: x['url'])):
200 format_id = stream.get('format', 'na')
201 access_service = stream.get('accessService')
202 preference = None
203 subtitle_suffix = ''
204 if access_service in ('SpokenSubtitles', 'SignLanguage', 'VisuallyInterpreted'):
205 preference = -1
206 format_id += f'-{access_service}'
207 subtitle_suffix = f'-{access_service}'
208 elif access_service == 'StandardVideo':
209 preference = 1
210 fmts, subs = self._extract_m3u8_formats_and_subtitles(
211 stream.get('url'), video_id, ext='mp4', preference=preference, m3u8_id=format_id, fatal=False)
212 formats.extend(fmts)
214 api_subtitles = traverse_obj(stream, ('subtitles', lambda _, v: url_or_none(v['link']), {dict}))
215 if not api_subtitles:
216 self._merge_subtitles(subs, target=subtitles)
218 for sub_track in api_subtitles:
219 lang = sub_track.get('language') or 'da'
220 subtitles.setdefault(self.SUBTITLE_LANGS.get(lang, lang) + subtitle_suffix, []).append({
221 'url': sub_track['link'],
222 'ext': mimetype2ext(sub_track.get('format')) or 'vtt',
225 if not formats and traverse_obj(item, ('season', 'customFields', 'IsGeoRestricted')):
226 self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
228 return {
229 'id': video_id,
230 'formats': formats,
231 'subtitles': subtitles,
232 **traverse_obj(item, {
233 'title': 'title',
234 'alt_title': 'contextualTitle',
235 'description': 'description',
236 'thumbnail': ('images', 'wallpaper'),
237 'release_timestamp': ('customFields', 'BroadcastTimeDK', {parse_iso8601}),
238 'duration': ('duration', {int_or_none}),
239 'series': ('season', 'show', 'title'),
240 'season': ('season', 'title'),
241 'season_number': ('season', 'seasonNumber', {int_or_none}),
242 'season_id': 'seasonId',
243 'episode': 'episodeName',
244 'episode_number': ('episodeNumber', {int_or_none}),
245 'release_year': ('releaseYear', {int_or_none}),
250 class DRTVLiveIE(InfoExtractor):
251 IE_NAME = 'drtv:live'
252 _VALID_URL = r'https?://(?:www\.)?dr\.dk/(?:tv|TV)/live/(?P<id>[\da-z-]+)'
253 _GEO_COUNTRIES = ['DK']
254 _TEST = {
255 'url': 'https://www.dr.dk/tv/live/dr1',
256 'info_dict': {
257 'id': 'dr1',
258 'ext': 'mp4',
259 'title': 're:^DR1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
261 'params': {
262 # m3u8 download
263 'skip_download': True,
267 def _real_extract(self, url):
268 channel_id = self._match_id(url)
269 channel_data = self._download_json(
270 'https://www.dr.dk/mu-online/api/1.0/channel/' + channel_id,
271 channel_id)
272 title = channel_data['Title']
274 formats = []
275 for streaming_server in channel_data.get('StreamingServers', []):
276 server = streaming_server.get('Server')
277 if not server:
278 continue
279 link_type = streaming_server.get('LinkType')
280 for quality in streaming_server.get('Qualities', []):
281 for stream in quality.get('Streams', []):
282 stream_path = stream.get('Stream')
283 if not stream_path:
284 continue
285 stream_url = update_url_query(
286 f'{server}/{stream_path}', {'b': ''})
287 if link_type == 'HLS':
288 formats.extend(self._extract_m3u8_formats(
289 stream_url, channel_id, 'mp4',
290 m3u8_id=link_type, fatal=False, live=True))
291 elif link_type == 'HDS':
292 formats.extend(self._extract_f4m_formats(update_url_query(
293 f'{server}/{stream_path}', {'hdcore': '3.7.0'}),
294 channel_id, f4m_id=link_type, fatal=False))
296 return {
297 'id': channel_id,
298 'title': title,
299 'thumbnail': channel_data.get('PrimaryImageUri'),
300 'formats': formats,
301 'is_live': True,
305 class DRTVSeasonIE(InfoExtractor):
306 IE_NAME = 'drtv:season'
307 _VALID_URL = r'https?://(?:www\.)?(?:dr\.dk|dr-massive\.com)/drtv/saeson/(?P<display_id>[\w-]+)_(?P<id>\d+)'
308 _GEO_COUNTRIES = ['DK']
309 _TESTS = [{
310 'url': 'https://www.dr.dk/drtv/saeson/frank-and-kastaniegaarden_9008',
311 'info_dict': {
312 'id': '9008',
313 'display_id': 'frank-and-kastaniegaarden',
314 'title': 'Frank & Kastaniegaarden',
315 'series': 'Frank & Kastaniegaarden',
316 'season_number': 2008,
317 'alt_title': 'Season 2008',
319 'playlist_mincount': 8,
320 }, {
321 'url': 'https://www.dr.dk/drtv/saeson/frank-and-kastaniegaarden_8761',
322 'info_dict': {
323 'id': '8761',
324 'display_id': 'frank-and-kastaniegaarden',
325 'title': 'Frank & Kastaniegaarden',
326 'series': 'Frank & Kastaniegaarden',
327 'season_number': 2009,
328 'alt_title': 'Season 2009',
330 'playlist_mincount': 19,
333 def _real_extract(self, url):
334 display_id, season_id = self._match_valid_url(url).group('display_id', 'id')
335 data = self._download_json(SERIES_API % f'/saeson/{display_id}_{season_id}', display_id)
337 entries = [{
338 '_type': 'url',
339 'url': f'https://www.dr.dk/drtv{episode["path"]}',
340 'ie_key': DRTVIE.ie_key(),
341 'title': episode.get('title'),
342 'alt_title': episode.get('contextualTitle'),
343 'episode': episode.get('episodeName'),
344 'description': episode.get('shortDescription'),
345 'series': traverse_obj(data, ('entries', 0, 'item', 'title')),
346 'season_number': traverse_obj(data, ('entries', 0, 'item', 'seasonNumber')),
347 'episode_number': episode.get('episodeNumber'),
348 } for episode in traverse_obj(data, ('entries', 0, 'item', 'episodes', 'items'))]
350 return {
351 '_type': 'playlist',
352 'id': season_id,
353 'display_id': display_id,
354 'title': traverse_obj(data, ('entries', 0, 'item', 'title')),
355 'alt_title': traverse_obj(data, ('entries', 0, 'item', 'contextualTitle')),
356 'series': traverse_obj(data, ('entries', 0, 'item', 'title')),
357 'entries': entries,
358 'season_number': traverse_obj(data, ('entries', 0, 'item', 'seasonNumber')),
362 class DRTVSeriesIE(InfoExtractor):
363 IE_NAME = 'drtv:series'
364 _VALID_URL = r'https?://(?:www\.)?(?:dr\.dk|dr-massive\.com)/drtv/serie/(?P<display_id>[\w-]+)_(?P<id>\d+)'
365 _GEO_COUNTRIES = ['DK']
366 _TESTS = [{
367 'url': 'https://www.dr.dk/drtv/serie/frank-and-kastaniegaarden_6954',
368 'info_dict': {
369 'id': '6954',
370 'display_id': 'frank-and-kastaniegaarden',
371 'title': 'Frank & Kastaniegaarden',
372 'series': 'Frank & Kastaniegaarden',
373 'alt_title': '',
375 'playlist_mincount': 15,
378 def _real_extract(self, url):
379 display_id, series_id = self._match_valid_url(url).group('display_id', 'id')
380 data = self._download_json(SERIES_API % f'/serie/{display_id}_{series_id}', display_id)
382 entries = [{
383 '_type': 'url',
384 'url': f'https://www.dr.dk/drtv{season.get("path")}',
385 'ie_key': DRTVSeasonIE.ie_key(),
386 'title': season.get('title'),
387 'alt_title': season.get('contextualTitle'),
388 'series': traverse_obj(data, ('entries', 0, 'item', 'title')),
389 'season_number': traverse_obj(data, ('entries', 0, 'item', 'seasonNumber')),
390 } for season in traverse_obj(data, ('entries', 0, 'item', 'show', 'seasons', 'items'))]
392 return {
393 '_type': 'playlist',
394 'id': series_id,
395 'display_id': display_id,
396 'title': traverse_obj(data, ('entries', 0, 'item', 'title')),
397 'alt_title': traverse_obj(data, ('entries', 0, 'item', 'contextualTitle')),
398 'series': traverse_obj(data, ('entries', 0, 'item', 'title')),
399 'entries': entries,