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

Code before:
        'only_matching': True,
    }, {
        'url': 'https://m.twitch.tv/clip/FaintLightGullWholeWheat',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        slug = self._match_id(url)

        clip = self._download_gql(
            slug, [{
                'operationName': 'ShareClipRenderStatus',
                'variables': {'slug': slug},
            }],
            'Downloading clip GraphQL')[0]['data']['clip']

        if not clip:
            raise ExtractorError(
                'This clip is no longer available', expected=True)

        access_query = {
            'sig': clip['playbackAccessToken']['signature'],
            'token': clip['playbackAccessToken']['value'],
        }
        asset_default = traverse_obj(clip, ('assets', 0), {})
        asset_portrait = traverse_obj(clip, ('assets', 1), {})
        game = traverse_obj(clip, ('game', 'displayName'))

        formats = []
        for quality in traverse_obj(asset_default, ('videoQualities'), default=[]):
            if source := traverse_obj(quality, ('sourceURL', {url_or_none})):
                formats.append({
                    'url': update_url_query(source, access_query),
                    'format_id': quality.get('quality'),
                    'height': int_or_none(quality.get('quality')),
                    'fps': float_or_none(quality.get('frameRate')),
                    'aspect_ratio': float_or_none(asset_default.get('aspectRatio')),
                })

        for i, quality in enumerate(traverse_obj(asset_portrait, ('videoQualities'), default=[])):
            if source := traverse_obj(quality, ('sourceURL', {url_or_none})):
                formats.append({
                    'url': update_url_query(source, access_query),
                    'format_id': 'portrait-' + ('best' if i == 0 else quality.get('quality')),
                    'height': int_or_none(quality.get('quality')),
                    'fps': float_or_none(quality.get('frameRate')),
                    'aspect_ratio': float_or_none(asset_portrait.get('aspectRatio')),
                    'quality': -1,
                })

        thumbnails = []

Review comment (suggestion):
safer and clearer:
```suggestion
        asset_default = traverse_obj(clip, ('assets', 0, {dict})) or {}
        asset_portrait = traverse_obj(clip, ('assets', 1, {dict})) or {}
```

Code after:
        'url': 'https://go.twitch.tv/rossbroadcast/clip/ConfidentBraveHumanChefFrank',
        'only_matching': True,
    }, {
        'url': 'https://m.twitch.tv/clip/FaintLightGullWholeWheat',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        slug = self._match_id(url)

        clip = self._download_gql(
            slug, [{
                'operationName': 'ShareClipRenderStatus',
                'variables': {'slug': slug},
            }],
            'Downloading clip GraphQL')[0]['data']['clip']

        if not clip:
            raise ExtractorError(
                'This clip is no longer available', expected=True)

        access_query = {
            'sig': clip['playbackAccessToken']['signature'],
            'token': clip['playbackAccessToken']['value'],
        }
        asset_default = traverse_obj(clip, ('assets', 0, {dict})) or {}
        asset_portrait = traverse_obj(clip, ('assets', 1, {dict})) or {}

        formats = []
        default_aspect_ratio = float_or_none(asset_default.get('aspectRatio'))
        formats.extend(traverse_obj(asset_default, ('videoQualities', lambda _, v: url_or_none(v['sourceURL']), {
            'url': ('sourceURL', {update_url_query(query=access_query)}),
            'format_id': ('quality', {str}),
            'height': ('quality', {int_or_none}),
            'fps': ('frameRate', {float_or_none}),
            'aspect_ratio': {value(default_aspect_ratio)},
        })))
        portrait_aspect_ratio = float_or_none(asset_portrait.get('aspectRatio'))
        for source in traverse_obj(asset_portrait, ('videoQualities', lambda _, v: url_or_none(v['sourceURL']))):
            formats.append({
                'url': update_url_query(source['sourceURL'], access_query),
                'format_id': join_nonempty('portrait', source.get('quality')),
                'height': int_or_none(source.get('quality')),
                'fps': float_or_none(source.get('frameRate')),
                'aspect_ratio': portrait_aspect_ratio,
                'quality': -2,
            })

        thumbnails = []
        thumb_asset_default_url = url_or_none(asset_default.get('thumbnailURL'))
        if thumb_asset_default_url: