Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/magentamusik.py (Python)

Code before:
from .common import InfoExtractor


class MagentaMusikIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?magentamusik\.de/(?P<id>.*)'

    _TESTS = [{
        'url': 'https://www.magentamusik.de/marty-friedman-woa-2023-9208205928595409235',
        'md5': 'd82dd4748f55fc91957094546aaf8584',
        'info_dict': {
            'id': '9208205928595409235',
            'ext': 'mp4',
            'title': 'Marty Friedman: W:O:A 2023',
            'duration': 2760
        }
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)
        video_id = self._html_search_regex(r'"assetId":"[^\d]+([0-9]+)"', webpage, 'video_id')

        json = self._download_json("https://wcps.t-online.de/cvss/magentamusic/vodplayer/v3/player/58935/%s/Main%%20Movie" % video_id, video_id)
        xml_url = json['content']['feature']['representations'][0]['contentPackages'][0]['media']['href']
        metadata = json['content']['feature'].get('metadata')
        title = None
        if metadata:
            title = metadata.get('title')
            duration = metadata.get('runtimeInSeconds')
        xml = self._download_xml(xml_url, video_id)
        final_url = xml[0][0][0].attrib['src']

        formats = self._extract_m3u8_formats(
            final_url, video_id, ext='mp4')

        return {
            'id': video_id,
            'title': title,
            'formats': formats,
            'duration': duration
        }


Review comment (refactor):
Could potentially be made more robust with something like this:
```suggestion
        player_config = self._search_json(r'data-js-element="o-video-player__config">\s*', webpage, 'player config', display_id)
        if 'assetId' not in player_config:
            raise ExtractorError('No assetId found in player config')
        asset_details = self._download_json(
            f'https://wcps.t-online.de/cvss/magentamusic/vodclient/v2/assetdetails/58938/{player_config["assetId"]}',
            display_id, note='Downloading asset details')
        video_id = traverse_obj(asset_details, ('content', 'partnerInformation', ..., 'reference'), get_all=False)
```
`asset_details` also contains some additional metadata.
Curious to hear if others think this would be worth it. Maybe the simple regex approach could be kept as a fallback.

Code after:
from .common import InfoExtractor
from ..utils import ExtractorError, int_or_none, join_nonempty, url_or_none
from ..utils.traversal import traverse_obj


class MagentaMusikIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?magentamusik\.de/(?P<id>[^/?#]+)'

    _TESTS = [{
        'url': 'https://www.magentamusik.de/marty-friedman-woa-2023-9208205928595409235',
        'md5': 'd82dd4748f55fc91957094546aaf8584',
        'info_dict': {
            'id': '9208205928595409235',
            'display_id': 'marty-friedman-woa-2023-9208205928595409235',
            'ext': 'mp4',
            'title': 'Marty Friedman: W:O:A 2023',
            'alt_title': 'Konzert vom: 05.08.2023 13:00',
            'duration': 2760,
            'categories': ['Musikkonzert'],
            'release_year': 2023,
            'location': 'Deutschland',
        }
    }]

    def _real_extract(self, url):
        display_id = self._match_id(url)
        webpage = self._download_webpage(url, display_id)
        player_config = self._search_json(
            r'data-js-element="o-video-player__config">', webpage, 'player config', display_id, fatal=False)
        if not player_config:
            raise ExtractorError('No video found', expected=True)

        asset_id = player_config['assetId']
        asset_details = self._download_json(
            f'https://wcps.t-online.de/cvss/magentamusic/vodclient/v2/assetdetails/58938/{asset_id}',
            display_id, note='Downloading asset details')

        video_id = traverse_obj(
            asset_details, ('content', 'partnerInformation', ..., 'reference', {str}), get_all=False)
        if not video_id:
            raise ExtractorError('Unable to extract video id')

        vod_data = self._download_json(
            f'https://wcps.t-online.de/cvss/magentamusic/vodclient/v2/player/58935/{video_id}/Main%20Movie', video_id)
        smil_url = traverse_obj(
            vod_data, ('content', 'feature', 'representations', ...,