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

Code before:
                    'password': password,
                },
            }).encode(), expected_status=401)

        if sign_in.get('error'):
            raise ExtractorError('Invalid username or password', expected=True)

    def _real_extract(self, url):
        program_id = self._match_id(url)
        try:
            programs = self._download_json(
                f'{self._BASE_URL}/web_api/programs/{program_id}', program_id)
        except ExtractorError as e:
            if isinstance(e.cause, HTTPError) and e.cause.status == 404:
                raise ExtractorError('Invalid URL', expected=True)
            raise

        enc_id = lambda p: base64.b64encode(str(p['id']).encode()).decode()
        query = {k: v[0] for k, v in parse_qs(url).items() if v}
        if 'c' not in query:
            entries = [self.url_result(
                f'{url}?c={enc_id(program)}', OnsenIE,
            ) for program in traverse_obj(programs, ('contents', lambda _, v: v['id']))]

            return self.playlist_result(
                entries, program_id, programs['program_info']['title'])

        raw_id = base64.b64decode(query['c'] + '=' * (-len(query['c']) & 3)).decode()
        p_keys = ('contents', lambda _, v: v['id'] == int(raw_id))

        program = traverse_obj(programs, (*p_keys, any))
        if not program:
            raise ExtractorError(
                'This program is no longer available', expected=True)
        m3u8_url = traverse_obj(program, ('streaming_url', {url_or_none}))
        if not m3u8_url:
            self.raise_login_required(
                'This program is only available for premium supporters')

        display_id = enc_id(program)
        date_str = self._search_regex(
            rf'{program_id}0?(\d{{6}})', m3u8_url, 'date string', default=None)

        return {
            'display_id': display_id,
            'formats': self._extract_m3u8_formats(m3u8_url, raw_id, headers=self._HEADERS),
            'http_headers': self._HEADERS,
            'section_start': int_or_none(query.get('t', 0)),
            'upload_date': strftime_or_none(f'20{date_str}') if date_str else None,
            'webpage_url': f'{self._BASE_URL}/program/{program_id}?c={display_id}',
            **traverse_obj(program, {

Review comment (refactor):
- Formatting
- I would use `update_url_query` here since the `url` may already have a query with params other than `c`
- playlist title extraction should be non-fatal
```suggestion
            entries = [
                self.url_result(update_url_query(url, {'c': self._get_encoded_id(program)}), OnsenIE)
                for program in traverse_obj(programs, ('contents', lambda _, v: v['id']))
            ]

            return self.playlist_result(
                entries, program_id, traverse_obj(programs, ('program_info', 'title', {str})))
```

Code after:
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            }, data=json.dumps({
                'session': {
                    'email': username,
                    'password': password,
                },
            }).encode(), expected_status=401)

        if sign_in.get('error'):
            raise ExtractorError('Invalid username or password', expected=True)

    def _real_extract(self, url):
        program_id = self._match_id(url)
        try:
            programs = self._download_json(
                f'{self._BASE_URL}/web_api/programs/{program_id}', program_id)
        except ExtractorError as e:
            if isinstance(e.cause, HTTPError) and e.cause.status == 404:
                raise ExtractorError('Invalid URL', expected=True)
            raise

        query = {k: v[-1] for k, v in parse_qs(url).items() if v}
        if 'c' not in query:
            entries = [
                self.url_result(update_url_query(url, {'c': self._get_encoded_id(program)}), OnsenIE)
                for program in traverse_obj(programs, ('contents', lambda _, v: v['id']))
            ]

            return self.playlist_result(
                entries, program_id, traverse_obj(programs, ('program_info', 'title', {clean_html})))

        raw_id = base64.urlsafe_b64decode(f'{query["c"]}===').decode()
        p_keys = ('contents', lambda _, v: v['id'] == int(raw_id))

        program = traverse_obj(programs, (*p_keys, any))
        if not program:
            raise ExtractorError(
                'This program is no longer available', expected=True)
        m3u8_url = traverse_obj(program, ('streaming_url', {url_or_none}))
        if not m3u8_url:
            self.raise_login_required(
                'This program is only available for premium supporters')

        display_id = self._get_encoded_id(program)
        date_str = self._search_regex(
            rf'{program_id}0?(\d{{6}})', m3u8_url, 'date string', default=None)

        return {
            'display_id': display_id,
            'formats': self._extract_m3u8_formats(m3u8_url, raw_id, headers=self._HEADERS),