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

Code before:
                if thumbnail_id not in _QUALITIES:
                    continue
                thumbnails.append({
                    'url': thumbnail_url,
                    'preference': quality_key(thumbnail_id),
                })
        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,
        }

    @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 []]

Review comment (refactor):
> moments seem to be a multipurpose feature (but for now this might be the only type that exists), and I have a feeling that when Twitch modifies this feature, chapter extraction might silently break and I'll never notice.
probably this is silly, but I see it the way as there is a warning too when a description was not found: it hints that something might be off there.

even if they add/change a new moment type, as long as the momentPosition and duration exists, the chapter will be well-formed and it doesnt matter

Code after:
                    continue
                if thumbnail_id not in _QUALITIES:
                    continue
                thumbnails.append({
                    'url': thumbnail_url,
                    'preference': quality_key(thumbnail_id),
                })
        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,