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

Code before:
            'description': 'md5:de6ca5d5a2d56849e4021f2bf2850df4',
            'duration': 1320,
            'series': 'Backstage',
            'season_number': 1,
            'episode_number': 8,
            'episode': '8. episode',
            'age_limit': 0,
        },
        'params': {
            'skip_download': True,
        },
        'skip': 'ProgramRightsHasExpired',
    }]

    def _real_extract(self, url):
        display_id, season_number, episode_number = self._match_valid_url(url).groups()

        webpage = self._download_webpage(url, display_id)

        info = self._search_json_ld(webpage, display_id, default={})
        nrk_id = info.get('@id') or self._html_search_meta(
            'nrk:program-id', webpage, default=None) or self._search_regex(
            (rf'"selectedEpisodePrfId":"{NRKTVIE._EPISODE_RE}"',
             rf'data-program-id=["\']({NRKTVIE._EPISODE_RE})'), webpage,
            'nrk id')
        assert re.match(NRKTVIE._EPISODE_RE, nrk_id)

        info.update({
            '_type': 'url',
            'id': nrk_id,
            'url': f'nrk:{nrk_id}',
            'ie_key': NRKIE.ie_key(),
            'season_number': int(season_number),
            'episode_number': int(episode_number),
        })
        return info


class NRKTVSerieBaseIE(NRKBaseIE):
    def _extract_entries(self, entry_list):
        if not isinstance(entry_list, list):
            return []
        entries = []
        for episode in entry_list:
            nrk_id = episode.get('prfId') or episode.get('episodeId')
            if not nrk_id or not isinstance(nrk_id, str):
                continue
            entries.append(self.url_result(
                f'nrk:{nrk_id}', ie=NRKIE.ie_key(), video_id=nrk_id))
        return entries


Review comment (refactor):
I'm not a fan of the old code *at all*

I don't like returning the JSON LD info in the flat result since it could differ from what `NRKIE` returns

I don't like the `assert` (this project's convention is that only developers should ever see an `AssertionError`, not the end user)

IMO we should get rid of the old code that doesn't work anymore, and we should also check if the redirected URL already has what we need

```py

    def _real_extract(self, url):
        display_id, season_number, episode_number = self._match_valid_url(url).group(
            'id', 'season_number', 'episode_number')
        webpage, urlh = self._download_webpage_handle(url, display_id)

        if NRKTVIE.suitable(urlh.url):
            nrk_id = NRKTVIE._match_id(urlh.url)
        else:
            nrk_id = self._search_json(
                r'<script\b[^>]+\bid="pageData"[^>]*>', webpage,
                'page data', display_id)['initialState']['selectedEpisodePrfId']
            if not re.fullmatch(NRKTVIE._EPISODE_RE, nrk_id):
                raise ExtractorError('Unable to extract NRK ID')

        return self.url_result(
            f'nrk:{nrk_id}', NRKIE, nrk_id,
            season_number=int(season_number),
            episode_number=int(episode_number))
```

Code after:
            'title': 'Backstage - 8. episode',
            'description': 'md5:de6ca5d5a2d56849e4021f2bf2850df4',
            'duration': 1320,
            'series': 'Backstage',
            'season_number': 1,
            'episode_number': 8,
            'episode': '8. episode',
            'age_limit': 0,
        },
        'params': {
            'skip_download': True,
        },
        'skip': 'ProgramRightsHasExpired',
    }]

    def _real_extract(self, url):
        display_id, season_number, episode_number = self._match_valid_url(url).group(
            'id', 'season_number', 'episode_number')
        webpage, urlh = self._download_webpage_handle(url, display_id)

        if NRKTVIE.suitable(urlh.url):
            nrk_id = NRKTVIE._match_id(urlh.url)
        else:
            nrk_id = self._search_json(
                r'<script\b[^>]+\bid="pageData"[^>]*>', webpage,
                'page data', display_id)['initialState']['selectedEpisodePrfId']
            if not re.fullmatch(NRKTVIE._EPISODE_RE, nrk_id):
                raise ExtractorError('Unable to extract NRK ID')

        return self.url_result(
            f'nrk:{nrk_id}', NRKIE, nrk_id,
            season_number=int(season_number),
            episode_number=int(episode_number))


class NRKTVSerieBaseIE(NRKBaseIE):
    def _extract_entries(self, entry_list):
        if not isinstance(entry_list, list):
            return []
        entries = []
        for episode in entry_list:
            nrk_id = episode.get('prfId') or episode.get('episodeId')
            if not nrk_id or not isinstance(nrk_id, str):
                continue
            entries.append(self.url_result(
                f'nrk:{nrk_id}', ie=NRKIE.ie_key(), video_id=nrk_id))
        return entries

    _ASSETS_KEYS = ('episodes', 'instalments')

    def _extract_assets_key(self, embedded):