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

Code before:

        if m3u8_info.get('errorCode') == 1:
            # errorCode 1 means geo-blocked
            self.raise_geo_restricted(countries=['CA'])
        elif m3u8_info.get('errorCode') not in [35, 0, None]:
            # 35 means media unavailable, which is handled below. 0 means no
            # error. None is there just in case. Any other errorCode can't be
            # handled as we don't know what it is.
            raise ExtractorError(
                f'CBCGem said {m3u8_info.get("errorCode")} - {m3u8_info.get("message")}'
            )

        m3u8_url = m3u8_info.get('url')

        # Sometimes the m3u8 URL is a not available, and errorCode is 35.
        # This might only happen with member content. But just retrying seems
        # to work fine.
        max_retries = self.get_param('extractor_retries', 15)
        i = 0
        while m3u8_url is None:
            if i == max_retries:
                # Didn't work after all tries, give up
                raise ExtractorError("Couldn't retrieve m3u8 URL")
            m3u8_url = self._download_json(
                video_info['playSession']['url'], video_id).get('url')
            i += 1

        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'

                # Put described audio at the beginning of the list, so that it
                # isn't chosen by default, as most people won't want it.
                if 'descriptive' in format['format_id'].lower():
                    format['preference'] = -2

        self._sort_formats(formats)

        return {
            'id': video_id,
            'title': video_info['title'],
            'description': video_info.get('description'),
            'thumbnail': video_info.get('image'),
            'series': video_info.get('series'),
            'season_number': video_info.get('season'),

Review comment (refactor):
(untested)
```suggestion
        last_error, attempt, retries = None, -1, self.get_param('extractor_retries', 3)
        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)
```

Code after:
        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'

                # Put described audio at the beginning of the list, so that it
                # isn't chosen by default, as most people won't want it.
                if 'descriptive' in format['format_id'].lower():
                    format['preference'] = -2

        self._sort_formats(formats)

        return {
            'id': video_id,
            'title': video_info['title'],
            'description': video_info.get('description'),
            'thumbnail': video_info.get('image'),
            'series': video_info.get('series'),
            'season_number': video_info.get('season'),
            'season': f'Season {video_info.get("season")}',
            'episode_number': video_info.get('episode'),
            'episode': video_info.get('title'),
            'episode_id': video_id,
            'duration': video_info.get('duration'),
            'categories': [video_info.get('category')],