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

Code before:
            '_format_sort_fields': ('res', 'proto')  # Prioritize direct http formats over HLS
        }


class CBCPlayerPlaylistIE(InfoExtractor):
    IE_NAME = 'cbc.ca:player:playlist'
    _VALID_URL = r'(?:cbcplayer:|https?://(?:www\.)?cbc\.ca/(?:player/)(?!play/))(?P<id>.+)'
    _TESTS = [{
        'url': 'https://www.cbc.ca/player/news/TV%20Shows/The%20National/Latest%20Broadcast',
        'playlist_mincount': 25,
        'info_dict': {
            'id': 'news/tv shows/the national/latest broadcast',
            'ie_key': 'CBCPlayer',
        }
    }, {
        'url': 'https://www.cbc.ca/player/news/Canada/North',
        'playlist_mincount': 25,
        'info_dict': {
            'id': 'news/canada/north',
            'ie_key': 'CBCPlayer',
        }
    }]

    def _real_extract(self, url):
        # We have no other playlist id other than the URL, so use that
        playlist_id = self._match_id(url).replace('%20', ' ').lower()
        # the json info we're looking for isn't marked as such, therefore we need a bit of a workaround.
        json_content = self._parse_json(
            re.sub(
                r'^.*?{', '{',
                get_element_by_id('initialStateDom', self._download_webpage(url, playlist_id))
            )[:-1],
            playlist_id)
        playlist_items = []
        for key, value in json_content.get('video').get('clipsByCategory').items():
            # We need to do a case insensitive match. If anyone has a better way, feel free to improve.
            if key.lower() == playlist_id:
                for video in value.get('items'):
                    playlist_items.append({
                        '_type': 'url',
                        'url': 'https://www.cbc.ca/player/play/%s' % video['id'],
                        'ie_key': 'CBCPlayer',
                        'timestamp': video['airDate'],
                        'categories': [video['contentArea']],
                        'description': video['description'],
                        'duration': video['duration'],
                        'id': video['id'],
                        'is_live': video['isLive'],
                        'series': video['showName'],
                        'thumbnail': video['thumbnail'],
                        'title': video['title'],

Review comment (suggestion):
IMO use `urllib.parse.unqouote` (and add `import urllib.parse` to top of file) in case there is other percent-encoding in URL
```suggestion
        playlist_id = urllib.parse.unquote(self._match_id(url)).lower()
```

Code after:
            '_format_sort_fields': ('res', 'proto')  # Prioritize direct http formats over HLS
        }


class CBCPlayerPlaylistIE(InfoExtractor):
    IE_NAME = 'cbc.ca:player:playlist'
    _VALID_URL = r'https?://(?:www\.)?cbc\.ca/(?:player/)(?!play/)(?P<id>[^?#]+)'
    _TESTS = [{
        'url': 'https://www.cbc.ca/player/news/TV%20Shows/The%20National/Latest%20Broadcast',
        'playlist_mincount': 25,
        'info_dict': {
            'id': 'news/tv shows/the national/latest broadcast',
        }
    }, {
        'url': 'https://www.cbc.ca/player/news/Canada/North',
        'playlist_mincount': 25,
        'info_dict': {
            'id': 'news/canada/north',
        }
    }]

    def _real_extract(self, url):
        playlist_id = urllib.parse.unquote(self._match_id(url)).lower()
        webpage = self._download_webpage(url, playlist_id)
        json_content = self._search_json(
            r'window\.__INITIAL_STATE__\s*=', webpage, 'initial state', playlist_id)

        def entries():
            for video_id in traverse_obj(json_content, (
                'video', 'clipsByCategory', lambda k, _: k.lower() == playlist_id, 'items', ..., 'id'
            )):
                yield self.url_result(f'https://www.cbc.ca/player/play/{video_id}', CBCPlayerIE)

        return self.playlist_result(entries(), playlist_id)


class CBCGemIE(InfoExtractor):
    IE_NAME = 'gem.cbc.ca'
    _VALID_URL = r'https?://gem\.cbc\.ca/(?:media/)?(?P<id>[0-9a-z-]+/s[0-9]+[a-z][0-9]+)'
    _TESTS = [{
        # This is a normal, public, TV show video
        'url': 'https://gem.cbc.ca/media/schitts-creek/s06e01',
        'md5': '93dbb31c74a8e45b378cf13bd3f6f11e',
        'info_dict': {
            'id': 'schitts-creek/s06e01',
            'ext': 'mp4',
            'title': 'Smoke Signals',
            'description': 'md5:929868d20021c924020641769eb3e7f1',
            'thumbnail': 'https://images.radio-canada.ca/v1/synps-cbc/episode/perso/cbc_schitts_creek_season_06e01_thumbnail_v01.jpg?im=Resize=(Size)',
            'duration': 1314,
            'categories': ['comedy'],