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

Code before:
    _LOGIN_ERRORS = {
        1502: 'The username and password you entered did not match.',  # LOGIN_FAILED
        1503: 'Payment method is expired.',  # LOGIN_FAILED_ACCOUNT_NOT_ACTIVE
        1506: 'Your BrainPOP plan has expired.',  # LOGIN_FAILED_ACCOUNT_EXPIRED
        1507: 'Terms not accepted.',  # LOGIN_FAILED_TERMS_NOT_ACCEPTED
        1508: 'Account not activated.',  # LOGIN_FAILED_SUBSCRIPTION_NOT_ACTIVE
        1512: 'The maximum number of devices permitted are logged in with your account right now.',  # LOGIN_FAILED_LOGIN_LIMIT_REACHED
        1513: 'You are trying to access your account from outside of its allowed IP range.',  # LOGIN_FAILED_INVALID_IP
        1514: 'Individual accounts are not included in your plan. Try again with your shared username and password.',  # LOGIN_FAILED_MBP_DISABLED
        1515: 'Account not activated.',  # LOGIN_FAILED_TEACHER_NOT_ACTIVE
        1523: 'That username and password won\'t work on this BrainPOP site.',  # LOGIN_FAILED_NO_ACCESS
        1524: 'You\'ll need to join a class before you can login.',  # LOGIN_FAILED_STUDENT_NO_PERIOD
        1526: 'Your account is locked. Reset your password, or ask a teacher or administrator for help.',  # LOGIN_FAILED_ACCOUNT_LOCKED
    }

    def _assemble_formats(self, slug, format_id, display_id, token='', extra_fields={}):
        formats = []
        formats.append({
            'format_id': format_id,
            'url': '%s?%s' % (urljoin(self._VIDEO_URL, slug), token),
            **extra_fields
        })
        formats.extend({**f, **extra_fields} for f in self._extract_m3u8_formats(
            '%s.m3u8?%s' % (urljoin(self._HLS_URL, slug), token),
            display_id, 'mp4', m3u8_id=f'{format_id}-hls', fatal=False))
        return formats

    def _extract_adaptive_formats(self, data, token, display_id, key_format='%s', extra_fields={}):
        formats = []
        if data.get(key_format % 'high'):
            formats.extend(self._assemble_formats(data[key_format % 'high'], 'high', display_id, token, {
                'quality': -1,
                **extra_fields
            }))
        if data.get(key_format % 'low'):
            formats.extend(self._assemble_formats(data[key_format % 'high'], 'low', display_id, token, {
                'quality': -2,
                **extra_fields
            }))
        if data.get(key_format % 'ad_high'):
            formats.extend(self._assemble_formats(data[key_format % 'ad_high'], 'ad_high', display_id, token, {
                'quality': -1,
                'format_note': 'Audio description',
                'source_preference': -2,
                **extra_fields
            }))
        if data.get(key_format % 'ad_low'):
            formats.extend(self._assemble_formats(data[key_format % 'ad_low'], 'ad_low', display_id, token, {
                'quality': -2,
                'format_note': 'Audio description',
                'source_preference': -2,

Review comment (refactor):
```suggestion
        formats = self._extract_m3u8_formats(
            f'{urljoin(self._VIDEO_URL, slug)}.m3u8?{token}',
            display_id, 'mp4', m3u8_id=f'{format_id}-hls', fatal=False)
        formats.append({
            'format_id': format_id,
            'url': f'{urljoin(self._VIDEO_URL, slug)}?{token}',
        })
        for f in formats:
            f.update(extra_fields)
        return formats
```

Code after:
class BrainPOPBaseIE(InfoExtractor):
    _NETRC_MACHINE = 'brainpop'
    _ORIGIN = ''  # So that _VALID_URL doesn't crash
    _LOGIN_ERRORS = {
        1502: 'The username and password you entered did not match.',  # LOGIN_FAILED
        1503: 'Payment method is expired.',  # LOGIN_FAILED_ACCOUNT_NOT_ACTIVE
        1506: 'Your BrainPOP plan has expired.',  # LOGIN_FAILED_ACCOUNT_EXPIRED
        1507: 'Terms not accepted.',  # LOGIN_FAILED_TERMS_NOT_ACCEPTED
        1508: 'Account not activated.',  # LOGIN_FAILED_SUBSCRIPTION_NOT_ACTIVE
        1512: 'The maximum number of devices permitted are logged in with your account right now.',  # LOGIN_FAILED_LOGIN_LIMIT_REACHED
        1513: 'You are trying to access your account from outside of its allowed IP range.',  # LOGIN_FAILED_INVALID_IP
        1514: 'Individual accounts are not included in your plan. Try again with your shared username and password.',  # LOGIN_FAILED_MBP_DISABLED
        1515: 'Account not activated.',  # LOGIN_FAILED_TEACHER_NOT_ACTIVE
        1523: 'That username and password won\'t work on this BrainPOP site.',  # LOGIN_FAILED_NO_ACCESS
        1524: 'You\'ll need to join a class before you can login.',  # LOGIN_FAILED_STUDENT_NO_PERIOD
        1526: 'Your account is locked. Reset your password, or ask a teacher or administrator for help.',  # LOGIN_FAILED_ACCOUNT_LOCKED
    }

    @classproperty
    def _VALID_URL(cls):
        root = re.escape(cls._ORIGIN).replace(r'https:', r'https?:').replace(r'www\.', r'(?:www\.)?')
        return rf'{root}/(?P<slug>[^/]+/[^/]+/(?P<id>[^/?#&]+))'

    def _assemble_formats(self, slug, format_id, display_id, token='', extra_fields={}):
        formats = []
        formats = self._extract_m3u8_formats(
            f'{urljoin(self._HLS_URL, slug)}.m3u8?{token}',
            display_id, 'mp4', m3u8_id=f'{format_id}-hls', fatal=False)
        formats.append({
            'format_id': format_id,
            'url': f'{urljoin(self._VIDEO_URL, slug)}?{token}',
        })
        for f in formats:
            f.update(extra_fields)
        return formats

    def _extract_adaptive_formats(self, data, token, display_id, key_format='%s', extra_fields={}):
        formats = []
        additional_key_formats = {
            '%s': {},
            'ad_%s': {
                'format_note': 'Audio description',
                'source_preference': -2
            }
        }
        for additional_key_format, additional_key_fields in additional_key_formats.items():
            for key_quality, key_index in enumerate(('high', 'low')):
                full_key_index = additional_key_format % (key_format % key_index)
                if data.get(full_key_index):
                    formats.extend(self._assemble_formats(data[full_key_index], full_key_index, display_id, token, {
                        'quality': -1 - key_quality,