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

Code before:
)


class SharePointIE(InfoExtractor):
    _VALID_URL = r'https?://[^\.]+\.sharepoint\.com/:v:/g/[^/]+/[^/]+/(?P<id>[^/?]+)'

    _TESTS = [
        {
            'url': 'https://lut-my.sharepoint.com/:v:/g/personal/juha_eerola_student_lab_fi/EUrAmrktb4ZMhUcY9J2PqMEBD_9x_l0DyYWVgAvp-TTOMw?e=ZpQOOw',
            'info_dict': {
                'id': 'EUrAmrktb4ZMhUcY9J2PqMEBD_9x_l0DyYWVgAvp-TTOMw',
                'ext': 'mp4',
                'title': 'CmvpJST.mp4',
                'duration': 54.567,
            },
            'params': {'skip_download': 'm3u8'}
        },
    ]

    def _parse_mediapackage(self, video, video_id):
        formats = []

        base_media_url = video['.transformUrl'].replace('/thumbnail?', '/videomanifest?')
        base_media_url += f"&cTag={video['.ctag']}&action=Access&part=index"

        metadata = json.loads(video.get('MediaServiceFastMetadata'))

        # Normally the player is configured with all possible options added to the format URLs,
        # but we always get all formats back no matter what settings you send
        formats.extend(self._extract_mpd_formats(
            f"{base_media_url}&format=dash", video_id, mpd_id='dash', fatal=False
        ))
        formats.extend(self._extract_m3u8_formats(
            f"{base_media_url}&format=hls", video_id, m3u8_id='hls', entry_protocol='m3u8_native',
            fatal=False
        ))
        # HLSv6
        formats.extend(self._extract_m3u8_formats(
            f"{base_media_url}&format=hls-vnext", video_id, m3u8_id='hls-vnext',
            entry_protocol='m3u8_native', fatal=False
        ))

        # Most formats are found multiple times, we remove the occurrences without metadata
        formats = [x for x in formats if x.get('ext') is not None]

        return {
            'id': video_id,
            'formats': formats,
            'title': video.get('name'),
            'duration': float_or_none(traverse_obj(metadata, ('media', 'duration')), scale=10000000),
        }

Review comment (suggestion):
* This is still fatal and gets no benefit from the `.get`. ~~Use `self._parse_json` instead~~ See Grub4k's suggestion below
* Move it below near where it's actually used

Code after:


class SharePointIE(InfoExtractor):
    _BASE_URL_RE = r'https?://[\w-]+\.sharepoint\.com/'
    _VALID_URL = [
        rf'{_BASE_URL_RE}:v:/[a-z]/(?:[^/?#]+/)*(?P<id>[^/?#]{{46}})/?(?:$|[?#])',
        rf'{_BASE_URL_RE}(?!:v:)(?:[^/?#]+/)*stream\.aspx\?(?:[^#]+&)?id=(?P<id>[^&#]+)',
    ]
    _TESTS = [{
        'url': 'https://lut-my.sharepoint.com/:v:/g/personal/juha_eerola_student_lab_fi/EUrAmrktb4ZMhUcY9J2PqMEBD_9x_l0DyYWVgAvp-TTOMw?e=ZpQOOw',
        'md5': '2950821d0d4937a0a76373782093b435',
        'info_dict': {
            'id': '01EQRS7EKKYCNLSLLPQZGIKRYY6SOY7KGB',
            'display_id': 'EUrAmrktb4ZMhUcY9J2PqMEBD_9x_l0DyYWVgAvp-TTOMw',
            'ext': 'mp4',
            'title': 'CmvpJST',
            'duration': 54.567,
            'thumbnail': r're:https://.+/thumbnail',
            'uploader_id': '8dcec565-a956-4b91-95e5-bacfb8bc015f',
        },
    }, {
        'url': 'https://greaternyace.sharepoint.com/:v:/s/acementornydrive/ETski5eAfNVEoPRZUAyy1wEBpLgVFYWso5bjbZjfBLlPUg?e=PQUfVb',
        'md5': 'c496a01644223273bff12e93e501afd1',
        'info_dict': {
            'id': '01QI4AVTZ3ESFZPAD42VCKB5CZKAGLFVYB',
            'display_id': 'ETski5eAfNVEoPRZUAyy1wEBpLgVFYWso5bjbZjfBLlPUg',
            'ext': 'mp4',
            'title': '930103681233985536',
            'duration': 3797.326,
            'thumbnail': r're:https://.+/thumbnail',
        },
    }, {
        'url': 'https://lut-my.sharepoint.com/personal/juha_eerola_student_lab_fi/_layouts/15/stream.aspx?id=%2Fpersonal%2Fjuha_eerola_student_lab_fi%2FDocuments%2FM-DL%2FCmvpJST.mp4&ga=1&referrer=StreamWebApp.Web&referrerScenario=AddressBarCopied.view',
        'info_dict': {
            'id': '01EQRS7EKKYCNLSLLPQZGIKRYY6SOY7KGB',
            'display_id': '/personal/juha_eerola_student_lab_fi/Documents/M-DL/CmvpJST.mp4',
            'ext': 'mp4',
            'title': 'CmvpJST',
            'duration': 54.567,
            'thumbnail': r're:https://.+/thumbnail',
            'uploader_id': '8dcec565-a956-4b91-95e5-bacfb8bc015f',
        },
        'skip': 'Session cookies needed',
    }, {
        'url': 'https://izoobasisschool.sharepoint.com/:v:/g/Eaqleq8COVBIvIPvod0U27oBypC6aWOkk8ptuDpmJ6arHw',
        'only_matching': True,
    }, {
        'url': 'https://uskudaredutr-my.sharepoint.com/:v:/g/personal/songul_turkaydin_uskudar_edu_tr/EbTf-VRUIbtGuIN73tx1MuwBCHBOmNcWNqSLw61Fd2_o0g?e=n5Vkof',
        'only_matching': True,
    }, {
        'url': 'https://epam-my.sharepoint.com/:v:/p/dzmitry_tamashevich/Ec4ZOs-rATZHjFYZWVxjczEB649FCoYFKDV_x3RxZiWAGA?e=4hswgA',