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

Code before:
        'url': 'https://m.facebook.com/ads/library/?id=901230958115569',
        'only_matching': True,
    }]

    _FORMATS_MAP = {
        'watermarked_video_sd_url': ('sd-wmk', 'SD, watermarked'),
        'video_sd_url': ('sd', None),
        'watermarked_video_hd_url': ('hd-wmk', 'HD, watermarked'),
        'video_hd_url': ('hd', None),
    }

    def _extract_formats(self, video_dict):
        formats = []
        for format_key, format_url in traverse_obj(video_dict, (
            {dict.items}, lambda _, v: v[0] in self._FORMATS_MAP and url_or_none(v[1]),
        )):
            formats.append({
                'format_id': self._FORMATS_MAP[format_key][0],
                'format_note': self._FORMATS_MAP[format_key][1],
                'url': format_url,
                'ext': 'mp4',
                'quality': qualities(tuple(self._FORMATS_MAP))(format_key),
            })
        return formats

    def _download_webpage_handle(self, url_or_request, video_id, *args, fatal=True, **kwargs):
        """
        Facebook may return a 403 with an HTML page containing a JS snippet that:
        1. Fetches a /__rd_verify_... endpoint with POST
        2. That endpoint sets an rd_challenge cookie
        3. The original request with that cookie succeeds

        We need to mimic this for us to see the true webpage

        See https://github.com/yt-dlp/yt-dlp/issues/15577 for details
        """

        # we initially try to download the webpage as normal
        # but we hijack `expected_status` to allow 403 status code too
        # (indicateve of rd_challenge cookie missing)
        _original_expected_status = kwargs.get('expected_status')
        expected_status = _original_expected_status.copy() if _original_expected_status else [200]
        expected_status.append(403)
        kwargs['expected_status'] = expected_status

        response = super()._download_webpage_handle(
            url_or_request,
            video_id,
            *args,
            fatal=fatal,
            **kwargs,

Review comment (refactor):
I'm not a big fan of overriding the common methods that aren't particularly intended to be reimplemented by subclasses. I would just create a new method instead.

Also, instead of using the `expected_status` approach, I would use a try/catch block and write it like:

```py
    def _download_fb_webpage_and_verify(self, url, video_id):
        try:
            return self._download_webpage(url, video_id)
        except ExtractorError as e:
            if (
                not isinstance(e.cause, HTTPError)
                or e.cause.status != 403
                or e.cause.reason != 'Client challenge'
            ):
                raise
            error_page = self._webpage_read_content(e.cause.response, url, video_id)

        # Now outside of the try/except block you can do your stuff to get the real webpage

        return webpage
```

Code after:
        'info_dict': {
            'id': '312304267031140',
            'title': 'Casper Wave Hybrid Mattress',
            'uploader': 'Casper',
            'uploader_id': '224110981099062',
            'uploader_url': 'https://www.facebook.com/Casper/',
            'timestamp': 1766299837,
            'upload_date': '20251221',
            'like_count': int,
        },
        'playlist_count': 2,
    }, {
        'url': 'https://www.facebook.com/ads/library/?id=874812092000430',
        'info_dict': {
            'id': '874812092000430',
            'title': 'TikTok',
            'uploader': 'Case \u00e0 Chocs',
            'uploader_id': '112960472096793',
            'uploader_url': 'https://www.facebook.com/Caseachocs/',
            'timestamp': 1768498293,
            'upload_date': '20260115',
            'like_count': int,
            'description': 'md5:f02a255fcf7dce6ed40e9494cf4bc49a',
        },
        'playlist_count': 3,
    }, {
        'url': 'https://es-la.facebook.com/ads/library/?id=901230958115569',
        'only_matching': True,
    }, {
        'url': 'https://m.facebook.com/ads/library/?id=901230958115569',
        'only_matching': True,
    }]

    _FORMATS_MAP = {
        'watermarked_video_sd_url': ('sd-wmk', 'SD, watermarked'),
        'video_sd_url': ('sd', None),
        'watermarked_video_hd_url': ('hd-wmk', 'HD, watermarked'),
        'video_hd_url': ('hd', None),
    }

    def _extract_formats(self, video_dict):
        formats = []
        for format_key, format_url in traverse_obj(video_dict, (
            {dict.items}, lambda _, v: v[0] in self._FORMATS_MAP and url_or_none(v[1]),
        )):
            formats.append({
                'format_id': self._FORMATS_MAP[format_key][0],
                'format_note': self._FORMATS_MAP[format_key][1],
                'url': format_url,
                'ext': 'mp4',
                'quality': qualities(tuple(self._FORMATS_MAP))(format_key),