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

Code before:

        media_key = broadcast['media_key']
        source = self._call_api(
            f'live_video_stream/status/{media_key}', media_key)['source']
        m3u8_url = source.get('noRedirectPlaybackUrl') or source['location']
        if '/live_video_stream/geoblocked/' in m3u8_url:
            self.raise_geo_restricted()
        m3u8_id = urllib.parse.parse_qs(urllib.parse.urlparse(
            m3u8_url).query).get('type', [None])[0]
        state, width, height = self._extract_common_format_info(broadcast)
        info['formats'] = self._extract_pscp_m3u8_formats(
            m3u8_url, broadcast_id, m3u8_id, state, width, height)
        return info


class TwitterSpacesIE(TwitterBaseIE):
    IE_NAME = 'twitter:spaces'
    _VALID_URL = TwitterBaseIE._BASE_REGEX + r'i/spaces/(?P<id>[0-9a-zA-Z]{13})'

    _TESTS = [{
        'url': 'https://twitter.com/i/spaces/1OwxWwQOPlNxQ',
        'info_dict': {
            'id': '1OwxWwQOPlNxQ',
            'ext': 'm4a',
            'title': 'Everybody in: @mtbarra & @elonmusk discuss the future of EV charging',
            'description': 'md5:318cde7a7c2753fa5069cda27a6f2413',
            'live_status': 'was_live',
            'release_date': '20230608',
            'release_timestamp': 1686256230,
            'thumbnail': r're:https?://pbs\.twimg\.com/profile_images/.+',
            'timestamp': 1686254250,
            'upload_date': '20230608',
            'uploader': 'Mary Barra',
            'uploader_id': 'mtbarra',
        },
        'params': {'skip_download': 'm3u8'},
    }, {
        # post_live/TimedOut but downloadable
        'url': 'https://twitter.com/i/spaces/1vAxRAVQWONJl',
        'info_dict': {
            'id': '1vAxRAVQWONJl',
            'ext': 'm4a',
            'title': 'Framing Up FinOps: Billing Tools',
            'description': 'md5:d36a80d8607ce1c3feb0e26604045f0d',
            'uploader': 'Google Cloud',
            'uploader_id': 'googlecloud',
            'live_status': 'post_live',
            'thumbnail': r're:https?://pbs\.twimg\.com/profile_images/.+',
            'timestamp': 1681409554,
            'upload_date': '20230413',
            'release_timestamp': 1681839082,

Review comment (refactor):
since twitter names change, let's avoid using `md5:` values in the descriptions

you can use `re:` if the line gets too long, e.g. `r're:Twitter Space participated by stupid dipshit.{50}'`

Code after:

        media_key = broadcast['media_key']
        source = self._call_api(
            f'live_video_stream/status/{media_key}', media_key)['source']
        m3u8_url = source.get('noRedirectPlaybackUrl') or source['location']
        if '/live_video_stream/geoblocked/' in m3u8_url:
            self.raise_geo_restricted()
        m3u8_id = urllib.parse.parse_qs(urllib.parse.urlparse(
            m3u8_url).query).get('type', [None])[0]
        state, width, height = self._extract_common_format_info(broadcast)
        info['formats'] = self._extract_pscp_m3u8_formats(
            m3u8_url, broadcast_id, m3u8_id, state, width, height)
        return info


class TwitterSpacesIE(TwitterBaseIE):
    IE_NAME = 'twitter:spaces'
    _VALID_URL = TwitterBaseIE._BASE_REGEX + r'i/spaces/(?P<id>[0-9a-zA-Z]{13})'

    _TESTS = [{
        'url': 'https://twitter.com/i/spaces/1OwxWwQOPlNxQ',
        'info_dict': {
            'id': '1OwxWwQOPlNxQ',
            'ext': 'm4a',
            'title': 'Everybody in: @mtbarra & @elonmusk discuss the future of EV charging',
            'description': 'Twitter Space participated by Elon Musk',
            'live_status': 'was_live',
            'release_date': '20230608',
            'release_timestamp': 1686256230,
            'thumbnail': r're:https?://pbs\.twimg\.com/profile_images/.+',
            'timestamp': 1686254250,
            'upload_date': '20230608',
            'uploader': 'Mary Barra',
            'uploader_id': 'mtbarra',
        },
        'params': {'skip_download': 'm3u8'},
    }, {
        # post_live/TimedOut but downloadable
        'url': 'https://twitter.com/i/spaces/1vAxRAVQWONJl',
        'info_dict': {
            'id': '1vAxRAVQWONJl',
            'ext': 'm4a',
            'title': 'Framing Up FinOps: Billing Tools',
            'description': 'Twitter Space participated by rupa, Alfonso Hernandez',
            'uploader': 'Google Cloud',
            'uploader_id': 'googlecloud',
            'live_status': 'post_live',
            'thumbnail': r're:https?://pbs\.twimg\.com/profile_images/.+',
            'timestamp': 1681409554,
            'upload_date': '20230413',
            'release_timestamp': 1681839082,