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

Code before:
        manifest = call_playback_api('manifest', {'preferredCdn': 'akamai'})

        video_id = try_get(manifest, lambda x: x['id'], str) or video_id

        if manifest.get('playability') == 'nonPlayable':
            self._raise_error(manifest['nonPlayable'])

        playable = manifest['playable']

        formats = []
        for asset in playable['assets']:
            if not isinstance(asset, dict):
                continue
            if asset.get('encrypted'):
                continue
            format_url = url_or_none(asset.get('url'))
            if not format_url:
                continue
            asset_format = (asset.get('format') or '').lower()
            if asset_format == 'hls' or determine_ext(format_url) == 'm3u8':
                # Remove the 'adap' query parameter. Returns different streams depending on the value.
                # Known values are: small, large, small_h265, large_h265. Removing the parameter returns all available streams.
                format_url = update_url_query(format_url, {'adap': []})
                # Disable subtitles, they are fetched separately.
                format_url = update_url_query(format_url, {'s': 0})
                formats.extend(self._extract_nrk_formats(format_url, video_id))
            elif asset_format == 'mp3':
                formats.append({
                    'url': format_url,
                    'format_id': asset_format,
                    'vcodec': 'none',
                })

        data = call_playback_api('metadata')

        preplay = data['preplay']
        titles = preplay['titles']
        title = titles['title']
        alt_title = titles.get('subtitle')

        description = try_get(preplay, lambda x: x['description'].replace('\r', '\n'))
        duration = parse_duration(playable.get('duration')) or parse_duration(data.get('duration'))

        thumbnails = []
        for image in try_get(
                preplay, lambda x: x['poster']['images'], list) or []:
            if not isinstance(image, dict):
                continue
            image_url = url_or_none(image.get('url'))
            if not image_url:
                continue

Review comment (refactor):
IMO it would make more sense to do this stuff in the `_extract_nrk_formats` method (where we are already performing some URL surgery), and it could be simplified too

```diff
     def _extract_nrk_formats(self, asset_url, video_id):
+        asset_url = update_url_query(asset_url, {
+            # Remove 'adap' to return all streams (known values are: small, large, small_h265, large_h265)
+            'adap': [],
+            # Disable subtitles since they are fetched separately
+            's': 0,
+         })
         if re.match(r'https?://[^/]+\.akamaihd\.net/i/', asset_url):
             return self._extract_akamai_formats(asset_url, video_id)
         asset_url = re.sub(r'(?:bw_(?:low|high)=\d+|no_audio_only)&?', '', asset_url)
         formats = self._extract_m3u8_formats(
             asset_url, video_id, 'mp4', 'm3u8_native', fatal=False)
         if not formats and re.search(self._CDN_REPL_REGEX, asset_url):
             formats = self._extract_m3u8_formats(
                 re.sub(self._CDN_REPL_REGEX, '://nrk-od-%02d.akamaized.net/no/' % random.randint(0, 99), asset_url),
                 video_id, 'mp4', 'm3u8_native', fatal=False)
         return formats
```

```suggestion
                formats.extend(self._extract_nrk_formats(format_url, video_id))
```

Code after:
                return self._call_api(f'playback/{item}/program/{video_id}', video_id, item, query=query)
            except ExtractorError as e:
                if isinstance(e.cause, HTTPError) and e.cause.status == 400:
                    return self._call_api(f'playback/{item}/{video_id}', video_id, item, query=query)
                raise

        # known values for preferredCdn: akamai, globalconnect and telenor
        manifest = call_playback_api('manifest', {'preferredCdn': 'akamai'})

        video_id = try_get(manifest, lambda x: x['id'], str) or video_id

        if manifest.get('playability') == 'nonPlayable':
            self._raise_error(manifest['nonPlayable'])

        playable = manifest['playable']

        formats = []
        for asset in playable['assets']:
            if not isinstance(asset, dict):
                continue
            if asset.get('encrypted'):
                continue
            format_url = url_or_none(asset.get('url'))
            if not format_url:
                continue
            asset_format = (asset.get('format') or '').lower()
            if asset_format == 'hls' or determine_ext(format_url) == 'm3u8':
                formats.extend(self._extract_nrk_formats(format_url, video_id))
            elif asset_format == 'mp3':
                formats.append({
                    'url': format_url,
                    'format_id': asset_format,
                    'vcodec': 'none',
                })

        data = call_playback_api('metadata')

        preplay = data['preplay']
        titles = preplay['titles']
        title = titles['title']
        alt_title = titles.get('subtitle')

        description = try_get(preplay, lambda x: x['description'].replace('\r', '\n'))
        duration = parse_duration(playable.get('duration')) or parse_duration(data.get('duration'))

        thumbnails = []
        for image in try_get(
                preplay, lambda x: x['poster']['images'], list) or []:
            if not isinstance(image, dict):
                continue
            image_url = url_or_none(image.get('url'))