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

Code before:
                self.to_screen(f'{video_id}: URL is invalid, skipping: {e.cause}')
                continue
            return {
                'url': url,
                'filesize': int_or_none(response.headers.get('Content-Length'))
            }

    def _raise_if_restricted(self, webpage):
        page_title = clean_html(get_element_by_class('page-title', webpage)) or ''
        if re.fullmatch(r'(?:Channel|Video) Restricted', page_title):
            reason = clean_html(get_element_by_id('page-detail', webpage)) or page_title
            self.raise_geo_restricted(reason)

    def _real_extract(self, url):
        base_url, video_id = self._match_valid_url(url).group('base_url', 'id')
        webpage = self._download_webpage(
            f'https://www.bitchute.com/video/{video_id}', video_id, headers=self._HEADERS)

        self._raise_if_restricted(webpage)
        publish_date = clean_html(get_element_by_class('video-publish-date', webpage))
        entries = self._parse_html5_media_entries(url, webpage, video_id)
        details = get_element_by_class('details', webpage) or ''
        uploader_path = extract_attributes(
            get_element_html_by_class('spa', get_element_html_by_class('creator', details)) or '').get('href')
        channel_path = extract_attributes(
            get_element_html_by_class('spa', get_element_html_by_class('name', details)) or '').get('href')

        formats = []
        for format_ in traverse_obj(entries, (0, 'formats', ...)):
            if self.get_param('check_formats') is not False:
                format_.update(self._check_format(format_.pop('url'), video_id) or {})
                if 'url' not in format_:
                    continue
            formats.append(format_)

        if not formats:
            self.raise_no_formats(
                'Video is unavailable. Please make sure this video is playable in the browser '
                'before reporting this issue.', expected=True, video_id=video_id)

        return {
            'id': video_id,
            'title': self._html_extract_title(webpage) or self._og_search_title(webpage),
            'description': self._og_search_description(webpage, default=None),
            'thumbnail': self._og_search_thumbnail(webpage),
            'uploader': clean_html(get_element_by_class('name', details)),
            'upload_date': unified_strdate(self._search_regex(
                r'at \d+:\d+ UTC on (.+?)\.', publish_date, 'upload date', fatal=False)),
            'uploader_url': f'{base_url}{uploader_path}' if uploader_path else None,
            'channel_url': f'{base_url}{channel_path}' if channel_path else None,
            'formats': formats,

Review comment (refactor):
1. all of this "details" extraction should be moved down near the return for better code flow/readability
2. `uploader_path` and `channel_path` are currently fatal because `get_element_html_by_class` can return `None` and throws when its html input is `None`
3. matching `base_url` in `_VALID_URL` is overkill, we can always just use `https://www.bitchute.com`
4. use `utils.urljoin`
5. move duplicated code into a function
```py
        details = get_element_by_class('details', webpage) or ''
        uploader_html = get_element_html_by_class('creator', details) or ''
        channel_html = get_element_html_by_class('name', details) or ''

        def construct_url(html):
            path = extract_attributes(get_element_html_by_class('spa', html) or '').get('href')
            return urljoin('https://www.bitchute.com', path)

        return {
            # ...
            'uploader': clean_html(uploader_html),
            'channel': clean_html(channel_html),
            'uploader_url': construct_url(uploader_html),
            'channel_url': construct_url(channel_html),
            # ...
        }
```

Code after:
    _GEO_BYPASS = False

    _HEADERS = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.57 Safari/537.36',
        'Referer': 'https://www.bitchute.com/',
    }

    def _check_format(self, video_url, video_id):
        urls = orderedSet(
            re.sub(r'(^https?://)(seed\d+)(?=\.bitchute\.com)', fr'\g<1>{host}', video_url)
            for host in (r'\g<2>', 'seed122', 'seed125', 'seed126', 'seed128',
                         'seed132', 'seed150', 'seed151', 'seed152', 'seed153',
                         'seed167', 'seed171', 'seed177', 'seed305', 'seed307',
                         'seedp29xb', 'zb10-7gsop1v78'))
        for url in urls:
            try:
                response = self._request_webpage(
                    HEADRequest(url), video_id=video_id, note=f'Checking {url}', headers=self._HEADERS)
            except ExtractorError as e:
                self.to_screen(f'{video_id}: URL is invalid, skipping: {e.cause}')
                continue
            return {
                'url': url,
                'filesize': int_or_none(response.headers.get('Content-Length'))
            }

    def _raise_if_restricted(self, webpage):
        page_title = clean_html(get_element_by_class('page-title', webpage)) or ''
        if re.fullmatch(r'(?:Channel|Video) Restricted', page_title):
            reason = clean_html(get_element_by_id('page-detail', webpage)) or page_title
            self.raise_geo_restricted(reason)

    @staticmethod
    def _make_url(html):
        path = extract_attributes(get_element_html_by_class('spa', html) or '').get('href')
        return urljoin('https://www.bitchute.com', path)

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(
            f'https://www.bitchute.com/video/{video_id}', video_id, headers=self._HEADERS)

        self._raise_if_restricted(webpage)
        publish_date = clean_html(get_element_by_class('video-publish-date', webpage))
        entries = self._parse_html5_media_entries(url, webpage, video_id)

        formats = []
        for format_ in traverse_obj(entries, (0, 'formats', ...)):
            if self.get_param('check_formats') is not False:
                format_.update(self._check_format(format_.pop('url'), video_id) or {})
                if 'url' not in format_: