Merge pull request #12861 from Tithen-Firion/cbsinteractive-fix
[youtube-dl] / youtube_dl / extractor / drtv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     ExtractorError,
7     int_or_none,
8     float_or_none,
9     mimetype2ext,
10     parse_iso8601,
11     remove_end,
12     update_url_query,
13 )
14
15
16 class DRTVIE(InfoExtractor):
17     _VALID_URL = r'https?://(?:www\.)?dr\.dk/(?:tv/se|nyheder|radio/ondemand)/(?:[^/]+/)*(?P<id>[\da-z-]+)(?:[/#?]|$)'
18     _GEO_BYPASS = False
19     _GEO_COUNTRIES = ['DK']
20     IE_NAME = 'drtv'
21     _TESTS = [{
22         'url': 'https://www.dr.dk/tv/se/boern/ultra/klassen-ultra/klassen-darlig-taber-10',
23         'md5': '7ae17b4e18eb5d29212f424a7511c184',
24         'info_dict': {
25             'id': 'klassen-darlig-taber-10',
26             'ext': 'mp4',
27             'title': 'Klassen - Dårlig taber (10)',
28             'description': 'md5:815fe1b7fa656ed80580f31e8b3c79aa',
29             'timestamp': 1471991907,
30             'upload_date': '20160823',
31             'duration': 606.84,
32         },
33     }, {
34         # embed
35         'url': 'https://www.dr.dk/nyheder/indland/live-christianias-rydning-af-pusher-street-er-i-gang',
36         'info_dict': {
37             'id': 'christiania-pusher-street-ryddes-drdkrjpo',
38             'ext': 'mp4',
39             'title': 'LIVE Christianias rydning af Pusher Street er i gang',
40             'description': 'md5:2a71898b15057e9b97334f61d04e6eb5',
41             'timestamp': 1472800279,
42             'upload_date': '20160902',
43             'duration': 131.4,
44         },
45         'params': {
46             'skip_download': True,
47         },
48     }, {
49         # with SignLanguage formats
50         'url': 'https://www.dr.dk/tv/se/historien-om-danmark/-/historien-om-danmark-stenalder',
51         'info_dict': {
52             'id': 'historien-om-danmark-stenalder',
53             'ext': 'mp4',
54             'title': 'Historien om Danmark: Stenalder (1)',
55             'description': 'md5:8c66dcbc1669bbc6f873879880f37f2a',
56             'timestamp': 1490401996,
57             'upload_date': '20170325',
58             'duration': 3502.04,
59             'formats': 'mincount:20',
60         },
61         'params': {
62             'skip_download': True,
63         },
64     }]
65
66     def _real_extract(self, url):
67         video_id = self._match_id(url)
68
69         webpage = self._download_webpage(url, video_id)
70
71         if '>Programmet er ikke længere tilgængeligt' in webpage:
72             raise ExtractorError(
73                 'Video %s is not available' % video_id, expected=True)
74
75         video_id = self._search_regex(
76             (r'data-(?:material-identifier|episode-slug)="([^"]+)"',
77                 r'data-resource="[^>"]+mu/programcard/expanded/([^"]+)"'),
78             webpage, 'video id')
79
80         programcard = self._download_json(
81             'http://www.dr.dk/mu/programcard/expanded/%s' % video_id,
82             video_id, 'Downloading video JSON')
83         data = programcard['Data'][0]
84
85         title = remove_end(self._og_search_title(
86             webpage, default=None), ' | TV | DR') or data['Title']
87         description = self._og_search_description(
88             webpage, default=None) or data.get('Description')
89
90         timestamp = parse_iso8601(data.get('CreatedTime'))
91
92         thumbnail = None
93         duration = None
94
95         restricted_to_denmark = False
96
97         formats = []
98         subtitles = {}
99
100         for asset in data['Assets']:
101             kind = asset.get('Kind')
102             if kind == 'Image':
103                 thumbnail = asset.get('Uri')
104             elif kind in ('VideoResource', 'AudioResource'):
105                 duration = float_or_none(asset.get('DurationInMilliseconds'), 1000)
106                 restricted_to_denmark = asset.get('RestrictedToDenmark')
107                 asset_target = asset.get('Target')
108                 for link in asset.get('Links', []):
109                     uri = link.get('Uri')
110                     if not uri:
111                         continue
112                     target = link.get('Target')
113                     format_id = target or ''
114                     preference = None
115                     if asset_target in ('SpokenSubtitles', 'SignLanguage'):
116                         preference = -1
117                         format_id += '-%s' % asset_target
118                     if target == 'HDS':
119                         f4m_formats = self._extract_f4m_formats(
120                             uri + '?hdcore=3.3.0&plugin=aasp-3.3.0.99.43',
121                             video_id, preference, f4m_id=format_id)
122                         if kind == 'AudioResource':
123                             for f in f4m_formats:
124                                 f['vcodec'] = 'none'
125                         formats.extend(f4m_formats)
126                     elif target == 'HLS':
127                         formats.extend(self._extract_m3u8_formats(
128                             uri, video_id, 'mp4', entry_protocol='m3u8_native',
129                             preference=preference, m3u8_id=format_id))
130                     else:
131                         bitrate = link.get('Bitrate')
132                         if bitrate:
133                             format_id += '-%s' % bitrate
134                         formats.append({
135                             'url': uri,
136                             'format_id': format_id,
137                             'tbr': int_or_none(bitrate),
138                             'ext': link.get('FileFormat'),
139                             'vcodec': 'none' if kind == 'AudioResource' else None,
140                         })
141                 subtitles_list = asset.get('SubtitlesList')
142                 if isinstance(subtitles_list, list):
143                     LANGS = {
144                         'Danish': 'da',
145                     }
146                     for subs in subtitles_list:
147                         if not subs.get('Uri'):
148                             continue
149                         lang = subs.get('Language') or 'da'
150                         subtitles.setdefault(LANGS.get(lang, lang), []).append({
151                             'url': subs['Uri'],
152                             'ext': mimetype2ext(subs.get('MimeType')) or 'vtt'
153                         })
154
155         if not formats and restricted_to_denmark:
156             self.raise_geo_restricted(
157                 'Unfortunately, DR is not allowed to show this program outside Denmark.',
158                 countries=self._GEO_COUNTRIES)
159
160         self._sort_formats(formats)
161
162         return {
163             'id': video_id,
164             'title': title,
165             'description': description,
166             'thumbnail': thumbnail,
167             'timestamp': timestamp,
168             'duration': duration,
169             'formats': formats,
170             'subtitles': subtitles,
171         }
172
173
174 class DRTVLiveIE(InfoExtractor):
175     IE_NAME = 'drtv:live'
176     _VALID_URL = r'https?://(?:www\.)?dr\.dk/(?:tv|TV)/live/(?P<id>[\da-z-]+)'
177     _GEO_COUNTRIES = ['DK']
178     _TEST = {
179         'url': 'https://www.dr.dk/tv/live/dr1',
180         'info_dict': {
181             'id': 'dr1',
182             'ext': 'mp4',
183             'title': 're:^DR1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
184         },
185         'params': {
186             # m3u8 download
187             'skip_download': True,
188         },
189     }
190
191     def _real_extract(self, url):
192         channel_id = self._match_id(url)
193         channel_data = self._download_json(
194             'https://www.dr.dk/mu-online/api/1.0/channel/' + channel_id,
195             channel_id)
196         title = self._live_title(channel_data['Title'])
197
198         formats = []
199         for streaming_server in channel_data.get('StreamingServers', []):
200             server = streaming_server.get('Server')
201             if not server:
202                 continue
203             link_type = streaming_server.get('LinkType')
204             for quality in streaming_server.get('Qualities', []):
205                 for stream in quality.get('Streams', []):
206                     stream_path = stream.get('Stream')
207                     if not stream_path:
208                         continue
209                     stream_url = update_url_query(
210                         '%s/%s' % (server, stream_path), {'b': ''})
211                     if link_type == 'HLS':
212                         formats.extend(self._extract_m3u8_formats(
213                             stream_url, channel_id, 'mp4',
214                             m3u8_id=link_type, fatal=False, live=True))
215                     elif link_type == 'HDS':
216                         formats.extend(self._extract_f4m_formats(update_url_query(
217                             '%s/%s' % (server, stream_path), {'hdcore': '3.7.0'}),
218                             channel_id, f4m_id=link_type, fatal=False))
219         self._sort_formats(formats)
220
221         return {
222             'id': channel_id,
223             'title': title,
224             'thumbnail': channel_data.get('PrimaryImageUri'),
225             'formats': formats,
226             'is_live': True,
227         }