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

Code before:
            'view_count': int,
        },
    }]

    def _real_initialize(self):
        if self._get_cookies('https://www.toutiao.com').get('ttwid'):
            return

        _, urlh = self._download_json_handle(
            '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)}),

Review comment (refactor):
`['ttwid'].value` is doubly fatal. If the idea is to `raise_login_required` when the cookie hasn't been set (instead of throwing a `KeyError`/`AttributeError`), then we need to check this safely:
```suggestion
        if ttwid := try_call(lambda: self._get_cookies(urlh.url)['ttwid'].value):
            self._set_cookie('.toutiao.com', 'ttwid', ttwid)
            return

        self.raise_login_required()
```
also `cookies` is the default `method` value when there is no `_NETRC_MACHINE` set for the extractor

Code after:
            'uploader_id': 'MS4wLjABAAAA4f7d4mwtApALtHIiq-QM20dwXqe32NUz0DeWF7wbHKw',
            'view_count': int,
        },
    }]

    def _real_initialize(self):
        if self._get_cookies('https://www.toutiao.com').get('ttwid'):
            return

        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}),