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

Code before:
            'https://ttwid.bytedance.com/ttwid/union/register/', None,
            'Fetching ttwid', 'Unable to fetch ttwid', headers={
                'Content-Type': 'application/json',
            }, data=json.dumps({
                'aid': 24,
                'needFid': False,
                'region': 'cn',
                'service': 'www.toutiao.com',
                'union': True,
            }).encode(),
        )

        if ttwid := self._get_cookies(urlh.url)['ttwid'].value:
            self._set_cookie('.toutiao.com', 'ttwid', ttwid)
            return

        self.raise_login_required(method='cookies')

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)
        video_data = traverse_obj(webpage, (
            {find_element(tag='script', id='RENDER_DATA')},
            {urllib.parse.unquote}, {json.loads}, 'data', 'initialVideo',
        ))

        formats = []
        for video in traverse_obj(video_data, (
            'videoPlayInfo', 'video_list', lambda _, v: v['main_url'],
        )):
            formats.append({
                'url': video['main_url'],
                **traverse_obj(video, ('video_meta', {
                    'acodec': ('audio_profile', {str_or_none}),
                    'asr': ('audio_sample_rate', {int_or_none}),
                    'audio_channels': ('audio_channels', {float_or_none}, {int_or_none}),
                    'ext': ('vtype', {str}),
                    'filesize': ('size', {int_or_none}),
                    'format_id': ('definition', {str}),
                    'fps': ('fps', {int_or_none}),
                    'height': ('vheight', {int_or_none}),
                    'tbr': ('real_bitrate', {float_or_none(scale=1000)}),
                    'vcodec': ('codec_type', {str}),
                    'width': ('vwidth', {int_or_none}),
                })),
            })

        return {
            'id': video_id,
            'formats': formats,
            **traverse_obj(video_data, {

Review comment (security):
I would only use `find_element` (or any of the HTML helpers) when looking for JSON if you're certain that the unescaping of HTML special characters is necessary (e.g. if the JSON is an attribute value). Otherwise it can be potentially harmful (e.g. `&quot;` being unescaped to `"` inside a string value and breaking our JSON decoder) and you should use `_search_json` instead. (I'm guessing that's not the case here since you are needing to `unquote` it here first, but just an FYI)

Code after:
        urlh = self._request_webpage(
            'https://ttwid.bytedance.com/ttwid/union/register/', None,
            'Fetching ttwid', 'Unable to fetch ttwid', headers={
                'Content-Type': 'application/json',
            }, data=json.dumps({
                'aid': 24,
                'needFid': False,
                'region': 'cn',
                'service': 'www.toutiao.com',
                'union': True,
            }).encode(),
        )

        if ttwid := try_call(lambda: self._get_cookies(urlh.url)['ttwid'].value):
            self._set_cookie('.toutiao.com', 'ttwid', ttwid)
            return

        self.raise_login_required()

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)
        video_data = traverse_obj(webpage, (
            {find_element(tag='script', id='RENDER_DATA')},
            {urllib.parse.unquote}, {json.loads}, 'data', 'initialVideo',
        ))

        formats = []
        for video in traverse_obj(video_data, (
            'videoPlayInfo', 'video_list', lambda _, v: v['main_url'],
        )):
            formats.append({
                'url': video['main_url'],
                **traverse_obj(video, ('video_meta', {
                    'acodec': ('audio_profile', {str}),
                    'asr': ('audio_sample_rate', {int_or_none}),
                    'audio_channels': ('audio_channels', {float_or_none}, {int_or_none}),
                    'ext': ('vtype', {str}),
                    'filesize': ('size', {int_or_none}),
                    'format_id': ('definition', {str}),
                    'fps': ('fps', {int_or_none}),
                    'height': ('vheight', {int_or_none}),
                    'tbr': ('real_bitrate', {float_or_none(scale=1000)}),
                    'vcodec': ('codec_type', {str}),
                    'width': ('vwidth', {int_or_none}),
                })),
            })

        return {
            'id': video_id,
            'formats': formats,