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

Code before:
            title += ' / ' + info['subtitle']

        return title

    def get_timestamp(self, subtitle):
        if not subtitle:
            return None
        # Extract timestamp from:
        #  "subtitle": "Osa 53  (05.08.2016 20:00)",
        match = self._search_regex(self.SUBTITLE_DATE_RE, subtitle, 'dateandtime', default=None)
        if not match:
            return None

        # https://stackoverflow.com/a/27914405/2314626
        date = datetime.strptime(match, '%d.%m.%Y %H:%M')
        unixtime = time.mktime(date.timetuple())

        try:
            import pytz
            # the time is always in this timezone
            tz = pytz.timezone('Europe/Tallinn')
            unixtime += tz.utcoffset(date).microseconds
        except ImportError:
            pass

        return unixtime

    def get_formats(self, playlist, video_id):
        formats = []
        session = self.get_session(traverse_obj(playlist, ('data', 'path')), video_id)
        sid = session.get('session')
        for stream in traverse_obj(playlist, ('data', 'streams'), default=[]):
            if not stream.get('file'):
                continue
            formats.append({
                'protocol': 'm3u8',
                'ext': 'mp4',
                'url': url_or_none(stream.get('file') + '&s=' + sid),
            })

        self._sort_formats(formats)

        return formats

    def get_playlist(self, video_id):
        url = 'https://kanal2.postimees.ee/player/playlist/%(video_id)s' % {'video_id': video_id}
        query = {
            'type': 'episodes',
        }
        headers = {
            'X-Requested-With': 'XMLHttpRequest',

Review comment (suggestion):
one-time use function; use `unified_timestamp` instead, could even be inlined in return:
```py
            'timestamp': unified_timestamp(
                self._search_regex(
                    r'\((\d{2}\.\d{2}\.\d{4}\s\d{2}:\d{2})\)$', sub,
                    'dateandtime', default=None))
```

or alternatively, there is an `airdate` string value in the playlist JSON that could be used for `upload_date`

Code after:
from .common import InfoExtractor
from ..utils import (
    ExtractorError,
    join_nonempty,
    traverse_obj,
    unified_timestamp,
    update_url_query,
)


class Kanal2IE(InfoExtractor):
    _VALID_URL = r'https?://kanal2\.postimees\.ee/[^?#]+\?([^#]+&)?id=(?P<id>\d+)'
    _TESTS = [{
        'note': 'Test standard url (#5575)',
        'url': 'https://kanal2.postimees.ee/pluss/video/?id=40792',
        'md5': '7ea7b16266ec1798743777df241883dd',
        'info_dict': {
            'id': '40792',
            'ext': 'mp4',
            'title': 'Aedniku aabits / Osa 53  (05.08.2016 20:00)',
            'thumbnail': r're:https?://.*\.jpg$',
            'description': 'md5:53cabf3c5d73150d594747f727431248',
            'upload_date': '20160805',
            'timestamp': 1470420000,
        },
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        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='') + ' +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