]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/drtv.py
88d096b307cdf6d484ef6b89253f6cdbcb82deb0
[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 )
13
14
15 class DRTVIE(InfoExtractor):
16 _VALID_URL = r'https?://(?:www\.)?dr\.dk/(?:tv/se|nyheder)/(?:[^/]+/)*(?P<id>[\da-z-]+)(?:[/#?]|$)'
17
18 _TESTS = [{
19 'url': 'https://www.dr.dk/tv/se/boern/ultra/klassen-ultra/klassen-darlig-taber-10',
20 'md5': '25e659cccc9a2ed956110a299fdf5983',
21 'info_dict': {
22 'id': 'klassen-darlig-taber-10',
23 'ext': 'mp4',
24 'title': 'Klassen - Dårlig taber (10)',
25 'description': 'md5:815fe1b7fa656ed80580f31e8b3c79aa',
26 'timestamp': 1471991907,
27 'upload_date': '20160823',
28 'duration': 606.84,
29 },
30 'params': {
31 'skip_download': True,
32 },
33 }, {
34 'url': 'https://www.dr.dk/nyheder/indland/live-christianias-rydning-af-pusher-street-er-i-gang',
35 'md5': '2c37175c718155930f939ef59952474a',
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': '- Det er det fedeste, der er sket i 20 år, fortæller christianit til DR Nyheder.',
41 'timestamp': 1472800279,
42 'upload_date': '20160902',
43 'duration': 131.4,
44 },
45 }]
46
47 def _real_extract(self, url):
48 video_id = self._match_id(url)
49
50 webpage = self._download_webpage(url, video_id)
51
52 if '>Programmet er ikke længere tilgængeligt' in webpage:
53 raise ExtractorError(
54 'Video %s is not available' % video_id, expected=True)
55
56 video_id = self._search_regex(
57 (r'data-(?:material-identifier|episode-slug)="([^"]+)"',
58 r'data-resource="[^>"]+mu/programcard/expanded/([^"]+)"'),
59 webpage, 'video id')
60
61 programcard = self._download_json(
62 'http://www.dr.dk/mu/programcard/expanded/%s' % video_id,
63 video_id, 'Downloading video JSON')
64 data = programcard['Data'][0]
65
66 title = remove_end(self._og_search_title(
67 webpage, default=None), ' | TV | DR') or data['Title']
68 description = self._og_search_description(
69 webpage, default=None) or data.get('Description')
70
71 timestamp = parse_iso8601(data.get('CreatedTime'))
72
73 thumbnail = None
74 duration = None
75
76 restricted_to_denmark = False
77
78 formats = []
79 subtitles = {}
80
81 for asset in data['Assets']:
82 if asset.get('Kind') == 'Image':
83 thumbnail = asset.get('Uri')
84 elif asset.get('Kind') == 'VideoResource':
85 duration = float_or_none(asset.get('DurationInMilliseconds'), 1000)
86 restricted_to_denmark = asset.get('RestrictedToDenmark')
87 spoken_subtitles = asset.get('Target') == 'SpokenSubtitles'
88 for link in asset.get('Links', []):
89 uri = link.get('Uri')
90 if not uri:
91 continue
92 target = link.get('Target')
93 format_id = target or ''
94 preference = None
95 if spoken_subtitles:
96 preference = -1
97 format_id += '-spoken-subtitles'
98 if target == 'HDS':
99 formats.extend(self._extract_f4m_formats(
100 uri + '?hdcore=3.3.0&plugin=aasp-3.3.0.99.43',
101 video_id, preference, f4m_id=format_id))
102 elif target == 'HLS':
103 formats.extend(self._extract_m3u8_formats(
104 uri, video_id, 'mp4', entry_protocol='m3u8_native',
105 preference=preference, m3u8_id=format_id))
106 else:
107 bitrate = link.get('Bitrate')
108 if bitrate:
109 format_id += '-%s' % bitrate
110 formats.append({
111 'url': uri,
112 'format_id': format_id,
113 'tbr': int_or_none(bitrate),
114 'ext': link.get('FileFormat'),
115 })
116 subtitles_list = asset.get('SubtitlesList')
117 if isinstance(subtitles_list, list):
118 LANGS = {
119 'Danish': 'da',
120 }
121 for subs in subtitles_list:
122 if not subs.get('Uri'):
123 continue
124 lang = subs.get('Language') or 'da'
125 subtitles.setdefault(LANGS.get(lang, lang), []).append({
126 'url': subs['Uri'],
127 'ext': mimetype2ext(subs.get('MimeType')) or 'vtt'
128 })
129
130 if not formats and restricted_to_denmark:
131 self.raise_geo_restricted(
132 'Unfortunately, DR is not allowed to show this program outside Denmark.',
133 expected=True)
134
135 self._sort_formats(formats)
136
137 return {
138 'id': video_id,
139 'title': title,
140 'description': description,
141 'thumbnail': thumbnail,
142 'timestamp': timestamp,
143 'duration': duration,
144 'formats': formats,
145 'subtitles': subtitles,
146 }