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

Code before:

        json = self._search_json(start_pattern=r'window\.loomSSRVideo\s*=', string=webpage, name="Json from Loom Webpage", video_id=video_id)
        videourl = self.fetch_loom_download_url(video_id)
        ext = self._search_regex(r'([a-zA-Z0-9]+)(?=\?)', videourl, 'ext', fatal=False)

        date_string = json.get('createdAt')
        date = datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S.%fZ").strftime("%Y%m%d")

        formats = []
        formats.append({
            'url': videourl,
            'width': traverse_obj(json, ('video_properties', 'width')),
            'height': traverse_obj(json, ('video_properties', 'height')),
            'ext': ext,
            'filesize': traverse_obj(json, ('video_properties', 'byte_size')),
        })

        return {
            'id': video_id,
            'title': json.get('name'),
            'uploader': json.get('owner_full_name'),
            'upload_date': date,
            'formats': formats,
            # 'view_count': json["total_views"], # View Count is always changing so don't know how to test this.
            # TODO more properties (see yt_dlp/extractor/common.py)
        }


Review comment (performance):
- If we are only extracting a single format, the format fields (including `url`) can be returned in the info dict root. This reduces the complexity the extractor code
- we can use one `traverse_obj` call to extract all the values from the `metadata` var
- set syntax in a traversal path (`{example}`) is special. If a function is inside the curly braces, it will be used to transform the value that is returned. If a type is inside the curly braces, a value will only be returned if it is of that type
- we need to import `int_or_none` and `parse_iso8601` from utils
- for the `view_count` in the tests you can just use `int`, e.g.
```py
        'view_count': int,
```
```suggestion
        return {
            'id': video_id,
            'url': self._download_json(
                f'https://www.loom.com/api/campaigns/sessions/{video_id}/transcoded-url',
                video_id, 'Downloading video url',  data=b'')['url']
            **traverse_obj(metadata, {
                'title': ('name', {str}),
                'uploader': ('owner_full_name', {str}),
                'timestamp': ('createdAt', {parse_iso8601}),
                'view_count': ('total_views', {int_or_none}),
                'width': ('video_properties', 'width', {int_or_none}),
                'height': ('video_properties', 'height', {int_or_none}),
                'filesize': ('video_properties', 'byte_size', {int_or_none}),
            }),
        }
```

Code after:
        },
    }, {
        # webm raw-url, mp4 transcoded-url, cdn url == transcoded-url, no subs
        'url': 'https://www.loom.com/share/c43a642f815f4378b6f80a889bb73d8d',
        'md5': '70f529317be8cf880fcc2c649a531900',
        'info_dict': {
            'id': 'c43a642f815f4378b6f80a889bb73d8d',
            'ext': 'webm',
            'title': 'Lilah Nielsen Intro Video',
            'uploader': 'Lilah Nielsen',
            'upload_date': '20200826',
            'timestamp': 1598480716,
            'duration': 20,
        },
    }, {
        # m3u8 raw-url, mp4 transcoded-url, cdn url == raw-url, vtt sub and json subs
        'url': 'https://www.loom.com/share/9458bcbf79784162aa62ffb8dd66201b',
        'md5': '51737ec002969dd28344db4d60b9cbbb',
        'info_dict': {
            'id': '9458bcbf79784162aa62ffb8dd66201b',
            'ext': 'mp4',
            'title': 'Sharing screen with gpt-4',
            'description': 'Sharing screen with GPT 4 vision model and asking questions to guide through blender.',
            'uploader': 'Suneel Matham',
            'chapters': 'count:3',
            'upload_date': '20231109',
            'timestamp': 1699518978,
            'duration': 93,
        },
    }, {
        # mpd raw-url, mp4 transcoded-url, cdn url == raw-url, no subs
        'url': 'https://www.loom.com/share/24351eb8b317420289b158e4b7e96ff2',
        'info_dict': {
            'id': '24351eb8b317420289b158e4b7e96ff2',
            'ext': 'webm',
            'title': 'OMFG clown',
            'description': 'md5:285c5ee9d62aa087b7e3271b08796815',
            'uploader': 'MrPumkin B',
            'upload_date': '20210924',
            'timestamp': 1632519618,
            'duration': 210,
        },
        'params': {'skip_download': 'dash'},
    }, {
        # password-protected
        'url': 'https://www.loom.com/share/50e26e8aeb7940189dff5630f95ce1f4',
        'md5': '5cc7655e7d55d281d203f8ffd14771f7',
        'info_dict': {
            'id': '50e26e8aeb7940189dff5630f95ce1f4',
            'ext': 'mp4',
            'title': 'iOS Mobile Upload',