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

Code before:
        # reddit video @ nm reddit
        'url': 'https://nm.reddit.com/r/Cricket/comments/8idvby/lousy_cameraman_finds_himself_in_cairns_line_of/',
        'only_matching': True,
    }, {
        'url': 'https://www.redditmedia.com/r/serbia/comments/pu9wbx/ako_vu%C4%8Di%C4%87_izgubi_izbore_ja_%C4%87u_da_crknem/',
        'only_matching': True,
    }]

    def _perform_login(self, username, password):
        captcha = self._download_json(
            'https://www.reddit.com/api/requires_captcha/login.json', None,
            'Checking login requirement')['required']
        if captcha:
            raise ExtractorError('Reddit is requiring captcha before login', expected=True)
        login = self._download_json(
            f'https://www.reddit.com/api/login/{username}', None, data=urlencode_postdata({
                'op': 'login-main',
                'user': username,
                'passwd': password,
                'api_type': 'json',
            }), note='Logging in', errnote='Login request failed')
        errors = traverse_obj(login, ('json', 'errors', ...))
        if errors or not traverse_obj(login, ('json', 'data', 'cookie', {str})):
            raise ExtractorError(
                f'Unable to login, {"Reddit API says: " if errors else "no cookie was given"}'
                f'{"; ".join(traverse_obj(errors, (..., 1)))}', expected=bool(errors))

    def _real_extract(self, url):
        host, slug, video_id = self._match_valid_url(url).group('host', 'slug', 'id')

        data = self._download_json(
            f'https://{host}/{slug}/.json', video_id, fatal=False, expected_status=403)
        if not data:
            fallback_host = 'old.reddit.com' if host != 'old.reddit.com' else 'www.reddit.com'
            self.to_screen(f'{host} request failed, retrying with {fallback_host}')
            data = self._download_json(
                f'https://{fallback_host}/{slug}/.json', video_id, expected_status=403)

        if traverse_obj(data, 'error') == 403:
            reason = data.get('reason')
            if reason == 'quarantined':
                self.raise_login_required('Quarantined subreddit; an account that has opted in is required')
            elif reason == 'private':
                self.raise_login_required('Private subreddit; an account that has been approved is required')
            else:
                raise ExtractorError(f'HTTP Error 403 Forbidden; reason given: {reason}')

        data = data[0]['data']['children'][0]['data']
        video_url = data['url']

        over_18 = data.get('over_18')

Review comment (refactor):
Unnecessary DRY imo

```suggestion
        errors = ';'.join(traverse_obj(login, ('json', 'errors', ..., 1)))
        if errors:
            raise ExtractorError(f'Unable to login, Reddit API says {errors}', expected=True)
        elif not traverse_obj(login, ('json', 'data', 'cookie', {str})):
            raise ExtractorError('Unable to login, no cookie was returned')
```

Code after:
        # reddit video @ nm reddit
        'url': 'https://nm.reddit.com/r/Cricket/comments/8idvby/lousy_cameraman_finds_himself_in_cairns_line_of/',
        'only_matching': True,
    }, {
        'url': 'https://www.redditmedia.com/r/serbia/comments/pu9wbx/ako_vu%C4%8Di%C4%87_izgubi_izbore_ja_%C4%87u_da_crknem/',
        'only_matching': True,
    }]

    def _perform_login(self, username, password):
        captcha = self._download_json(
            'https://www.reddit.com/api/requires_captcha/login.json', None,
            'Checking login requirement')['required']
        if captcha:
            raise ExtractorError('Reddit is requiring captcha before login', expected=True)
        login = self._download_json(
            f'https://www.reddit.com/api/login/{username}', None, data=urlencode_postdata({
                'op': 'login-main',
                'user': username,
                'passwd': password,
                'api_type': 'json',
            }), note='Logging in', errnote='Login request failed')
        errors = '; '.join(traverse_obj(login, ('json', 'errors', ..., 1)))
        if errors:
            raise ExtractorError(f'Unable to login, Reddit API says {errors}', expected=True)
        elif not traverse_obj(login, ('json', 'data', 'cookie', {str})):
            raise ExtractorError('Unable to login, no cookie was returned')

    def _real_extract(self, url):
        host, slug, video_id = self._match_valid_url(url).group('host', 'slug', 'id')

        data = self._download_json(
            f'https://{host}/{slug}/.json', video_id, fatal=False, expected_status=403)
        if not data:
            fallback_host = 'old.reddit.com' if host != 'old.reddit.com' else 'www.reddit.com'
            self.to_screen(f'{host} request failed, retrying with {fallback_host}')
            data = self._download_json(
                f'https://{fallback_host}/{slug}/.json', video_id, expected_status=403)

        if traverse_obj(data, 'error') == 403:
            reason = data.get('reason')
            if reason == 'quarantined':
                self.raise_login_required('Quarantined subreddit; an account that has opted in is required')
            elif reason == 'private':
                self.raise_login_required('Private subreddit; an account that has been approved is required')
            else:
                raise ExtractorError(f'HTTP Error 403 Forbidden; reason given: {reason}')

        data = data[0]['data']['children'][0]['data']
        video_url = data['url']

        over_18 = data.get('over_18')