[ie/dplay] Fix extractors (#10471)
[yt-dlp3.git] / yt_dlp / extractor / sverigesradio.py
blob944ce18c46eab3b89ce59cd229c755f95bf1bb50
1 from .common import InfoExtractor
2 from ..utils import (
3 determine_ext,
4 extract_attributes,
5 get_element_by_id,
6 get_element_html_by_class,
7 int_or_none,
8 str_or_none,
9 traverse_obj,
10 url_or_none,
14 class SverigesRadioBaseIE(InfoExtractor):
15 _BASE_URL = 'https://sverigesradio.se/sida/playerajax/'
16 _QUALITIES = ['low', 'medium', 'high']
17 _EXT_TO_CODEC_MAP = {
18 'mp3': 'mp3',
19 'm4a': 'aac',
21 _CODING_FORMAT_TO_ABR_MAP = {
22 5: 128,
23 11: 192,
24 12: 32,
25 13: 96,
28 def _real_extract(self, url):
29 audio_id, display_id = self._match_valid_url(url).group('id', 'slug')
30 if not audio_id:
31 webpage = self._download_webpage(url, display_id)
32 audio_id = (
33 traverse_obj(
34 get_element_html_by_class('audio-button', webpage),
35 ({extract_attributes}, ('data-audio-id', 'data-publication-id')), get_all=False)
36 or self._parse_json(get_element_by_id('gtm-metadata', webpage), display_id)['pageId'])
38 query = {
39 'id': audio_id,
40 'type': self._AUDIO_TYPE,
43 item = self._download_json(
44 self._BASE_URL + 'audiometadata', audio_id,
45 'Downloading audio JSON metadata', query=query)['items'][0]
47 query['format'] = 'iis'
48 urls = []
49 formats = []
50 for quality in self._QUALITIES:
51 query['quality'] = quality
52 audio_url_data = self._download_json(
53 self._BASE_URL + 'getaudiourl', audio_id,
54 f'Downloading {quality} format JSON metadata',
55 fatal=False, query=query) or {}
56 audio_url = audio_url_data.get('audioUrl')
57 if not audio_url or audio_url in urls:
58 continue
59 urls.append(audio_url)
60 ext = determine_ext(audio_url)
61 coding_format = audio_url_data.get('codingFormat')
62 abr = int_or_none(self._search_regex(
63 r'_a(\d+)\.m4a', audio_url, 'audio bitrate',
64 default=None)) or self._CODING_FORMAT_TO_ABR_MAP.get(coding_format)
65 formats.append({
66 'abr': abr,
67 'acodec': self._EXT_TO_CODEC_MAP.get(ext),
68 'ext': ext,
69 'format_id': str_or_none(coding_format),
70 'vcodec': 'none',
71 'url': audio_url,
74 return {
75 'id': audio_id,
76 'formats': formats,
77 **traverse_obj(item, {
78 'title': 'subtitle',
79 'series': 'title',
80 'duration': ('duration', {int_or_none}),
81 'thumbnail': ('displayimageurl', {url_or_none}),
82 'description': 'description',
83 }),
87 class SverigesRadioPublicationIE(SverigesRadioBaseIE):
88 IE_NAME = 'sverigesradio:publication'
89 _VALID_URL = r'https?://(?:www\.)?sverigesradio\.se/(?:sida/)?(?:artikel|gruppsida)(?:\.aspx\?.*?\bartikel=(?P<id>[0-9]+)|/(?P<slug>[\w-]+))'
90 _TESTS = [{
91 'url': 'https://sverigesradio.se/sida/artikel.aspx?programid=83&artikel=7038546',
92 'md5': '6a4917e1923fccb080e5a206a5afa542',
93 'info_dict': {
94 'id': '7038546',
95 'ext': 'm4a',
96 'duration': 132,
97 'series': 'Nyheter (Ekot)',
98 'title': 'Esa Teittinen: Sanningen har inte kommit fram',
99 'description': 'md5:daf7ce66a8f0a53d5465a5984d3839df',
100 'thumbnail': r're:^https?://.*\.jpg',
102 }, {
103 'url': 'https://sverigesradio.se/artikel/tysk-fotbollsfeber-bayern-munchens-10-ariga-segersvit-kan-brytas',
104 'md5': 'f8a914ad50f491bb74eed403ab4bfef6',
105 'info_dict': {
106 'id': '8360345',
107 'ext': 'm4a',
108 'title': 'Tysk fotbollsfeber när Bayern Münchens 10-åriga segersvit kan brytas',
109 'series': 'Radiosporten',
110 'description': 'md5:5254610e20ce527ecb3a6102a06dcc5f',
111 'duration': 72,
112 'thumbnail': r're:^https?://.*\.jpg',
114 }, {
115 'url': 'https://sverigesradio.se/sida/gruppsida.aspx?programid=3304&grupp=6247&artikel=7146887',
116 'only_matching': True,
118 _AUDIO_TYPE = 'publication'
121 class SverigesRadioEpisodeIE(SverigesRadioBaseIE):
122 IE_NAME = 'sverigesradio:episode'
123 _VALID_URL = r'https?://(?:www\.)?sverigesradio\.se/(?:sida/)?avsnitt/(?:(?P<id>\d+)|(?P<slug>[\w-]+))(?:$|[#?])'
124 _TESTS = [{
125 'url': 'https://sverigesradio.se/avsnitt/1140922?programid=1300',
126 'md5': '20dc4d8db24228f846be390b0c59a07c',
127 'info_dict': {
128 'id': '1140922',
129 'ext': 'mp3',
130 'duration': 3307,
131 'series': 'Konflikt',
132 'title': 'Metoo och valen',
133 'description': 'md5:fcb5c1f667f00badcc702b196f10a27e',
134 'thumbnail': r're:^https?://.*\.jpg',
136 }, {
137 'url': 'https://sverigesradio.se/avsnitt/p4-live-med-first-aid-kit-scandinavium-mars-2023',
138 'md5': 'ce17fb82520a8033dbb846993d5589fe',
139 'info_dict': {
140 'id': '2160416',
141 'ext': 'm4a',
142 'title': 'P4 Live med First Aid Kit',
143 'description': 'md5:6d5b78eed3d2b65f6de04daa45e9285d',
144 'thumbnail': r're:^https?://.*\.jpg',
145 'series': 'P4 Live',
146 'duration': 5640,
149 _AUDIO_TYPE = 'episode'