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

Code before:
            'id': info['_id'],
            'title': info.get('title') or 'Untitled Broadcast',
            'description': info.get('description'),
            'duration': int_or_none(info.get('length')),
            'thumbnails': thumbnails,
            'uploader': info.get('channel', {}).get('display_name'),
            'uploader_id': info.get('channel', {}).get('name'),
            'timestamp': parse_iso8601(info.get('recorded_at')),
            'view_count': int_or_none(info.get('views')),
            'is_live': is_live,
        }

    @staticmethod
    def _extract_info_gql(info, item_id):
        def moment_to_chapter(moment):
            moment = moment.get('node')
            if moment.get('type') != 'GAME_CHANGE':
                # TODO: warning or debug log if found unknown moment type
                return None  # TODO: or empty dict?

            momentPosition = int_or_none(moment.get('positionMilliseconds'), 1000)
            momentDuration = int_or_none(moment.get('durationMilliseconds'), 1000)

            if momentPosition is None or momentDuration is None:
                # TODO: warning or debug log about missing important data
                chapters.clear()
                return None  # TODO: or empty dict?

            return {
                'start_time': momentPosition,
                'end_time': momentPosition + momentDuration,
                'title': str_or_none(moment.get('description'))
            }

        vod_id = info.get('id') or item_id
        # id backward compatibility for download archives
        if vod_id[0] != 'v':
            vod_id = 'v%s' % vod_id
        thumbnail = url_or_none(info.get('previewThumbnailURL'))
        if thumbnail:
            for p in ('width', 'height'):
                thumbnail = thumbnail.replace('{%s}' % p, '0')

        chapters = [moment_to_chapter(moment) for moment in info.get('moments') or []]

        return {
            'id': vod_id,
            'title': info.get('title') or 'Untitled Broadcast',
            'description': info.get('description'),
            'duration': int_or_none(info.get('lengthSeconds')),
            'thumbnail': thumbnail,

Review comment (bug):
1. `chapters` should be `None` (not `[]`) if chapter data extraction fails
2. It is bad practice to clear a nonlocal variable like this, especially in the middle of assigning it

Code after:
        return {
            'id': info['_id'],
            'title': info.get('title') or 'Untitled Broadcast',
            'description': info.get('description'),
            'duration': int_or_none(info.get('length')),
            'thumbnails': thumbnails,
            'uploader': info.get('channel', {}).get('display_name'),
            'uploader_id': info.get('channel', {}).get('name'),
            'timestamp': parse_iso8601(info.get('recorded_at')),
            'view_count': int_or_none(info.get('views')),
            'is_live': is_live,
        }

    def _extract_moments(self, info, item_id):
        for moment in info.get('moments') or []:
            start_time = int_or_none(moment.get('positionMilliseconds'), 1000)
            duration = int_or_none(moment.get('durationMilliseconds'), 1000)
            name = str_or_none(moment.get('description'))

            if start_time is None or duration is None:
                self.report_warning(f'Important chapter information missing for chapter {name}', item_id)
                continue
            yield {
                'start_time': start_time,
                'end_time': start_time + duration,
                'title': name,
            }

    def _extract_info_gql(self, info, item_id):
        vod_id = info.get('id') or item_id
        # id backward compatibility for download archives
        if vod_id[0] != 'v':
            vod_id = 'v%s' % vod_id
        thumbnail = url_or_none(info.get('previewThumbnailURL'))
        if thumbnail:
            for p in ('width', 'height'):
                thumbnail = thumbnail.replace('{%s}' % p, '0')

        return {
            'id': vod_id,
            'title': info.get('title') or 'Untitled Broadcast',
            'description': info.get('description'),
            'duration': int_or_none(info.get('lengthSeconds')),
            'thumbnail': thumbnail,
            'uploader': try_get(info, lambda x: x['owner']['displayName'], compat_str),
            'uploader_id': try_get(info, lambda x: x['owner']['login'], compat_str),
            'timestamp': unified_timestamp(info.get('publishedAt')),
            'view_count': int_or_none(info.get('viewCount')),
            'chapters': list(self._extract_moments(info, item_id)),
        }