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

Code before:
            'series': 'Mighty Ships',
            'season_number': 7,
            'episode_number': 3,
            'episode': 'Wind Surf',
        },
        'params': {
            'format': 'bestvideo',
            'skip_download': True,
        }
    }, {
        'url': 'https://7plus.com.au/UUUU?episode-id=AUMS43-001',
        'only_matching': True,
    }]

    def _real_initialize(self):
        self.token = None

        cookies = self._get_cookies('https://7plus.com.au')
        if not cookies:
            return

        try:
            api_key = next(x for x in cookies if x.startswith('glt_'))[4:]
            login_token = cookies[f'glt_{api_key}'].value
        except StopIteration:
            raise ExtractorError('Unable to extract gigya API key and login token')

        login_resp = self._download_json('https://login.7plus.com.au/accounts.getJWT', None, 'Logging in', query={
            'APIKey': api_key,
            'sdk': 'js_latest',
            'login_token': login_token,
            'authMode': 'cookie',
            'pageURL': 'https://7plus.com.au/',
            'sdkBuild': '12471',
            'format': 'json',
        })

        if 'errorMessage' in login_resp:
            raise ExtractorError(f'7plus returned an error: {login_resp["errorMessage"]}', expected=True)

        try:
            id_token = login_resp['id_token']
        except KeyError:
            raise ExtractorError('Unable to extract id token')

        token_resp = self._download_json('https://7plus.com.au/auth/token', None, 'Getting auth token', data=json.dumps({
            'idToken': id_token,
            'platformId': 'web',
            'regSource': '7plus',
        }).encode('utf-8'), headers={'Content-Type': 'application/json'})


Review comment (suggestion):
cookie based login must never throw error since the cookies may not have been intended to log in to this site

Code after:
            'series': 'Mighty Ships',
            'season_number': 7,
            'episode_number': 3,
            'episode': 'Wind Surf',
        },
        'params': {
            'format': 'bestvideo',
            'skip_download': True,
        }
    }, {
        'url': 'https://7plus.com.au/UUUU?episode-id=AUMS43-001',
        'only_matching': True,
    }]

    def _real_initialize(self):
        self.token = None

        cookies = self._get_cookies('https://7plus.com.au')
        api_key = next((x for x in cookies if x.startswith('glt_')), '')[4:]
        if not api_key:  # Cookies are signed out, skip login
            return

        login_resp = self._download_json(
            'https://login.7plus.com.au/accounts.getJWT', None, 'Logging in', fatal=False,
            query={
                'APIKey': api_key,
                'sdk': 'js_latest',
                'login_token': cookies[f'glt_{api_key}'].value,
                'authMode': 'cookie',
                'pageURL': 'https://7plus.com.au/',
                'sdkBuild': '12471',
                'format': 'json',
            }) or {}

        if 'errorMessage' in login_resp:
            self.report_warning(f'Unable to login: 7plus said: {login_resp["errorMessage"]}')
            return
        id_token = login_resp.get('id_token')
        if not id_token:
            self.report_warning('Unable to login: Could not extract id token')
            return

        token_resp = self._download_json(
            'https://7plus.com.au/auth/token', None, 'Getting auth token', fatal=False,
            headers={'Content-Type': 'application/json'}, data=json.dumps({
                'idToken': id_token,
                'platformId': 'web',
                'regSource': '7plus',
            }).encode('utf-8')) or {}
        self.token = token_resp.get('token')
        if not self.token: