Repository: yt-dlp/yt-dlp

Issue:
[sverigesradio] URLs doesn't seem to work
### DO NOT REMOVE OR SKIP THE ISSUE TEMPLATE

- [X] I understand that I will be **blocked** if I *intentionally* remove or skip any mandatory\* field

### Checklist

- [X] I'm reporting that yt-dlp is broken on a **supported** site
- [X] I've verified that I'm running yt-dlp version **2023.03.04** ([update instructions](https://github.com/yt-dlp/yt-dlp#update)) or later (specify commit)
- [X] I've checked that all provided URLs are playable in a browser with the same IP and same login details
- [X] I've checked that all URLs and arguments with special characters are [properly quoted or escaped](https://github.com/yt-dlp/yt-dlp/wiki/FAQ#video-url-contains-an-ampersand--and-im-getting-some-strange-output-1-2839-or-v-is-not-recognized-as-an-internal-or-external-command)
- [X] I've searched [known issues](https://github.com/yt-dlp/yt-dlp/issues/3766) and the [bugtracker](https://github.com/yt-dlp/yt-dlp/issues?q=) for similar issues **including closed ones**. DO NOT post duplicates
- [X] I've read the [guidelines for opening an issue](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#opening-an-issue)
- [X] I've read about [sharing account credentials](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#are-you-willing-to-share-account-details-if-needed) and I'm willing to share it if required

### Region

Sweden

### Provide a description that is worded well enough to be understood

There are two extractors, sverigesradio:episode ande sverigesradio:publication, that, to my understanding, should work with URLs from sverigesradio.se. However, no URLs I throw at yt-dlp for that site works, it falls back to the generic extractor. Here are two examples (complete output provided for both, no account required):
https://sverigesradio.se/artikel/tysk-fotbollsfeber-bayern-munchens-10-ariga-segersvit-kan-brytas
https://sverigesradio.se/avsnitt/p4-live-med-first-aid-kit-scandinavium-mars-2023

### Provide verbose output that clearly demonstrates the problem

- [X] Run **your** yt-dlp command with **-vU** flag added (`yt-dlp -vU <your command line>`)
- [ ] If using API, add `'verbose': True` to `YoutubeDL` params instead
- [X] Copy the WHOLE output (starting with `[debug] Command-line config`) and insert it below

### Complete Verbose Output

```shell
[debug] Command-line config: ['--no-config', '-vU', 'https://sverigesradio.se/artikel/tysk-fotbollsfeber-bayern-munchens-10-ariga-segersvit-kan-brytas']
[debug] Encodings: locale cp1252, fs utf-8, pref cp1252, out utf-8, error utf-8, screen utf-8
[debug] yt-dlp version nightly@2023.05.27.104018 [daafbf49b] (win_exe)
[debug] Python 3.8.10 (CPython AMD64 64bit) - Windows-10-10.0.22621-SP0 (OpenSSL 1.1.1k  25 Mar 2021)
[debug] exe versions: ffmpeg N-110654-g63767b79a5-20230518 (setts), ffprobe N-110654-g63767b79a5-20230518
[debug] Optional libraries: Cryptodome-3.18.0, brotli-1.0.9, certifi-2023.05.07, mutagen-1.46.0, sqlite3-2.6.0, websockets-11.0.3
[debug] Proxy map: {}
[debug] Loaded 1822 extractors
[debug] Fetching release info: https://api.github.com/repos/yt-dlp/yt-dlp-nightly-builds/releases/latest
Available version: nightly@2023.05.27.104018, Current version: nightly@2023.05.27.104018
Current Build Hash: 1f01e550a8bb814b3a92039f5749d583ea9c084ad4bf9cafd933e52a0c9e2813
yt-dlp is up to date (nightly@2023.05.27.104018)
[generic] Extracting URL: https://sverigesradio.se/artikel/tysk-fotbollsfeber-bayern-munchens-10-ariga-segersvit-kan-brytas
[generic] tysk-fotbollsfeber-bayern-munchens-10-ariga-segersvit-kan-brytas: Downloading webpage
WARNING: [generic] Falling back on generic information extractor
[generic] tysk-fotbollsfeber-bayern-munchens-10-ariga-segersvit-kan-brytas: Extracting information
[debug] Looking for embeds
ERROR: Unsupported URL: https://sverigesradio.se/artikel/tysk-fotbollsfeber-bayern-munchens-10-ariga-segersvit-kan-brytas
Traceback (most recent call last):
  File "yt_dlp\YoutubeDL.py", line 1552, in wrapper
  File "yt_dlp\YoutubeDL.py", line 1628, in __extract_info
  File "yt_dlp\extractor\common.py", line 698, in extract
  File "yt_dlp\extractor\generic.py", line 2568, in _real_extract
yt_dlp.utils.UnsupportedError: Unsupported URL: https://sverigesradio.se/artikel/tysk-fotbollsfeber-bayern-munchens-10-ariga-segersvit-kan-brytas

[debug] Command-line config: ['--no-config', '-vU', 'https://sverigesradio.se/avsnitt/p4-live-med-first-aid-kit-scandinavium-mars-2023']
[debug] Encodings: locale cp1252, fs utf-8, pref cp1252, out utf-8, error utf-8, screen utf-8
[debug] yt-dlp version nightly@2023.05.27.104018 [daafbf49b] (win_exe)
[debug] Python 3.8.10 (CPython AMD64 64bit) - Windows-10-10.0.22621-SP0 (OpenSSL 1.1.1k  25 Mar 2021)
[debug] exe versions: ffmpeg N-110654-g63767b79a5-20230518 (setts), ffprobe N-110654-g63767b79a5-20230518
[debug] Optional libraries: Cryptodome-3.18.0, brotli-1.0.9, certifi-2023.05.07, mutagen-1.46.0, sqlite3-2.6.0, websockets-11.0.3
[debug] Proxy map: {}
[debug] Loaded 1822 extractors
[debug] Fetching release info: https://api.github.com/repos/yt-dlp/yt-dlp-nightly-builds/releases/latest
Available version: nightly@2023.05.27.104018, Current version: nightly@2023.05.27.104018
Current Build Hash: 1f01e550a8bb814b3a92039f5749d583ea9c084ad4bf9cafd933e52a0c9e2813
yt-dlp is up to date (nightly@2023.05.27.104018)
[generic] Extracting URL: https://sverigesradio.se/avsnitt/p4-live-med-first-aid-kit-scandinavium-mars-2023
[generic] p4-live-med-first-aid-kit-scandinavium-mars-2023: Downloading webpage
WARNING: [generic] Falling back on generic information extractor
[generic] p4-live-med-first-aid-kit-scandinavium-mars-2023: Extracting information
[debug] Looking for embeds
ERROR: Unsupported URL: https://sverigesradio.se/avsnitt/p4-live-med-first-aid-kit-scandinavium-mars-2023
Traceback (most recent call last):
  File "yt_dlp\YoutubeDL.py", line 1552, in wrapper
  File "yt_dlp\YoutubeDL.py", line 1628, in __extract_info
  File "yt_dlp\extractor\common.py", line 698, in extract
  File "yt_dlp\extractor\generic.py", line 2568, in _real_extract
yt_dlp.utils.UnsupportedError: Unsupported URL: https://sverigesradio.se/avsnitt/p4-live-med-first-aid-kit-scandinavium-mars-2023
```



Discussion:

Resolving patch:
diff --git a/yt_dlp/extractor/sverigesradio.py b/yt_dlp/extractor/sverigesradio.py
--- a/yt_dlp/extractor/sverigesradio.py
+++ b/yt_dlp/extractor/sverigesradio.py
@@ -1,8 +1,13 @@
 from .common import InfoExtractor
 from ..utils import (
     determine_ext,
+    extract_attributes,
+    get_element_by_id,
+    get_element_html_by_class,
     int_or_none,
     str_or_none,
+    traverse_obj,
+    url_or_none,
 )
 
 
@@ -21,7 +26,15 @@ class SverigesRadioBaseIE(InfoExtractor):
     }
 
     def _real_extract(self, url):
-        audio_id = self._match_id(url)
+        audio_id, display_id = self._match_valid_url(url).group('id', 'slug')
+        if not audio_id:
+            webpage = self._download_webpage(url, display_id)
+            audio_id = (
+                traverse_obj(
+                    get_element_html_by_class('audio-button', webpage),
+                    ({extract_attributes}, ('data-audio-id', 'data-publication-id')), get_all=False)
+                or self._parse_json(get_element_by_id('gtm-metadata', webpage), display_id)['pageId'])
+
         query = {
             'id': audio_id,
             'type': self._AUDIO_TYPE,
@@ -30,7 +43,6 @@ def _real_extract(self, url):
         item = self._download_json(
             self._BASE_URL + 'audiometadata', audio_id,
             'Downloading audio JSON metadata', query=query)['items'][0]
-        title = item['subtitle']
 
         query['format'] = 'iis'
         urls = []
@@ -61,18 +73,20 @@ def _real_extract(self, url):
 
         return {
             'id': audio_id,
-            'title': title,
             'formats': formats,
-            'series': item.get('title'),
-            'duration': int_or_none(item.get('duration')),
-            'thumbnail': item.get('displayimageurl'),
-            'description': item.get('description'),
+            **traverse_obj(item, {
+                'title': 'subtitle',
+                'series': 'title',
+                'duration': ('duration', {int_or_none}),
+                'thumbnail': ('displayimageurl', {url_or_none}),
+                'description': 'description',
+            }),
         }
 
 
 class SverigesRadioPublicationIE(SverigesRadioBaseIE):
     IE_NAME = 'sverigesradio:publication'
-    _VALID_URL = r'https?://(?:www\.)?sverigesradio\.se/sida/(?:artikel|gruppsida)\.aspx\?.*?\bartikel=(?P<id>[0-9]+)'
+    _VALID_URL = r'https?://(?:www\.)?sverigesradio\.se/(?:sida/)?(?:artikel|gruppsida)(?:\.aspx\?.*?\bartikel=(?P<id>[0-9]+)|/(?P<slug>[\w-]+))'
     _TESTS = [{
         'url': 'https://sverigesradio.se/sida/artikel.aspx?programid=83&artikel=7038546',
         'md5': '6a4917e1923fccb080e5a206a5afa542',
@@ -85,6 +99,18 @@ class SverigesRadioPublicationIE(SverigesRadioBaseIE):
             'description': 'md5:daf7ce66a8f0a53d5465a5984d3839df',
             'thumbnail': r're:^https?://.*\.jpg',
         },
+    }, {
+        'url': 'https://sverigesradio.se/artikel/tysk-fotbollsfeber-bayern-munchens-10-ariga-segersvit-kan-brytas',
+        'md5': 'f8a914ad50f491bb74eed403ab4bfef6',
+        'info_dict': {
+            'id': '8360345',
+            'ext': 'm4a',
+            'title': 'Tysk fotbollsfeber när Bayern Münchens 10-åriga segersvit kan brytas',
+            'series': 'Radiosporten',
+            'description': 'md5:5254610e20ce527ecb3a6102a06dcc5f',
+            'duration': 72,
+            'thumbnail': r're:^https?://.*\.jpg',
+        },
     }, {
         'url': 'https://sverigesradio.se/sida/gruppsida.aspx?programid=3304&grupp=6247&artikel=7146887',
         'only_matching': True,
@@ -94,8 +120,8 @@ class SverigesRadioPublicationIE(SverigesRadioBaseIE):
 
 class SverigesRadioEpisodeIE(SverigesRadioBaseIE):
     IE_NAME = 'sverigesradio:episode'
-    _VALID_URL = r'https?://(?:www\.)?sverigesradio\.se/(?:sida/)?avsnitt/(?P<id>[0-9]+)'
-    _TEST = {
+    _VALID_URL = r'https?://(?:www\.)?sverigesradio\.se/(?:sida/)?avsnitt/(?:(?P<id>\d+)|(?P<slug>[\w-]+))(?:$|[#?])'
+    _TESTS = [{
         'url': 'https://sverigesradio.se/avsnitt/1140922?programid=1300',
         'md5': '20dc4d8db24228f846be390b0c59a07c',
         'info_dict': {
@@ -106,6 +132,18 @@ class SverigesRadioEpisodeIE(SverigesRadioBaseIE):
             'title': 'Metoo och valen',
             'description': 'md5:fcb5c1f667f00badcc702b196f10a27e',
             'thumbnail': r're:^https?://.*\.jpg',
-        }
-    }
+        },
+    }, {
+        'url': 'https://sverigesradio.se/avsnitt/p4-live-med-first-aid-kit-scandinavium-mars-2023',
+        'md5': 'ce17fb82520a8033dbb846993d5589fe',
+        'info_dict': {
+            'id': '2160416',
+            'ext': 'm4a',
+            'title': 'P4 Live med First Aid Kit',
+            'description': 'md5:6d5b78eed3d2b65f6de04daa45e9285d',
+            'thumbnail': r're:^https?://.*\.jpg',
+            'series': 'P4 Live',
+            'duration': 5640,
+        },
+    }]
     _AUDIO_TYPE = 'episode'


Test patch: