]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/drtv.py
Prepare to release.
[youtubedl] / 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': '25e659cccc9a2ed956110a299fdf5983',
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 'params': {
34 'skip_download': True,
35 },
36 }, {
37 'url': 'https://www.dr.dk/nyheder/indland/live-christianias-rydning-af-pusher-street-er-i-gang',
38 'md5': '2c37175c718155930f939ef59952474a',
39 'info_dict': {
40 'id': 'christiania-pusher-street-ryddes-drdkrjpo',
41 'ext': 'mp4',
42 'title': 'LIVE Christianias rydning af Pusher Street er i gang',
43 'description': '- Det er det fedeste, der er sket i 20 år, fortæller christianit til DR Nyheder.',
44 'timestamp': 1472800279,
45 'upload_date': '20160902',
46 'duration': 131.4,
47 },
48 }]
49
50 def _real_extract(self, url):
51 video_id = self._match_id(url)
52
53 webpage = self._download_webpage(url, video_id)
54
55 if '>Programmet er ikke længere tilgængeligt' in webpage:
56 raise ExtractorError(
57 'Video %s is not available' % video_id, expected=True)
58
59 video_id = self._search_regex(
60 (r'data-(?:material-identifier|episode-slug)="([^"]+)"',
61 r'data-resource="[^>"]+mu/programcard/expanded/([^"]+)"'),
62 webpage, 'video id')
63
64 programcard = self._download_json(
65 'http://www.dr.dk/mu/programcard/expanded/%s' % video_id,
66 video_id, 'Downloading video JSON')
67 data = programcard['Data'][0]
68
69 title = remove_end(self._og_search_title(
70 webpage, default=None), ' | TV | DR') or data['Title']
71 description = self._og_search_description(
72 webpage, default=None) or data.get('Description')
73
74 timestamp = parse_iso8601(data.get('CreatedTime'))
75
76 thumbnail = None
77 duration = None
78
79 restricted_to_denmark = False
80
81 formats = []
82 subtitles = {}
83
84 for asset in data['Assets']:
85 kind = asset.get('Kind')
86 if kind == 'Image':
87 thumbnail = asset.get('Uri')
88 elif kind in ('VideoResource', 'AudioResource'):
89 duration = float_or_none(asset.get('DurationInMilliseconds'), 1000)
90 restricted_to_denmark = asset.get('RestrictedToDenmark')
91 spoken_subtitles = asset.get('Target') == 'SpokenSubtitles'
92 for link in asset.get('Links', []):
93 uri = link.get('Uri')
94 if not uri:
95 continue
96 target = link.get('Target')
97 format_id = target or ''
98 preference = None
99 if spoken_subtitles:
100 preference = -1
101 format_id += '-spoken-subtitles'
102 if target == 'HDS':
103 f4m_formats = self._extract_f4m_formats(
104 uri + '?hdcore=3.3.0&plugin=aasp-3.3.0.99.43',
105 video_id, preference, f4m_id=format_id)
106 if kind == 'AudioResource':
107 for f in f4m_formats:
108 f['vcodec'] = 'none'
109 formats.extend(f4m_formats)
110 elif target == 'HLS':
111 formats.extend(self._extract_m3u8_formats(
112 uri, video_id, 'mp4', entry_protocol='m3u8_native',
113 preference=preference, m3u8_id=format_id))
114 else:
115 bitrate = link.get('Bitrate')
116 if bitrate:
117 format_id += '-%s' % bitrate
118 formats.append({
119 'url': uri,
120 'format_id': format_id,
121 'tbr': int_or_none(bitrate),
122 'ext': link.get('FileFormat'),
123 'vcodec': 'none' if kind == 'AudioResource' else None,
124 })
125 subtitles_list = asset.get('SubtitlesList')
126 if isinstance(subtitles_list, list):
127 LANGS = {
128 'Danish': 'da',
129 }
130 for subs in subtitles_list:
131 if not subs.get('Uri'):
132 continue
133 lang = subs.get('Language') or 'da'
134 subtitles.setdefault(LANGS.get(lang, lang), []).append({
135 'url': subs['Uri'],
136 'ext': mimetype2ext(subs.get('MimeType')) or 'vtt'
137 })
138
139 if not formats and restricted_to_denmark:
140 self.raise_geo_restricted(
141 'Unfortunately, DR is not allowed to show this program outside Denmark.',
142 countries=self._GEO_COUNTRIES)
143
144 self._sort_formats(formats)
145
146 return {
147 'id': video_id,
148 'title': title,
149 'description': description,
150 'thumbnail': thumbnail,
151 'timestamp': timestamp,
152 'duration': duration,
153 'formats': formats,
154 'subtitles': subtitles,
155 }
156
157
158 class DRTVLiveIE(InfoExtractor):
159 IE_NAME = 'drtv:live'
160 _VALID_URL = r'https?://(?:www\.)?dr\.dk/(?:tv|TV)/live/(?P<id>[\da-z-]+)'
161 _GEO_COUNTRIES = ['DK']
162 _TEST = {
163 'url': 'https://www.dr.dk/tv/live/dr1',
164 'info_dict': {
165 'id': 'dr1',
166 'ext': 'mp4',
167 'title': 're:^DR1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
168 },
169 'params': {
170 # m3u8 download
171 'skip_download': True,
172 },
173 }
174
175 def _real_extract(self, url):
176 channel_id = self._match_id(url)
177 channel_data = self._download_json(
178 'https://www.dr.dk/mu-online/api/1.0/channel/' + channel_id,
179 channel_id)
180 title = self._live_title(channel_data['Title'])
181
182 formats = []
183 for streaming_server in channel_data.get('StreamingServers', []):
184 server = streaming_server.get('Server')
185 if not server:
186 continue
187 link_type = streaming_server.get('LinkType')
188 for quality in streaming_server.get('Qualities', []):
189 for stream in quality.get('Streams', []):
190 stream_path = stream.get('Stream')
191 if not stream_path:
192 continue
193 stream_url = update_url_query(
194 '%s/%s' % (server, stream_path), {'b': ''})
195 if link_type == 'HLS':
196 formats.extend(self._extract_m3u8_formats(
197 stream_url, channel_id, 'mp4',
198 m3u8_id=link_type, fatal=False, live=True))
199 elif link_type == 'HDS':
200 formats.extend(self._extract_f4m_formats(update_url_query(
201 '%s/%s' % (server, stream_path), {'hdcore': '3.7.0'}),
202 channel_id, f4m_id=link_type, fatal=False))
203 self._sort_formats(formats)
204
205 return {
206 'id': channel_id,
207 'title': title,
208 'thumbnail': channel_data.get('PrimaryImageUri'),
209 'formats': formats,
210 'is_live': True,
211 }