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

Code before:
        'skip': 'Geo-restricted to Canada',
    }, {
        # geo-restricted to Canada, bypassable
        # TV show playlist, all public videos at time of coding (2021-09)
        'url': 'https://gem.cbc.ca/media/schitts-creek/s06',
        'playlist_count': 16,
        'info_dict': {
            'id': 'schitts-creek/s06',
            'title': 'Season 6',
            'description': 'md5:6a92104a56cbeb5818cc47884d4326a2',
        },
        'skip': 'Geo-restricted to Canada',
    }]
    _API_BASE = 'https://services.radio-canada.ca/ott/cbc-api/v2/'
    _API_VIDEO = _API_BASE + 'assets/'
    _API_SHOW = _API_BASE + 'shows/'
    _BROWSER_BASE = 'https://gem.cbc.ca/media/'
    # Regexes for analyzing video_id - the id group of _VALID_URL
    _VIDEO_ID = r'[0-9a-z-]+/s[0-9]+[a-z][0-9]+'
    _PLAYLIST_ID = r'([0-9a-z-]+)/s([0-9]+)'

    def _real_extract(self, url):
        url_id = self._match_id(url)

        playlist_match = re.fullmatch(self._PLAYLIST_ID, url_id)
        if playlist_match:
            season_id = url_id
            show = playlist_match.group(1)
            show_info = self._download_json(self._API_SHOW + show, season_id)
            season = int(playlist_match.group(2))
            season_info = list_get(show_info['seasons'], season - 1)

            if season_info is None:
                raise ExtractorError(f"Couldn't find season {season} of {show}")

            episodes = []
            for episode in season_info['assets']:
                episodes.append({
                    '_type': 'url_transparent',
                    'ie_key': 'CBCGem',
                    'url': self._BROWSER_BASE + episode['id'],
                    'id': episode['id'],
                    'title': episode.get('title'),
                    'description': episode.get('description'),
                    'thumbnail': episode.get('image'),
                    'series': episode.get('series'),
                    'season_number': episode.get('season'),
                    'season': season_info['title'],
                    'season_id': season_info.get('id'),
                    'episode_number': episode.get('episode'),
                    'episode': episode.get('title'),

Review comment (refactor):
having a massive `if` is a sign that the 2 urls is probly better handled by 2 seperate extractors

Code after:
            'title': 'The Cup Runneth Over',
            'description': 'md5:9bca14ea49ab808097530eb05a29e797',
            'thumbnail': 'https://images.radio-canada.ca/v1/synps-cbc/episode/perso/cbc_schitts_creek_season_01e01_thumbnail_v01.jpg?im=Resize=(Size)',
            'series': 'Schitt\'s Creek',
            'season_number': 1,
            'season': 'Season 1',
            'episode_number': 1,
            'episode': 'The Cup Runneth Over',
            'episode_id': 'schitts-creek/s01e01',
            'duration': 1309,
            'categories': ['comedy'],
        },
        'params': {'format': 'bv'},
        'skip': 'Geo-restricted to Canada',
    }]
    _API_BASE = 'https://services.radio-canada.ca/ott/cbc-api/v2/assets/'

    def _real_extract(self, url):
        video_id = self._match_id(url)
        video_info = self._download_json(self._API_BASE + video_id, video_id)

        last_error = None
        attempt = -1
        retries = self.get_param('extractor_retries', 15)
        while attempt < retries:
            attempt += 1
            if last_error:
                self.report_warning('%s. Retrying ...' % last_error)
            m3u8_info = self._download_json(
                video_info['playSession']['url'], video_id,
                note='Downloading JSON metadata%s' % f' (attempt {attempt})')
            m3u8_url = m3u8_info.get('url')
            if m3u8_url:
                break
            elif m3u8_info.get('errorCode') == 1:
                self.raise_geo_restricted(countries=['CA'])
            else:
                last_error = f'{self.IE_NAME} said: {m3u8_info.get("errorCode")} - {m3u8_info.get("message")}'
                # 35 means media unavailable, but retries work
                if m3u8_info.get('errorCode') != 35 or attempt >= retries:
                    raise ExtractorError(last_error)

        formats = self._extract_m3u8_formats(m3u8_url, video_id, m3u8_id='hls')
        self._remove_duplicate_formats(formats)

        for i, format in enumerate(formats):
            if format.get('vcodec') == 'none':
                if format.get('ext') is None:
                    format['ext'] = 'm4a'
                if format.get('acodec') is None:
                    format['acodec'] = 'mp4a.40.2'