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

Code before:
            'modified_date': '20240404',
        },
    }, {
        # Episode not accessible through API
        'url': 'https://www.rtp.pt/play/estudoemcasa/p7776/e500050/portugues-1-ano',
        'md5': '57660c0b46db9f22118c52cbd65975e4',
        'info_dict': {
            'id': 'e500050',
            'ext': 'mp4',
            'title': 'Português - 1.º ano',
            'duration': 1669.0,
            'description': 'md5:be68925c81269f8c6886589f25fe83ea',
            'upload_date': '20201020',
            'timestamp': 1603180799,
            'thumbnail': 'https://cdn-images.rtp.pt/EPG/imagens/39482_59449_64850.png?v=3&w=860',
        },
    }]

    _USER_AGENT = 'rtpplay/2.0.66 (pt.rtp.rtpplay; build:2066; iOS 15.8.3) Alamofire/5.9.1'
    _AUTH_TOKEN = None

    def _fetch_auth_token(self):
        if self._AUTH_TOKEN:
            return self._AUTH_TOKEN
        self._AUTH_TOKEN = traverse_obj(self._download_json(Request(
            url='https://rtpplayapi.rtp.pt/play/api/2/token-manager',
            headers={
                'Accept': '*/*',
                'rtp-play-auth': 'RTPPLAY_MOBILE_IOS',
                'rtp-play-auth-hash': 'fac9c328b2f27e26e03d7f8942d66c05b3e59371e16c2a079f5c83cc801bd3ee',
                'rtp-play-auth-timestamp': '2145973229682',
                'User-Agent': self._USER_AGENT,
            }, extensions={'keep_header_casing': True}), None,
            note='Fetching guest auth token', errnote='Could not fetch guest auth token',
            fatal=False), ('token', 'token', {str}))
        return self._AUTH_TOKEN

    @staticmethod
    def _cleanup_media_url(url):
        if urllib.parse.urlparse(url).netloc == 'streaming-ondemand.rtp.pt':
            return None
        return url.replace('/drm-fps/', '/hls/').replace('/drm-dash/', '/dash/')

    def _extract_formats(self, media_urls, episode_id):
        formats = []
        subtitles = {}
        for media_url in set(traverse_obj(media_urls, (..., {url_or_none}, {self._cleanup_media_url}))):
            ext = determine_ext(media_url)
            if ext == 'm3u8':
                fmts, subs = self._extract_m3u8_formats_and_subtitles(
                    media_url, episode_id, m3u8_id='hls', fatal=False)

Review comment (suggestion):
```suggestion
            'https://rtpplayapi.rtp.pt/play/api/2/token-manager',
```

Code after:
            'modified_date': '20240404',
        },
    }, {
        # Episode not accessible through API
        'url': 'https://www.rtp.pt/play/estudoemcasa/p7776/e500050/portugues-1-ano',
        'md5': '57660c0b46db9f22118c52cbd65975e4',
        'info_dict': {
            'id': 'e500050',
            'ext': 'mp4',
            'title': 'Português - 1.º ano',
            'duration': 1669.0,
            'description': 'md5:be68925c81269f8c6886589f25fe83ea',
            'upload_date': '20201020',
            'timestamp': 1603180799,
            'thumbnail': 'https://cdn-images.rtp.pt/EPG/imagens/39482_59449_64850.png?v=3&w=860',
        },
    }]

    _USER_AGENT = 'rtpplay/2.0.66 (pt.rtp.rtpplay; build:2066; iOS 15.8.3) Alamofire/5.9.1'
    _AUTH_TOKEN = None

    def _fetch_auth_token(self):
        if self._AUTH_TOKEN:
            return self._AUTH_TOKEN
        self._AUTH_TOKEN = traverse_obj(self._download_json(Request(
            'https://rtpplayapi.rtp.pt/play/api/2/token-manager',
            headers={
                'Accept': '*/*',
                'rtp-play-auth': 'RTPPLAY_MOBILE_IOS',
                'rtp-play-auth-hash': 'fac9c328b2f27e26e03d7f8942d66c05b3e59371e16c2a079f5c83cc801bd3ee',
                'rtp-play-auth-timestamp': '2145973229682',
                'User-Agent': self._USER_AGENT,
            }, extensions={'keep_header_casing': True}), None,
            note='Fetching guest auth token', errnote='Could not fetch guest auth token',
            fatal=False), ('token', 'token', {str}))
        return self._AUTH_TOKEN

    @staticmethod
    def _cleanup_media_url(url):
        if urllib.parse.urlparse(url).netloc == 'streaming-ondemand.rtp.pt':
            return None
        return url.replace('/drm-fps/', '/hls/').replace('/drm-dash/', '/dash/')

    def _extract_formats(self, media_urls, episode_id):
        formats = []
        subtitles = {}
        for media_url in set(traverse_obj(media_urls, (..., {url_or_none}, {self._cleanup_media_url}))):
            ext = determine_ext(media_url)
            if ext == 'm3u8':
                fmts, subs = self._extract_m3u8_formats_and_subtitles(
                    media_url, episode_id, m3u8_id='hls', fatal=False)