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

Code before:
        'info_dict': {
            'id': '78493569-9b3b-4a85-a409-ee76e789e25c',
            'ext': 'ismv',
            'title': ' Anomaly Detection & Root cause at Edge',
            'description': 'md5:f8f1ad93d7918649bfb97fa081b03b83',
            'thumbnail': r're:https://mediusdownload.event.microsoft.com/asset.*\.jpg.*',
            'subtitles': 'count:17',
        },
    }, {
        'url': 'https://medius.microsoft.com/Embed/Video?id=0dc69bda-079b-4070-a7db-a8da1a06a9c7',
        'only_matching': True,
    }, {
        'url': 'https://medius.microsoft.com/Embed/video-nc/fe823a91-959c-465b-96d4-8f4db624f72c',
        'only_matching': True,
    }]

    def _extract_subtitle(self, webpage, video_id):
        captions = traverse_obj(
            self._search_json(r'const\s+captionsConfiguration\s*=', webpage, 'captions', video_id, default=None),
            ('languageList', ..., {
                'url': ('src', {url_or_none}),
                'tag': ('srclang', {str}),
                'name': ('kind', {str}),
            })) or traverse_obj(
            re.findall(r'var\s+file\s+=\s+\{[^}]+\'(https://[^\']+\.vtt\?[^\']+)', webpage),
            (lambda _, v: url_or_none(v), {lambda x: {'url': x, 'tag': x.split('.vtt?')[0].split('_')[-1]}}))

        return self._sub_to_dict(captions)

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(f'https://medius.microsoft.com/Embed/video-nc/{video_id}', video_id)

        ism_url = self._search_regex(r'StreamUrl\s*=\s*"([^"]+manifest)"', webpage, 'ism url')

        return {
            'id': video_id,
            'title': self._og_search_title(webpage),
            'description': self._og_search_description(webpage),
            'formats': self._extract_ism(ism_url, video_id),
            'thumbnail': self._og_search_thumbnail(webpage),
            'subtitles': self._extract_subtitle(webpage, video_id),
        }


class MicrosoftLearnPlaylistIE(InfoExtractor):
    _VALID_URL = r'https?://learn\.microsoft\.com/(?:[\w\-]+/)?(?P<type>shows|events)/(?P<id>[\w\-]+)/?(?:[?#]|$)'
    _TESTS = [{
        'url': 'https://learn.microsoft.com/en-us/shows/bash-for-beginners',
        'info_dict': {
            'id': 'bash-for-beginners',

Review comment (suggestion):
This is hard to read IMO.

The filtering is unnecessary for two reasons: 1. we could just do `..., {url_or_none}` instead, and 2. we can already be reasonably sure that it will be a valid url because of the pattern used with `re.findall`. 

We could get by without traversal even:

```suggestion
            })) or [{'url': url, 'tag': url_basename(url).split('.vtt')[0].split('_')[-1]}
                    for url in re.findall(r'var\s+file\s+=\s+\{[^}]+\'(https://[^\']+\.vtt\?[^\']+)', webpage)]
```

Code after:
            'id': '78493569-9b3b-4a85-a409-ee76e789e25c',
            'ext': 'ismv',
            'title': ' Anomaly Detection & Root cause at Edge',
            'description': 'md5:f8f1ad93d7918649bfb97fa081b03b83',
            'thumbnail': r're:https://mediusdownload.event.microsoft.com/asset.*\.jpg.*',
            'subtitles': 'count:17',
        },
    }, {
        'url': 'https://medius.microsoft.com/Embed/Video?id=0dc69bda-079b-4070-a7db-a8da1a06a9c7',
        'only_matching': True,
    }, {
        'url': 'https://medius.microsoft.com/Embed/video-nc/fe823a91-959c-465b-96d4-8f4db624f72c',
        'only_matching': True,
    }]

    def _extract_subtitle(self, webpage, video_id):
        captions = traverse_obj(
            self._search_json(r'const\s+captionsConfiguration\s*=', webpage, 'captions', video_id, default=None),
            ('languageList', lambda _, v: url_or_none(v['src']), {
                'url': 'src',
                'tag': ('srclang', {str}),
                'name': ('kind', {str}),
            })) or [{'url': url, 'tag': url_basename(url).split('.vtt')[0].split('_')[-1]}
                    for url in re.findall(r'var\s+file\s+=\s+\{[^}]+\'(https://[^\']+\.vtt\?[^\']+)', webpage)]

        return self._sub_to_dict(captions)

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(f'https://medius.microsoft.com/Embed/video-nc/{video_id}', video_id)

        return {
            'id': video_id,
            'title': self._og_search_title(webpage),
            'description': self._og_search_description(webpage),
            'formats': self._extract_ism(
                self._search_regex(r'StreamUrl\s*=\s*"([^"]+manifest)"', webpage, 'ism url'), video_id),
            'thumbnail': self._og_search_thumbnail(webpage),
            'subtitles': self._extract_subtitle(webpage, video_id),
        }


class MicrosoftLearnPlaylistIE(InfoExtractor):
    _VALID_URL = r'https?://learn\.microsoft\.com/(?:[\w-]+/)?(?P<type>shows|events)/(?P<id>[\w-]+)/?(?:[?#]|$)'
    _TESTS = [{
        'url': 'https://learn.microsoft.com/en-us/shows/bash-for-beginners',
        'info_dict': {
            'id': 'bash-for-beginners',
            'title': 'Bash for Beginners',
            'description': 'md5:16a91c07222117d1e00912f0dbc02c2c',
        },