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

Code before:
            'timestamp': 1614812942,
            'upload_date': '20210303',
            'thumbnail': r're:^https?://[^?#]+\.jpg\?token=',
            'view_count': int,
            'concurrent_view_count': int,
            'live_status': 'was_live',
        },
    }, {
        'url': 'https://twitter.com/i/broadcasts/1OyKAVQrgzwGb',
        'info_dict': {
            'id': '1OyKAVQrgzwGb',
            'ext': 'mp4',
            'title': 'Starship Flight Test',
            'uploader': 'SpaceX',
            'uploader_id': 'SpaceX',
            'uploader_url': 'https://twitter.com/SpaceX',
            'timestamp': 1681993964,
            'upload_date': '20230420',
            'thumbnail': r're:^https?://[^?#]+\.jpg\?token=',
            'view_count': int,
            'concurrent_view_count': int,
            'live_status': 'was_live',
        },
    }, {
        'url': 'https://x.com/i/events/1910629646300762112',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        broadcast_type, broadcast_id = self._match_valid_url(url).groups()

        if broadcast_type == 'events':
            if b_id := traverse_obj(self._call_api(
                f'live_event/1/{broadcast_id}/timeline.json', broadcast_id,
            ), ('twitter_objects', 'broadcasts', ..., ('id', 'broadcast_id'), {str_or_none}, any)):
                return self.url_result(
                    f'https://x.com/i/broadcasts/{b_id}', TwitterBroadcastIE)
            raise ExtractorError('No broadcast_id found', expected=True)

        broadcast = self._call_api(
            'broadcasts/show.json', broadcast_id,
            {'ids': broadcast_id})['broadcasts'][broadcast_id]
        if not broadcast:
            raise ExtractorError('Broadcast no longer exists', expected=True)
        info = self._parse_broadcast_data(broadcast, broadcast_id)
        info['title'] = broadcast.get('status') or info.get('title')
        info['uploader_id'] = broadcast.get('twitter_username') or info.get('uploader_id')
        info['uploader_url'] = format_field(broadcast, 'twitter_username', 'https://twitter.com/%s', default=None)
        if info['live_status'] == 'is_upcoming':
            return info


Review comment (refactor):
For how long do these links keep working after the event ends? Can we add an actual test instead of just an `only_matching` test (since the extraction is actually different for events)?

Code after:
            'uploader_id': 'SpaceX',
            'uploader_url': 'https://twitter.com/SpaceX',
            'timestamp': 1614812942,
            'upload_date': '20210303',
            'thumbnail': r're:^https?://[^?#]+\.jpg\?token=',
            'view_count': int,
            'concurrent_view_count': int,
            'live_status': 'was_live',
        },
    }, {
        'url': 'https://twitter.com/i/broadcasts/1OyKAVQrgzwGb',
        'info_dict': {
            'id': '1OyKAVQrgzwGb',
            'ext': 'mp4',
            'title': 'Starship Flight Test',
            'display_id': '1OyKAVQrgzwGb',
            'uploader': 'SpaceX',
            'uploader_id': 'SpaceX',
            'uploader_url': 'https://twitter.com/SpaceX',
            'timestamp': 1681993964,
            'upload_date': '20230420',
            'thumbnail': r're:^https?://[^?#]+\.jpg\?token=',
            'view_count': int,
            'concurrent_view_count': int,
            'live_status': 'was_live',
        },
    }, {
        'url': 'https://x.com/i/events/1910629646300762112',
        'info_dict': {
            'id': '1LyxBWDRNqyKN',
            'ext': 'mp4',
            'title': '#ガンニバル ウォッチパーティー',
            'concurrent_view_count': int,
            'display_id': '1910629646300762112',
            'live_status': 'was_live',
            'release_date': '20250423',
            'release_timestamp': 1745409000,
            'tags': ['ガンニバル'],
            'thumbnail': r're:https?://[^?#]+\.jpg\?token=',
            'timestamp': 1745403328,
            'upload_date': '20250423',
            'uploader': 'ディズニープラス公式',
            'uploader_id': 'DisneyPlusJP',
            'uploader_url': 'https://twitter.com/DisneyPlusJP',
            'view_count': int,
        },
    }]

    def _real_extract(self, url):
        broadcast_type, display_id = self._match_valid_url(url).group('type', 'id')