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

Code before:
from .common import InfoExtractor
from .redge import RedCDNLivxIE
from ..utils import (
    clean_html,
    js_to_json,
    strip_or_none,
    traverse_obj,
    update_url_query,
)

from datetime import datetime, timedelta


def is_dst(date):
    year = date.year
    # last sunday of march and october, respectively. might break on switch days.
    dst_start = datetime(year, 3, 31, 2) - timedelta(days=(datetime(year, 3, 31).weekday() + 1) % 7)
    dst_end = datetime(year, 10, 31, 3) - timedelta(days=(datetime(year, 10, 31).weekday() + 1) % 7)
    return dst_start <= date <= dst_end


def rfc3339_to_atende(date):
    date = datetime.fromisoformat(date)
    date = date + timedelta(hours=1 if is_dst(date) else 0)
    return int((date.timestamp() - 978307200) * 1000)


class SejmIE(InfoExtractor):
    _VALID_URL = (
        r'https?://(?:www\.)?sejm\.gov\.pl/[Ss]ejm(?P<term>\d+)\.nsf/transmisje(?:_arch)?\.xsp(?:\?[^#]*)?#(?P<id>[\dA-F]+)',
        r'https?://(?:www\.)?sejm\.gov\.pl/[Ss]ejm(?P<term>\d+)\.nsf/transmisje(?:_arch)?\.xsp\?(?:[^#]+&)?unid=(?P<id>[\dA-F]+)',
        r'https?://sejm-embed\.redcdn\.pl/[Ss]ejm(?P<term>\d+)\.nsf/VideoFrame\.xsp/(?P<id>[\dA-F]+)',
    )
    IE_NAME = 'sejm'

    _TESTS = [{
        # multiple cameras, polish SL iterpreter
        'url': 'https://www.sejm.gov.pl/Sejm10.nsf/transmisje_arch.xsp#6181EF1AD9CEEBB5C1258A6D006452B5',
        'info_dict': {
            'id': '6181EF1AD9CEEBB5C1258A6D006452B5',
            'title': '1. posiedzenie Sejmu X kadencji',
            'duration': 20145,
            'live_status': 'was_live',
            'location': 'Sala Posiedzeń',
        },
        'playlist': [{
            'info_dict': {
                'id': 'ENC01-722340000000-722360145000',
                'ext': 'mp4',
                'duration': 20145,

Review comment (suggestion):
in light of https://github.com/yt-dlp/yt-dlp/pull/8978/commits/f070f2d48e39dd65351307590370941f30cdbe07 we may want to revise this somehow

in discord discussion, somebody suggested `import datetime as dt` and I thought that was a manageable solution/compromise

Code after:
import datetime

from .common import InfoExtractor
from .redge import RedCDNLivxIE
from ..utils import (
    clean_html,
    join_nonempty,
    js_to_json,
    strip_or_none,
    update_url_query,
)
from ..utils.traversal import traverse_obj


def is_dst(date):
    last_march = datetime.datetime(date.year, 3, 31)
    last_october = datetime.datetime(date.year, 10, 31)
    last_sunday_march = last_march - datetime.timedelta(days=last_march.isoweekday() % 7)
    last_sunday_october = last_october - datetime.timedelta(days=last_october.isoweekday() % 7)
    return last_sunday_march.replace(hour=2) <= date <= last_sunday_october.replace(hour=3)


def rfc3339_to_atende(date):
    date = datetime.datetime.fromisoformat(date)
    date = date + datetime.timedelta(hours=1 if is_dst(date) else 0)
    return int((date.timestamp() - 978307200) * 1000)


class SejmIE(InfoExtractor):
    _VALID_URL = (
        r'https?://(?:www\.)?sejm\.gov\.pl/[Ss]ejm(?P<term>\d+)\.nsf/transmisje(?:_arch)?\.xsp(?:\?[^#]*)?#(?P<id>[\dA-F]+)',
        r'https?://(?:www\.)?sejm\.gov\.pl/[Ss]ejm(?P<term>\d+)\.nsf/transmisje(?:_arch)?\.xsp\?(?:[^#]+&)?unid=(?P<id>[\dA-F]+)',
        r'https?://sejm-embed\.redcdn\.pl/[Ss]ejm(?P<term>\d+)\.nsf/VideoFrame\.xsp/(?P<id>[\dA-F]+)',
    )
    IE_NAME = 'sejm'

    _TESTS = [{
        # multiple cameras, polish SL iterpreter
        'url': 'https://www.sejm.gov.pl/Sejm10.nsf/transmisje_arch.xsp#6181EF1AD9CEEBB5C1258A6D006452B5',
        'info_dict': {
            'id': '6181EF1AD9CEEBB5C1258A6D006452B5',
            'title': '1. posiedzenie Sejmu X kadencji',
            'duration': 20145,
            'live_status': 'was_live',
            'location': 'Sala Posiedzeń',
        },
        'playlist': [{
            'info_dict': {
                'id': 'ENC01-722340000000-722360145000',
                'ext': 'mp4',