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

Code before:

    def _get_claims_token(self, email, password):
        if not self.claims_token_valid():
            self._claims_token = self._new_claims_token(email, password)
            self.cache.store(self._NETRC_MACHINE, 'claims_token', self._claims_token)
        return self._claims_token

    def _real_initialize(self):
        if self.claims_token_valid():
            return
        self._claims_token = self.cache.load(self._NETRC_MACHINE, 'claims_token')

    def _real_extract(self, url):
        video_id = self._match_id(url)
        video_info = self._download_json(
            f'https://services.radio-canada.ca/ott/cbc-api/v2/assets/{video_id}',
            video_id, expected_status=426)

        email, password = self._get_login_info()
        if email and password:
            claims_token = self._get_claims_token(email, password)
            headers = {'x-claims-token': claims_token}
        else:
            headers = {}
        m3u8_info = self._download_json(video_info['playSession']['url'], video_id, headers=headers)
        m3u8_url = re.sub(r'(manifestType=)[\w_-]+(&)', r'\1\2', m3u8_info.get('url'))

        if m3u8_info.get('errorCode') == 1:
            self.raise_geo_restricted(countries=['CA'])
        elif m3u8_info.get('errorCode') == 35:
            self.raise_login_required(method='password')
        elif m3u8_info.get('errorCode') != 0:
            raise ExtractorError(f'{self.IE_NAME} said: {m3u8_info.get("errorCode")} - {m3u8_info.get("message")}')

        formats = self._extract_m3u8_formats(m3u8_url, video_id, m3u8_id='hls')
        self._remove_duplicate_formats(formats)

        for fmt in formats:
            if fmt.get('vcodec') == 'none':
                if fmt.get('ext') is None:
                    fmt['ext'] = 'm4a'
                if fmt.get('acodec') is None:
                    fmt['acodec'] = 'mp4a.40.2'

                # Put described audio at the beginning of the list, so that it
                # isn't chosen by default, as most people won't want it.
                if 'descriptive' in fmt['format_id'].lower():
                    fmt['preference'] = -2

        return {
            'id': video_id,

Review comment (suggestion):
this line should be moved below the error handling
```suggestion
        m3u8_url = update_url_query(m3u8_info['url'], {'manifestType': ''})
```

Code after:
        return exp - time.time() < 10

    def claims_token_valid(self):
        return self._claims_token is not None and not self.claims_token_expired()

    def _get_claims_token(self, email, password):
        if not self.claims_token_valid():
            self._claims_token = self._new_claims_token(email, password)
            self.cache.store(self._NETRC_MACHINE, 'claims_token', self._claims_token)
        return self._claims_token

    def _real_initialize(self):
        if self.claims_token_valid():
            return
        self._claims_token = self.cache.load(self._NETRC_MACHINE, 'claims_token')

    def _real_extract(self, url):
        video_id = self._match_id(url)
        video_info = self._download_json(
            f'https://services.radio-canada.ca/ott/cbc-api/v2/assets/{video_id}',
            video_id, expected_status=426)

        email, password = self._get_login_info()
        if email and password:
            claims_token = self._get_claims_token(email, password)
            headers = {'x-claims-token': claims_token}
        else:
            headers = {}
        m3u8_info = self._download_json(video_info['playSession']['url'], video_id, headers=headers)

        if m3u8_info.get('errorCode') == 1:
            self.raise_geo_restricted(countries=['CA'])
        elif m3u8_info.get('errorCode') == 35:
            self.raise_login_required(method='password')
        elif m3u8_info.get('errorCode') != 0:
            raise ExtractorError(f'{self.IE_NAME} said: {m3u8_info.get("errorCode")} - {m3u8_info.get("message")}')

        formats = self._extract_m3u8_formats(
            m3u8_info['url'], video_id, 'mp4', m3u8_id='hls', query={'manifestType': ''})
        self._remove_duplicate_formats(formats)

        for fmt in formats:
            if fmt.get('vcodec') == 'none':
                if fmt.get('ext') is None:
                    fmt['ext'] = 'm4a'
                if fmt.get('acodec') is None:
                    fmt['acodec'] = 'mp4a.40.2'

                # Put described audio at the beginning of the list, so that it
                # isn't chosen by default, as most people won't want it.
                if 'descriptive' in fmt['format_id'].lower():