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

Code before:
        'only_matching': True,
    }, {
        'url': 'https://chaturbate.eu/fullvideo/?b=caylin',
        'only_matching': True,
    }, {
        'url': 'https://chaturbate.global/siswet19/',
        'only_matching': True,
    }]

    _ROOM_OFFLINE = 'Room is currently offline'
    _ROOM_PRIVATE = 'Room is currently in a private show'
    _ROOM_AWAY = 'Performer is currently away'
    _ROOM_PASSWORD = 'Room is password protected'
    _ROOM_HIDDEN = 'Hidden session in progress'
    _ROOM_BLOCKED = 'Room is not avaiable in this region'

    def _extract_from_api(self, video_id, tld):
        req = Request(
            f'https://chaturbate.{tld}/get_edge_hls_url_ajax/',
            data=f'room_slug={video_id}'.encode(),
            headers={
                'X-Requested-With': 'XMLHttpRequest',
                'Content-Type': 'application/x-www-form-urlencoded',
            },
        )
        response = self._download_json(req, video_id, fatal=False)

        status = response.get('room_status')
        if status == 'offline':
            raise ExtractorError(self._ROOM_OFFLINE, expected=True)
        elif status == 'private':
            raise ExtractorError(self._ROOM_PRIVATE, expected=True)
        elif status == 'away':
            raise ExtractorError(self._ROOM_AWAY, expected=True)
        elif status == 'hidden':
            raise ExtractorError(self._ROOM_HIDDEN, expected=True)
        elif status == 'password protected':
            raise ExtractorError(self._ROOM_PASSWORD, expected=True)
        elif status != 'public':
            return None

        m3u8_url = response.get('url')
        if not m3u8_url:
            raise ExtractorError(self._ROOM_BLOCKED, expected=True)

        formats = self._extract_m3u8_formats(
            m3u8_url, video_id, ext='mp4',
            fatal=False, live=True)

        return {
            'id': video_id,

Review comment (refactor):
- we can inline all the request params in the `_download_json()` call
- `'Content-Type': 'application/x-www-form-urlencoded'` header is added by default if the request has `data`
- let's future-proof this by adding the `'Accept': 'application/json'` header
- don't forget our `geo_verification_headers`, or else this would be inherently inferior to webpage extraction
- import `urlencode_postdata` from utils
- add `impersonate=True` per https://github.com/yt-dlp/yt-dlp/pull/11555#issuecomment-2479806227
- add `or {}` so that `response.get('room_status')` below is not fatal
```suggestion
        response = self._download_json(
            f'https://chaturbate.{tld}/get_edge_hls_url_ajax/', video_id,
            data=urlencode_postdata({'room_slug': video_id}), headers={
                **self.geo_verification_headers(),
                'Accept': 'application/json',
                'X-Requested-With': 'XMLHttpRequest',
            }, fatal=False, impersonate=True) or {}
```

Code after:
        'only_matching': True,
    }, {
        'url': 'https://chaturbate.eu/fullvideo/?b=caylin',
        'only_matching': True,
    }, {
        'url': 'https://chaturbate.global/siswet19/',
        'only_matching': True,
    }]

    _ERROR_MAP = {
        'offline': 'Room is currently offline',
        'private': 'Room is currently in a private show',
        'away': 'Performer is currently away',
        'password protected': 'Room is password protected',
        'hidden': 'Hidden session in progress',
    }

    def _extract_from_api(self, video_id, tld):
        response = self._download_json(
            f'https://chaturbate.{tld}/get_edge_hls_url_ajax/', video_id,
            data=urlencode_postdata({'room_slug': video_id}),
            headers={
                **self.geo_verification_headers(),
                'X-Requested-With': 'XMLHttpRequest',
                'Accept': 'application/json',
            }, fatal=False, impersonate=True) or {}

        status = response.get('room_status')
        if status != 'public':
            if error := self._ERROR_MAP.get(status):
                raise ExtractorError(error, expected=True)
            self.report_warning('Falling back to webpage extraction')
            return None

        m3u8_url = response.get('url')
        if not m3u8_url:
            self.raise_geo_restricted()

        return {
            'id': video_id,
            'title': video_id,
            'thumbnail': f'https://roomimg.stream.highwebmedia.com/ri/{video_id}.jpg',
            'is_live': True,
            'age_limit': 18,
            'formats': self._extract_m3u8_formats(m3u8_url, video_id, ext='mp4', live=True),
        }

    def _extract_from_webpage(self, video_id, tld):
        webpage = self._download_webpage(
            f'https://chaturbate.{tld}/{video_id}/', video_id,
            headers=self.geo_verification_headers(), impersonate=True)