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

Code before:
        'url': 'https://www.permanto.fi/web/alfatv/player/vod?assetId=165743121',
        'only_matching': True
    }, {
        'url': 'https://suite.icareus.com/fi/web/westend-indians/player/vod?assetId=47567389',
        'only_matching': True
    }, {
        'url': 'https://videos.minifiddlers.org/web/international-minifiddlers/player/vod?assetId=1982759',
        'only_matching': True
    }]
    _API2_PATH = '/icareus-suite-api-portlet/publishing'

    def _real_extract(self, url):
        maybe_id = self._match_id(url)
        page = self._download_webpage(url, maybe_id)
        video_id = self._search_regex(
            r"_icareus\['itemId'\]='(\d+)'", page, "video_id")
        api_base = self._search_regex(
            r'var publishingServiceURL = "(http.*?)";', page, "api_base")
        organization_id = self._search_regex(
            r"_icareus\['organizationId'\]='(\d+)'", page, "organization_id")
        token = self._search_regex(
            r"_icareus\['token'\]='([a-f0-9]+)'", page, "token")

        token2 = self._search_regex(
            r'''data\s*:\s*{action:"getAsset".*?token:'([a-f0-9]+)'}''', page,
            "token2", default=None, fatal=False)
        metajson = get_element_by_attribute('type', 'application/ld+json', page)

        metad = None
        if metajson:
            # The description can contain newlines, HTML tags, quote chars etc.
            # so we'll extract it manually
            mo = re.match(
                r'(.*",)\s*"description": "(.*?)",(\s*"thumbnailUrl":.*)',
                metajson, flags=re.DOTALL)
            if mo:
                desc_text = mo.group(2)
                metajson = mo.group(1) + mo.group(3)
                metad = self._parse_json(metajson, video_id, fatal=False)
            else:
                self.report_warning("Could not fix metadata JSON", video_id)

        livestream_title = get_element_by_class(
            'unpublished-info-item future-event-title', page)

        duration = None
        thumbnail = None
        if metad:
            title = metad.get('name')
            description = desc_text
            timestamp = unified_timestamp(metad.get('uploadDate'))

Review comment (suggestion):
* avoid use of `.*?`/`.+?`. You can use `[^"]+` instead
* Use `\s*` instead of space
* Use double quotes

Code after:
        'only_matching': True
    }, {
        'url': 'https://hyvinvointitv.fi/fi/web/hyvinvointitv/player/vod?assetId=89149730',
        'only_matching': True
    }, {
        'url': 'https://inez.fi/fi/web/inez-media/player/vod?assetId=71328822',
        'only_matching': True
    }, {
        'url': 'https://www.permanto.fi/fi/web/alfatv/player/vod?assetId=135497515',
        'only_matching': True
    }, {
        'url': 'https://videos.minifiddlers.org/web/international-minifiddlers/player/vod?assetId=1982759',
        'only_matching': True
    }]

    def _real_extract(self, url):
        base_url, temp_id = self._match_valid_url(url).groups()
        webpage = self._download_webpage(url, temp_id)

        video_id = self._search_regex(r"_icareus\['itemId'\]\s*=\s*'(\d+)'", webpage, 'video_id')
        organization_id = self._search_regex(r"_icareus\['organizationId'\]\s*=\s*'(\d+)'", webpage, 'organization_id')

        assets = self._download_json(
            self._search_regex(r'var\s+publishingServiceURL\s*=\s*"(http[^"]+)";', webpage, 'api_base'),
            video_id, data=urlencode_postdata({
                'version': '03',
                'action': 'getAssetPlaybackUrls',
                'organizationId': organization_id,
                'assetId': video_id,
                'token': self._search_regex(r"_icareus\['token'\]\s*=\s*'([a-f0-9]+)'", webpage, 'icareus_token'),
            }))

        subtitles = {
            remove_end(sdesc.split(' ')[0], ':'): [{'url': url_or_none(surl)}]
            for _, sdesc, surl in assets.get('subtitles') or []
        }

        formats = [{
            'format': item.get('name'),
            'format_id': 'audio',
            'vcodec': 'none',
            'url': url_or_none(item['url']),
            'tbr': int_or_none(self._search_regex(
                r'\((\d+)\s*k\)', item.get('name') or '', 'audio bitrate', default=None)),
        } for item in assets.get('audio_urls') or [] if url_or_none(item.get('url'))]

        for item in assets.get('urls') or []:
            video_url = url_or_none(item.get('url'))
            if video_url is None:
                continue
            ext = determine_ext(video_url)