2 from __future__
import unicode_literals
6 from .common
import InfoExtractor
7 from ..compat
import compat_str
23 class ZDFBaseIE(InfoExtractor
):
24 def _call_api(self
, url
, player
, referrer
, video_id
, item
):
25 return self
._download
_json
(
26 url
, video_id
, 'Downloading JSON %s' % item
,
29 'Api-Auth': 'Bearer %s' % player
['apiToken'],
32 def _extract_player(self
, webpage
, video_id
, fatal
=True):
33 return self
._parse
_json
(
35 r
'(?s)data-zdfplayer-jsb=(["\'])(?P
<json
>{.+?}
)\
1', webpage,
36 'player JSON
', default='{}' if not fatal else NO_DEFAULT,
41 class ZDFIE(ZDFBaseIE):
43 _VALID_URL = r'https?
://www\
.(zdf|
3sat
)\
.de
/(?
:[^
/]+/)*(?P
<id>[^
/?
]+)\
.html
'
44 _QUALITIES = ('auto
', 'low
', 'med
', 'high
', 'veryhigh
')
45 _GEO_COUNTRIES = ['DE
']
48 'url
': 'https
://www
.3sat
.de
/wissen
/wissenschaftsdoku
/luxusgut
-lebensraum
-100.html
',
50 'id': 'luxusgut
-lebensraum
-100',
52 'title
': 'Luxusgut Lebensraum
',
53 'description
': 'md5
:5c09b2f45ac3bc5233d1b50fc543d061
',
55 'timestamp
': 1566497700,
56 'upload_date
': '20190822',
59 'url
': 'https
://www
.zdf
.de
/dokumentation
/terra
-x
/die
-magie
-der
-farben
-von
-koenigspurpur
-und
-jeansblau
-100.html
',
61 'id': 'die
-magie
-der
-farben
-von
-koenigspurpur
-und
-jeansblau
-100',
63 'title
': 'Die Magie der
Farben (2/2)',
64 'description
': 'md5
:a89da10c928c6235401066b60a6d5c1a
',
66 'timestamp
': 1465021200,
67 'upload_date
': '20160604',
70 'url
': 'https
://www
.zdf
.de
/service
-und
-hilfe
/die
-neue
-zdf
-mediathek
/zdfmediathek
-trailer
-100.html
',
71 'only_matching
': True,
73 'url
': 'https
://www
.zdf
.de
/filme
/taunuskrimi
/die
-lebenden
-und
-die
-toten
-1---ein
-taunuskrimi
-100.html
',
74 'only_matching
': True,
76 'url
': 'https
://www
.zdf
.de
/dokumentation
/planet
-e
/planet
-e
-uebersichtsseite
-weitere
-dokumentationen
-von
-planet
-e
-100.html
',
77 'only_matching
': True,
81 def _extract_subtitles(src):
83 for caption in try_get(src, lambda x: x['captions
'], list) or []:
84 subtitle_url = url_or_none(caption.get('uri
'))
86 lang = caption.get('language
', 'deu
')
87 subtitles.setdefault(lang, []).append({
92 def _extract_format(self, video_id, formats, format_urls, meta):
93 format_url = url_or_none(meta.get('url
'))
96 if format_url in format_urls:
98 format_urls.add(format_url)
99 mime_type = meta.get('mimeType
')
100 ext = determine_ext(format_url)
101 if mime_type == 'application
/x
-mpegURL
' or ext == 'm3u8
':
102 formats.extend(self._extract_m3u8_formats(
103 format_url, video_id, 'mp4
', m3u8_id='hls
',
104 entry_protocol='m3u8_native
', fatal=False))
105 elif mime_type == 'application
/f4m
+xml
' or ext == 'f4m
':
106 formats.extend(self._extract_f4m_formats(
107 update_url_query(format_url, {'hdcore': '3.7.0'}), video_id, f4m_id='hds
', fatal=False))
109 f = parse_codecs(meta.get('mimeCodec
'))
111 for p in (meta.get('type'), meta.get('quality
')):
112 if p and isinstance(p, compat_str):
116 'format_id
': '-'.join(format_id),
117 'format_note
': meta.get('quality
'),
118 'language
': meta.get('language
'),
119 'quality
': qualities(self._QUALITIES)(meta.get('quality
')),
124 def _extract_entry(self, url, player, content, video_id):
125 title = content.get('title
') or content['teaserHeadline
']
127 t = content['mainVideoContent
']['http
://zdf
.de
/rels
/target
']
129 ptmd_path = t.get('http
://zdf
.de
/rels
/streams
/ptmd
')
133 'http
://zdf
.de
/rels
/streams
/ptmd
-template
'].replace(
134 '{playerId}
', 'portal
')
136 ptmd = self._call_api(
137 urljoin(url, ptmd_path), player, url, video_id, 'metadata
')
141 for p in ptmd['priorityList
']:
142 formitaeten = p.get('formitaeten
')
143 if not isinstance(formitaeten, list):
145 for f in formitaeten:
146 f_qualities = f.get('qualities
')
147 if not isinstance(f_qualities, list):
149 for quality in f_qualities:
150 tracks = try_get(quality, lambda x: x['audio
']['tracks
'], list)
154 self._extract_format(
155 video_id, formats, track_uris, {
156 'url
': track.get('uri
'),
157 'type': f.get('type'),
158 'mimeType
': f.get('mimeType
'),
159 'quality
': quality.get('quality
'),
160 'language
': track.get('language
'),
162 self._sort_formats(formats)
166 content, lambda x: x['teaserImageRef
']['layouts
'], dict)
168 for layout_key, layout_url in layouts.items():
169 layout_url = url_or_none(layout_url)
174 'format_id
': layout_key,
176 mobj = re.search(r'(?P
<width
>\d
+)x(?P
<height
>\d
+)', layout_key)
179 'width
': int(mobj.group('width
')),
180 'height
': int(mobj.group('height
')),
182 thumbnails.append(thumbnail)
187 'description
': content.get('leadParagraph
') or content.get('teasertext
'),
188 'duration
': int_or_none(t.get('duration
')),
189 'timestamp
': unified_timestamp(content.get('editorialDate
')),
190 'thumbnails
': thumbnails,
191 'subtitles
': self._extract_subtitles(ptmd),
195 def _extract_regular(self, url, player, video_id):
196 content = self._call_api(
197 player['content
'], player, url, video_id, 'content
')
198 return self._extract_entry(player['content
'], player, content, video_id)
200 def _extract_mobile(self, video_id):
201 document = self._download_json(
202 'https
://zdf
-cdn
.live
.cellular
.de
/mediathekV2
/document
/%s' % video_id,
203 video_id)['document
']
205 title = document['titel
']
209 for f in document['formitaeten
']:
210 self._extract_format(video_id, formats, format_urls, f)
211 self._sort_formats(formats)
214 teaser_bild = document.get('teaserBild
')
215 if isinstance(teaser_bild, dict):
216 for thumbnail_key, thumbnail in teaser_bild.items():
217 thumbnail_url = try_get(
218 thumbnail, lambda x: x['url
'], compat_str)
221 'url
': thumbnail_url,
223 'width
': int_or_none(thumbnail.get('width
')),
224 'height
': int_or_none(thumbnail.get('height
')),
230 'description
': document.get('beschreibung
'),
231 'duration
': int_or_none(document.get('length
')),
232 'timestamp
': unified_timestamp(try_get(
233 document, lambda x: x['meta
']['editorialDate
'], compat_str)),
234 'thumbnails
': thumbnails,
235 'subtitles
': self._extract_subtitles(document),
239 def _real_extract(self, url):
240 video_id = self._match_id(url)
242 webpage = self._download_webpage(url, video_id, fatal=False)
244 player = self._extract_player(webpage, url, fatal=False)
246 return self._extract_regular(url, player, video_id)
248 return self._extract_mobile(video_id)
251 class ZDFChannelIE(ZDFBaseIE):
252 _VALID_URL = r'https?
://www\
.zdf\
.de
/(?
:[^
/]+/)*(?P
<id>[^
/?
#&]+)'
254 'url': 'https://www.zdf.de/sport/das-aktuelle-sportstudio',
256 'id': 'das-aktuelle-sportstudio',
257 'title': 'das aktuelle sportstudio | ZDF',
259 'playlist_mincount': 23,
261 'url': 'https://www.zdf.de/dokumentation/planet-e',
264 'title': 'planet e.',
266 'playlist_mincount': 50,
268 'url': 'https://www.zdf.de/filme/taunuskrimi/',
269 'only_matching': True,
273 def suitable(cls
, url
):
274 return False if ZDFIE
.suitable(url
) else super(ZDFChannelIE
, cls
).suitable(url
)
276 def _real_extract(self
, url
):
277 channel_id
= self
._match
_id
(url
)
279 webpage
= self
._download
_webpage
(url
, channel_id
)
282 self
.url_result(item_url
, ie
=ZDFIE
.ie_key())
283 for item_url
in orderedSet(re
.findall(
284 r
'data-plusbar-url=["\'](http
.+?\
.html
)', webpage))]
286 return self.playlist_result(
287 entries, channel_id, self._og_search_title(webpage, fatal=False))
290 player = self._extract_player(webpage, channel_id)
292 channel_id = self._search_regex(
293 r'docId\s
*:\s
*(["\'])(?P<id>(?!\1).+?)\1', webpage,
294 'channel id', group='id')
296 channel = self._call_api(
297 'https://api.zdf.de/content/documents/%s.json' % channel_id,
298 player, url, channel_id)
301 for module in channel['module']:
302 for teaser in try_get(module, lambda x: x['teaser'], list) or []:
304 teaser, lambda x: x['http://zdf.de/rels/target'], dict)
307 items.extend(try_get(
309 lambda x: x['resultsWithVideo']['http://zdf.de/rels/search/results'],
311 items.extend(try_get(
313 lambda x: x['filterRef']['resultsWithVideo']['http://zdf.de/rels/search/results'],
319 t = try_get(item, lambda x: x['http://zdf.de/rels/target'], dict)
322 sharing_url = t.get('http://zdf.de/rels/sharing-url')
323 if not sharing_url or not isinstance(sharing_url, compat_str):
325 if sharing_url in entry_urls:
327 entry_urls.add(sharing_url)
328 entries.append(self.url_result(
329 sharing_url, ie=ZDFIE.ie_key(), video_id=t.get('id')))
331 return self.playlist_result(entries, channel_id, channel.get('title'))