]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/servus.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / servus.py
1 from .common import InfoExtractor
2 from ..utils import (
3 determine_ext,
4 float_or_none,
5 int_or_none,
6 unified_timestamp,
7 urlencode_postdata,
8 url_or_none,
9 )
10
11
12 class ServusIE(InfoExtractor):
13 _VALID_URL = r'''(?x)
14 https?://
15 (?:www\.)?
16 (?:
17 servus\.com/(?:(?:at|de)/p/[^/]+|tv/videos)|
18 (?:servustv|pm-wissen)\.com/videos
19 )
20 /(?P<id>[aA]{2}-\w+|\d+-\d+)
21 '''
22 _TESTS = [{
23 # new URL schema
24 'url': 'https://www.servustv.com/videos/aa-1t6vbu5pw1w12/',
25 'md5': '60474d4c21f3eb148838f215c37f02b9',
26 'info_dict': {
27 'id': 'AA-1T6VBU5PW1W12',
28 'ext': 'mp4',
29 'title': 'Die GrĂ¼nen aus Sicht des Volkes',
30 'alt_title': 'Talk im Hangar-7 Voxpops Gruene',
31 'description': 'md5:1247204d85783afe3682644398ff2ec4',
32 'thumbnail': r're:^https?://.*\.jpg',
33 'duration': 62.442,
34 'timestamp': 1605193976,
35 'upload_date': '20201112',
36 'series': 'Talk im Hangar-7',
37 'season': 'Season 9',
38 'season_number': 9,
39 'episode': 'Episode 31 - September 14',
40 'episode_number': 31,
41 }
42 }, {
43 # old URL schema
44 'url': 'https://www.servus.com/de/p/Die-Gr%C3%BCnen-aus-Sicht-des-Volkes/AA-1T6VBU5PW1W12/',
45 'only_matching': True,
46 }, {
47 'url': 'https://www.servus.com/at/p/Wie-das-Leben-beginnt/1309984137314-381415152/',
48 'only_matching': True,
49 }, {
50 'url': 'https://www.servus.com/tv/videos/aa-1t6vbu5pw1w12/',
51 'only_matching': True,
52 }, {
53 'url': 'https://www.servus.com/tv/videos/1380889096408-1235196658/',
54 'only_matching': True,
55 }, {
56 'url': 'https://www.pm-wissen.com/videos/aa-24mus4g2w2112/',
57 'only_matching': True,
58 }]
59
60 def _real_extract(self, url):
61 video_id = self._match_id(url).upper()
62
63 token = self._download_json(
64 'https://auth.redbullmediahouse.com/token', video_id,
65 'Downloading token', data=urlencode_postdata({
66 'grant_type': 'client_credentials',
67 }), headers={
68 'Authorization': 'Basic SVgtMjJYNEhBNFdEM1cxMTpEdDRVSkFLd2ZOMG5IMjB1NGFBWTBmUFpDNlpoQ1EzNA==',
69 })
70 access_token = token['access_token']
71 token_type = token.get('token_type', 'Bearer')
72
73 video = self._download_json(
74 'https://sparkle-api.liiift.io/api/v1/stv/channels/international/assets/%s' % video_id,
75 video_id, 'Downloading video JSON', headers={
76 'Authorization': '%s %s' % (token_type, access_token),
77 })
78
79 formats = []
80 thumbnail = None
81 for resource in video['resources']:
82 if not isinstance(resource, dict):
83 continue
84 format_url = url_or_none(resource.get('url'))
85 if not format_url:
86 continue
87 extension = resource.get('extension')
88 type_ = resource.get('type')
89 if extension == 'jpg' or type_ == 'reference_keyframe':
90 thumbnail = format_url
91 continue
92 ext = determine_ext(format_url)
93 if type_ == 'dash' or ext == 'mpd':
94 formats.extend(self._extract_mpd_formats(
95 format_url, video_id, mpd_id='dash', fatal=False))
96 elif type_ == 'hls' or ext == 'm3u8':
97 formats.extend(self._extract_m3u8_formats(
98 format_url, video_id, 'mp4', entry_protocol='m3u8_native',
99 m3u8_id='hls', fatal=False))
100 elif extension == 'mp4' or ext == 'mp4':
101 formats.append({
102 'url': format_url,
103 'format_id': type_,
104 'width': int_or_none(resource.get('width')),
105 'height': int_or_none(resource.get('height')),
106 })
107
108 attrs = {}
109 for attribute in video['attributes']:
110 if not isinstance(attribute, dict):
111 continue
112 key = attribute.get('fieldKey')
113 value = attribute.get('fieldValue')
114 if not key or not value:
115 continue
116 attrs[key] = value
117
118 title = attrs.get('title_stv') or video_id
119 alt_title = attrs.get('title')
120 description = attrs.get('long_description') or attrs.get('short_description')
121 series = attrs.get('label')
122 season = attrs.get('season')
123 episode = attrs.get('chapter')
124 duration = float_or_none(attrs.get('duration'), scale=1000)
125 season_number = int_or_none(self._search_regex(
126 r'Season (\d+)', season or '', 'season number', default=None))
127 episode_number = int_or_none(self._search_regex(
128 r'Episode (\d+)', episode or '', 'episode number', default=None))
129
130 return {
131 'id': video_id,
132 'title': title,
133 'alt_title': alt_title,
134 'description': description,
135 'thumbnail': thumbnail,
136 'duration': duration,
137 'timestamp': unified_timestamp(video.get('lastPublished')),
138 'series': series,
139 'season': season,
140 'season_number': season_number,
141 'episode': episode,
142 'episode_number': episode_number,
143 'formats': formats,
144 }