218f1020947c42c6ae611e7b1609abdf06ba6a11
[youtube-dl] / youtube_dl / extractor / drtv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import binascii
5 import hashlib
6 import re
7
8
9 from .common import InfoExtractor
10 from ..aes import aes_cbc_decrypt
11 from ..compat import compat_urllib_parse_unquote
12 from ..utils import (
13     bytes_to_intlist,
14     ExtractorError,
15     int_or_none,
16     intlist_to_bytes,
17     float_or_none,
18     mimetype2ext,
19     str_or_none,
20     unified_timestamp,
21     update_url_query,
22     url_or_none,
23 )
24
25
26 class DRTVIE(InfoExtractor):
27     _VALID_URL = r'https?://(?:www\.)?dr\.dk/(?:tv/se|nyheder|radio(?:/ondemand)?)/(?:[^/]+/)*(?P<id>[\da-z-]+)(?:[/#?]|$)'
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,
49         },
50         'expected_warnings': ['Unable to download f4m manifest'],
51     }, {
52         # embed
53         'url': 'https://www.dr.dk/nyheder/indland/live-christianias-rydning-af-pusher-street-er-i-gang',
54         'info_dict': {
55             'id': 'urn:dr:mu:programcard:57c926176187a50a9c6e83c6',
56             'ext': 'mp4',
57             'title': 'christiania pusher street ryddes drdkrjpo',
58             'description': 'md5:2a71898b15057e9b97334f61d04e6eb5',
59             'timestamp': 1472800279,
60             'upload_date': '20160902',
61             'duration': 131.4,
62         },
63         'params': {
64             'skip_download': True,
65         },
66         'expected_warnings': ['Unable to download f4m manifest'],
67     }, {
68         # with SignLanguage formats
69         'url': 'https://www.dr.dk/tv/se/historien-om-danmark/-/historien-om-danmark-stenalder',
70         'info_dict': {
71             'id': 'historien-om-danmark-stenalder',
72             'ext': 'mp4',
73             'title': 'Historien om Danmark: Stenalder',
74             'description': 'md5:8c66dcbc1669bbc6f873879880f37f2a',
75             'timestamp': 1546628400,
76             'upload_date': '20190104',
77             'duration': 3502.56,
78             'formats': 'mincount:20',
79         },
80         'params': {
81             'skip_download': True,
82         },
83     }, {
84         'url': 'https://www.dr.dk/radio/p4kbh/regionale-nyheder-kh4/p4-nyheder-2019-06-26-17-30-9',
85         'only_matching': True,
86     }]
87
88     def _real_extract(self, url):
89         video_id = self._match_id(url)
90
91         webpage = self._download_webpage(url, video_id)
92
93         if '>Programmet er ikke længere tilgængeligt' in webpage:
94             raise ExtractorError(
95                 'Video %s is not available' % video_id, expected=True)
96
97         video_id = self._search_regex(
98             (r'data-(?:material-identifier|episode-slug)="([^"]+)"',
99              r'data-resource="[^>"]+mu/programcard/expanded/([^"]+)"'),
100             webpage, 'video id', default=None)
101
102         if not video_id:
103             video_id = compat_urllib_parse_unquote(self._search_regex(
104                 r'(urn(?:%3A|:)dr(?:%3A|:)mu(?:%3A|:)programcard(?:%3A|:)[\da-f]+)',
105                 webpage, 'urn'))
106
107         data = self._download_json(
108             'https://www.dr.dk/mu-online/api/1.4/programcard/%s' % video_id,
109             video_id, 'Downloading video JSON', query={'expanded': 'true'})
110
111         title = str_or_none(data.get('Title')) or re.sub(
112             r'\s*\|\s*(?:TV\s*\|\s*DR|DRTV)$', '',
113             self._og_search_title(webpage))
114         description = self._og_search_description(
115             webpage, default=None) or data.get('Description')
116
117         timestamp = unified_timestamp(
118             data.get('PrimaryBroadcastStartTime') or data.get('SortDateTime'))
119
120         thumbnail = None
121         duration = None
122
123         restricted_to_denmark = False
124
125         formats = []
126         subtitles = {}
127
128         assets = []
129         primary_asset = data.get('PrimaryAsset')
130         if isinstance(primary_asset, dict):
131             assets.append(primary_asset)
132         secondary_assets = data.get('SecondaryAssets')
133         if isinstance(secondary_assets, list):
134             for secondary_asset in secondary_assets:
135                 if isinstance(secondary_asset, dict):
136                     assets.append(secondary_asset)
137
138         def hex_to_bytes(hex):
139             return binascii.a2b_hex(hex.encode('ascii'))
140
141         def decrypt_uri(e):
142             n = int(e[2:10], 16)
143             a = e[10 + n:]
144             data = bytes_to_intlist(hex_to_bytes(e[10:10 + n]))
145             key = bytes_to_intlist(hashlib.sha256(
146                 ('%s:sRBzYNXBzkKgnjj8pGtkACch' % a).encode('utf-8')).digest())
147             iv = bytes_to_intlist(hex_to_bytes(a))
148             decrypted = aes_cbc_decrypt(data, key, iv)
149             return intlist_to_bytes(
150                 decrypted[:-decrypted[-1]]).decode('utf-8').split('?')[0]
151
152         for asset in assets:
153             kind = asset.get('Kind')
154             if kind == 'Image':
155                 thumbnail = url_or_none(asset.get('Uri'))
156             elif kind in ('VideoResource', 'AudioResource'):
157                 duration = float_or_none(asset.get('DurationInMilliseconds'), 1000)
158                 restricted_to_denmark = asset.get('RestrictedToDenmark')
159                 asset_target = asset.get('Target')
160                 for link in asset.get('Links', []):
161                     uri = link.get('Uri')
162                     if not uri:
163                         encrypted_uri = link.get('EncryptedUri')
164                         if not encrypted_uri:
165                             continue
166                         try:
167                             uri = decrypt_uri(encrypted_uri)
168                         except Exception:
169                             self.report_warning(
170                                 'Unable to decrypt EncryptedUri', video_id)
171                             continue
172                     uri = url_or_none(uri)
173                     if not uri:
174                         continue
175                     target = link.get('Target')
176                     format_id = target or ''
177                     if asset_target in ('SpokenSubtitles', 'SignLanguage', 'VisuallyInterpreted'):
178                         preference = -1
179                         format_id += '-%s' % asset_target
180                     elif asset_target == 'Default':
181                         preference = 1
182                     else:
183                         preference = None
184                     if target == 'HDS':
185                         f4m_formats = self._extract_f4m_formats(
186                             uri + '?hdcore=3.3.0&plugin=aasp-3.3.0.99.43',
187                             video_id, preference, f4m_id=format_id, fatal=False)
188                         if kind == 'AudioResource':
189                             for f in f4m_formats:
190                                 f['vcodec'] = 'none'
191                         formats.extend(f4m_formats)
192                     elif target == 'HLS':
193                         formats.extend(self._extract_m3u8_formats(
194                             uri, video_id, 'mp4', entry_protocol='m3u8_native',
195                             preference=preference, m3u8_id=format_id,
196                             fatal=False))
197                     else:
198                         bitrate = link.get('Bitrate')
199                         if bitrate:
200                             format_id += '-%s' % bitrate
201                         formats.append({
202                             'url': uri,
203                             'format_id': format_id,
204                             'tbr': int_or_none(bitrate),
205                             'ext': link.get('FileFormat'),
206                             'vcodec': 'none' if kind == 'AudioResource' else None,
207                             'preference': preference,
208                         })
209             subtitles_list = asset.get('SubtitlesList') or asset.get('Subtitleslist')
210             if isinstance(subtitles_list, list):
211                 LANGS = {
212                     'Danish': 'da',
213                 }
214                 for subs in subtitles_list:
215                     if not isinstance(subs, dict):
216                         continue
217                     sub_uri = url_or_none(subs.get('Uri'))
218                     if not sub_uri:
219                         continue
220                     lang = subs.get('Language') or 'da'
221                     subtitles.setdefault(LANGS.get(lang, lang), []).append({
222                         'url': sub_uri,
223                         'ext': mimetype2ext(subs.get('MimeType')) or 'vtt'
224                     })
225
226         if not formats and restricted_to_denmark:
227             self.raise_geo_restricted(
228                 'Unfortunately, DR is not allowed to show this program outside Denmark.',
229                 countries=self._GEO_COUNTRIES)
230
231         self._sort_formats(formats)
232
233         return {
234             'id': video_id,
235             'title': title,
236             'description': description,
237             'thumbnail': thumbnail,
238             'timestamp': timestamp,
239             'duration': duration,
240             'formats': formats,
241             'subtitles': subtitles,
242             'series': str_or_none(data.get('SeriesTitle')),
243             'season': str_or_none(data.get('SeasonTitle')),
244             'season_number': int_or_none(data.get('SeasonNumber')),
245             'season_id': str_or_none(data.get('SeasonUrn')),
246             'episode': str_or_none(data.get('EpisodeTitle')),
247             'episode_number': int_or_none(data.get('EpisodeNumber')),
248             'release_year': int_or_none(data.get('ProductionYear')),
249         }
250
251
252 class DRTVLiveIE(InfoExtractor):
253     IE_NAME = 'drtv:live'
254     _VALID_URL = r'https?://(?:www\.)?dr\.dk/(?:tv|TV)/live/(?P<id>[\da-z-]+)'
255     _GEO_COUNTRIES = ['DK']
256     _TEST = {
257         'url': 'https://www.dr.dk/tv/live/dr1',
258         'info_dict': {
259             'id': 'dr1',
260             'ext': 'mp4',
261             'title': 're:^DR1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
262         },
263         'params': {
264             # m3u8 download
265             'skip_download': True,
266         },
267     }
268
269     def _real_extract(self, url):
270         channel_id = self._match_id(url)
271         channel_data = self._download_json(
272             'https://www.dr.dk/mu-online/api/1.0/channel/' + channel_id,
273             channel_id)
274         title = self._live_title(channel_data['Title'])
275
276         formats = []
277         for streaming_server in channel_data.get('StreamingServers', []):
278             server = streaming_server.get('Server')
279             if not server:
280                 continue
281             link_type = streaming_server.get('LinkType')
282             for quality in streaming_server.get('Qualities', []):
283                 for stream in quality.get('Streams', []):
284                     stream_path = stream.get('Stream')
285                     if not stream_path:
286                         continue
287                     stream_url = update_url_query(
288                         '%s/%s' % (server, stream_path), {'b': ''})
289                     if link_type == 'HLS':
290                         formats.extend(self._extract_m3u8_formats(
291                             stream_url, channel_id, 'mp4',
292                             m3u8_id=link_type, fatal=False, live=True))
293                     elif link_type == 'HDS':
294                         formats.extend(self._extract_f4m_formats(update_url_query(
295                             '%s/%s' % (server, stream_path), {'hdcore': '3.7.0'}),
296                             channel_id, f4m_id=link_type, fatal=False))
297         self._sort_formats(formats)
298
299         return {
300             'id': channel_id,
301             'title': title,
302             'thumbnail': channel_data.get('PrimaryImageUri'),
303             'formats': formats,
304             'is_live': True,
305         }