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

Code before:
        playlist = self._download_json(
            f'https://kanal2.postimees.ee/player/playlist/{video_id}',
            video_id, query={'type': 'episodes'},
            headers={'X-Requested-With': 'XMLHttpRequest'})

        return {
            'id': video_id,
            'title': join_nonempty(*traverse_obj(playlist, ('info', ('title', 'subtitle'))), delim=' / '),
            'description': traverse_obj(playlist, ('info', 'description')),
            'thumbnail': traverse_obj(playlist, ('data', 'image')),
            'formats': self.get_formats(playlist, video_id),
            'timestamp': unified_timestamp(self._search_regex(
                r'\((\d{2}\.\d{2}\.\d{4}\s\d{2}:\d{2})\)$',
                traverse_obj(playlist, ('info', 'subtitle')), 'timestamp', default=None) + ' +0200'),
        }

    def get_formats(self, playlist, video_id):
        session = self._download_json(
            'https://sts.postimees.ee/session/register',
            video_id, note='Creating session', errnote='Error creating session',
            headers={
                'X-Original-URI': traverse_obj(playlist, ('data', 'path')),
                'Accept': 'application/json',
            })
        if session['reason'] != 'OK':
            raise ExtractorError(f'Unable to obtain session - {session["reason"]}')

        formats = []
        for stream in traverse_obj(playlist, ('data', 'streams', ..., 'file')):
            formats.extend(self._extract_m3u8_formats(
                update_url_query(stream, {'s': session['session']}), video_id, 'mp4'))

        return formats


Review comment (refactor):
```suggestion
    def get_formats(self, playlist, video_id):
        path = traverse_obj(playlist, ('data', 'path'))
        if not path:
            raise ExtractorError('Path value not found in playlist JSON response')
        session = self._download_json(
            'https://sts.postimees.ee/session/register',
            video_id, note='Creating session', errnote='Error creating session',
            headers={
                'X-Original-URI': path,
                'Accept': 'application/json',
            })
        if session.get('reason') != 'OK' or not session.get('session'):
            reason = session.get('reason', 'unknown error')
            raise ExtractorError(f'Unable to obtain session: {reason}')
```

Code after:
            video_id, query={'type': 'episodes'},
            headers={'X-Requested-With': 'XMLHttpRequest'})

        return {
            'id': video_id,
            'title': join_nonempty(*traverse_obj(playlist, ('info', ('title', 'subtitle'))), delim=' / '),
            'description': traverse_obj(playlist, ('info', 'description')),
            'thumbnail': traverse_obj(playlist, ('data', 'image')),
            'formats': self.get_formats(playlist, video_id),
            'timestamp': unified_timestamp(self._search_regex(
                r'\((\d{2}\.\d{2}\.\d{4}\s\d{2}:\d{2})\)$',
                traverse_obj(playlist, ('info', 'subtitle')), 'timestamp', default='') + ' +0200'),
        }

    def get_formats(self, playlist, video_id):
        path = traverse_obj(playlist, ('data', 'path'))
        if not path:
            raise ExtractorError('Path value not found in playlist JSON response')
        session = self._download_json(
            'https://sts.postimees.ee/session/register',
            video_id, note='Creating session', errnote='Error creating session',
            headers={
                'X-Original-URI': path,
                'Accept': 'application/json',
            })
        if session.get('reason') != 'OK' or not session.get('session'):
            reason = session.get('reason', 'unknown error')
            raise ExtractorError(f'Unable to obtain session: {reason}')

        formats = []
        for stream in traverse_obj(playlist, ('data', 'streams', ..., 'file')):
            formats.extend(self._extract_m3u8_formats(
                update_url_query(stream, {'s': session['session']}), video_id, 'mp4'))

        return formats