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

Code before:
)


class MicrosoftStreamIE(InfoExtractor):
    IE_NAME = 'microsoftstream'
    IE_DESC = 'Microsoft Stream'
    _VALID_URL = r'https?://(?:web|www|msit)\.microsoftstream\.com/video/(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'

    _TESTS = [{
        'url': 'https://web.microsoftstream.com/video/6e51d928-4f46-4f1c-b141-369925e37b62?list=user&userId=f5491e02-e8fe-4e34-b67c-ec2e79a6ecc0',
        'only_matching': True,
    }, {
        'url': 'https://msit.microsoftstream.com/video/b60f5987-aabd-4e1c-a42f-c559d138f2ca',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)
        if '<title>Microsoft Stream</title>' not in webpage:
            self.raise_login_required(method='cookies')

        access_token = self._html_search_regex(r'"AccessToken":"(.+?)"', webpage, 'access token')
        api_url = self._html_search_regex(r'"ApiGatewayUri":"(.+?)"', webpage, 'api url')

        headers = {'Authorization': 'Bearer ' + access_token}

        video_data = self._download_json(
            '%s/videos/%s' % (api_url, video_id), video_id,
            headers=headers, query={
                '$expand': 'creator,tokens,status,liveEvent,extensions',
                'api-version': '1.4-private'
            })

        video_id = video_data.get('id') or video_id

        thumbnails = []
        for thumbnail_id in ('extraSmall', 'small', 'medium', 'large'):
            thumbnail_url = try_get(video_data, lambda x: x['posterImage'][thumbnail_id]['url'])
            if thumbnail_url is None:
                continue
            thumb = {
                'id': thumbnail_id,
                'url': thumbnail_url,
            }
            thumb_name = thumbnail_url.split('?')[0].split('/')[-1]
            thumb_name = str(compat_b64decode(thumb_name + '=' * (len(thumb_name) % 4)))
            thumb.update(parse_resolution(thumb_name))
            thumbnails.append(thumb)

        formats = []

Review comment (security):
```suggestion
        headers = {'Authorization': f'Bearer {access_token}'}
```

Code after:
    parse_resolution,
    try_get,
    url_basename,
)


class MicrosoftStreamIE(InfoExtractor):
    IE_NAME = 'microsoftstream'
    IE_DESC = 'Microsoft Stream'
    _VALID_URL = r'https?://(?:web|www|msit)\.microsoftstream\.com/video/(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'

    _TESTS = [{
        'url': 'https://web.microsoftstream.com/video/6e51d928-4f46-4f1c-b141-369925e37b62?list=user&userId=f5491e02-e8fe-4e34-b67c-ec2e79a6ecc0',
        'only_matching': True,
    }, {
        'url': 'https://msit.microsoftstream.com/video/b60f5987-aabd-4e1c-a42f-c559d138f2ca',
        'only_matching': True,
    }]

    def _get_all_subtitles(self, api_url, video_id, headers):
        subtitles = {}
        automatic_captions = {}
        text_tracks = self._download_json(
            f'{api_url}/videos/{video_id}/texttracks', video_id,
            note='Downloading subtitles JSON', fatal=False, headers=headers,
            query={'api-version': '1.4-private'}).get('value') or []
        for track in text_tracks:
            if not track.get('language') or not track.get('url'):
                continue
            sub_dict = automatic_captions if track.get('autoGenerated') else subtitles
            sub_dict.setdefault(track['language'], []).append({
                'ext': 'vtt',
                'url': track.get('url')
            })
        return {
            'subtitles': subtitles,
            'automatic_captions': automatic_captions
        }

    def extract_all_subtitles(self, *args, **kwargs):
        if (self.get_param('writesubtitles', False)
                or self.get_param('writeautomaticsub', False)
                or self.get_param('listsubtitles')):
            return self._get_all_subtitles(*args, **kwargs)
        return {}

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)
        if '<title>Microsoft Stream</title>' not in webpage:
            self.raise_login_required(method='cookies')