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

Code before:
        # get all manifest urls and remove duplicates
        manifests = [f.get('manifest_url') for f in tp_formats]
        manifests = list(dict.fromkeys(filter(None, manifests)))

        # return original formats if not DRM manifest is found
        if len(manifests) != 1 or (
                len(manifests) == 1 and '_sampleaes/' not in manifests[0]):
            return tp_formats

        nodrm_manifest = re.sub(
            r'_sampleaes/(\w+)_fp_', r'/\1_no_', manifests[0])

        if nodrm_manifest == manifests[0]:
            return tp_formats

        return self._extract_m3u8_formats(
            nodrm_manifest, video_id, m3u8_id='hls', fatal=False) or 'DRM'

    def _real_extract(self, url):
        guid = self._match_id(url)
        tp_path = 'PR1GhC/media/guid/2702976343/' + guid
        info = self._extract_theplatform_metadata(tp_path, guid)

        formats = []
        subtitles = {}
        first_e = None
        asset_type = 'geoNo:HD,browser,geoIT|geoNo:HD,geoIT|geoNo:SD,browser,geoIT|geoNo:SD,geoIT|geoNo|HD|SD'
        # TODO: fixup ISM+none manifest URLs
        has_drm = False
        for f in ('MPEG4', 'M3U'):
            try:
                tp_formats, tp_subtitles = self._extract_theplatform_smil(
                    update_url_query('http://link.theplatform.%s/s/%s' % (self._TP_TLD, tp_path), {
                        'mbr': 'true',
                        'formats': f,
                        'assetTypes': asset_type,
                    }), guid, 'Downloading %s SMIL data' % (f.split('+')[0]))
            except ExtractorError as e:
                if not first_e:
                    first_e = e
                continue
            tp_formats = self._check_drm_formats(tp_formats, guid)
            if tp_formats == 'DRM':
                has_drm = True
            else:
                formats.extend(tp_formats)
            subtitles = self._merge_subtitles(subtitles, tp_subtitles)

        # check for errors and report them
        if has_drm and not formats:
            self.report_drm(guid)

Review comment (refactor):
Although it doesn't help to acquire any media, it could be good to prioritise geo-blocking errors over missing formats, etc:
Here:
```
        first_e = geo_e = None
```
At l.240 below, after `first_e = e` (and `GeoRestrictedError` has to be imported):
```
                if not geo_e and isinstance(e, GeoRestrictedError):
                    geo_e = e
```
At l.253 below, instead of `raise first_e`:
```
                raise geo_e or first_e
```
So if no formats can be extracted it's really because formats were blocked from your region rather than because one of the API calls had no result.

Code after:
    def _check_drm_formats(self, tp_formats, video_id):
        has_nondrm, drm_manifest = False, ''
        for f in tp_formats:
            if '_sampleaes/' in (f.get('manifest_url') or ''):
                drm_manifest = drm_manifest or f['manifest_url']
                f['has_drm'] = True
            if not f.get('has_drm') and f.get('manifest_url'):
                has_nondrm = True

        nodrm_manifest = re.sub(r'_sampleaes/(\w+)_fp_', r'/\1_no_', drm_manifest)
        if has_nondrm or nodrm_manifest == drm_manifest:
            return

        tp_formats.extend(self._extract_m3u8_formats(
            nodrm_manifest, video_id, m3u8_id='hls', fatal=False) or [])

    def _real_extract(self, url):
        guid = self._match_id(url)
        tp_path = 'PR1GhC/media/guid/2702976343/' + guid
        info = self._extract_theplatform_metadata(tp_path, guid)

        formats = []
        subtitles = {}
        first_e = geo_e = None
        asset_type = 'geoNo:HD,browser,geoIT|geoNo:HD,geoIT|geoNo:SD,browser,geoIT|geoNo:SD,geoIT|geoNo|HD|SD'
        # TODO: fixup ISM+none manifest URLs
        for f in ('MPEG4', 'M3U'):
            try:
                tp_formats, tp_subtitles = self._extract_theplatform_smil(
                    update_url_query('http://link.theplatform.%s/s/%s' % (self._TP_TLD, tp_path), {
                        'mbr': 'true',
                        'formats': f,
                        'assetTypes': asset_type,
                    }), guid, 'Downloading %s SMIL data' % (f.split('+')[0]))
            except ExtractorError as e:
                if not geo_e and isinstance(e, GeoRestrictedError):
                    geo_e = e
                if not first_e:
                    first_e = e
                continue
            self._check_drm_formats(tp_formats, guid)
            formats.extend(tp_formats)
            subtitles = self._merge_subtitles(subtitles, tp_subtitles)

        # check for errors and report them
        if (first_e or geo_e) and not formats:
            raise geo_e or first_e

        self._sort_formats(formats)

        feed_data = self._download_json(