Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: assume RaBe time for archiv requests #328

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# RaBe cridlib for Python

Check failure on line 1 in README.md

View workflow job for this annotation

GitHub Actions / pytest / Test python 3.11

[doctest] README.md

Check failure on line 1 in README.md

View workflow job for this annotation

GitHub Actions / pytest / Test python 3.x

[doctest] README.md

Generate [RaBe CRIDs](https://github.com/radiorabe/crid-spec) based on several data sources:

Expand All @@ -23,7 +23,7 @@
>>> # parse an existing crid
>>> crid = cridlib.parse("crid://rabe.ch/v1/klangbecken#t=clock=19930301T131200.00Z")
>>> print(f"version: {crid.version}, show: {crid.show}, start: {crid.start}")
version: v1, show: klangbecken, start: 1993-03-01 13:12:00
version: v1, show: klangbecken, start: 1993-03-01 12:12:00+00:00

>>> # get crid for current show
>>> crid = cridlib.get()
Expand Down
4 changes: 2 additions & 2 deletions cridlib/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from datetime import datetime, timezone
from datetime import UTC, datetime, timezone

from .lib import CRID, canonicalize_show
from .strategy import future, now, past
Expand Down Expand Up @@ -48,7 +48,7 @@ def get(timestamp: datetime | None = None, fragment: str = "") -> CRID:
if _show:
_show = canonicalize_show(_show)

_tscode = f"t=clock={_ts.strftime('%Y%m%dT%H%M%S.%f')[:-4]}Z"
_tscode = f"t=clock={_ts.astimezone(UTC).strftime('%Y%m%dT%H%M%S.%f')[:-4]}Z"
_fragment = f"{_tscode}{'&' + fragment if fragment else ''}"

return CRID(f"crid://rabe.ch/v1{'/' + _show if _show else ''}#{_fragment}")
6 changes: 3 additions & 3 deletions cridlib/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from datetime import datetime
from datetime import UTC, datetime
from pathlib import PurePath
from typing import Self
from urllib.parse import parse_qs
Expand Down Expand Up @@ -104,10 +104,10 @@ def __init__(self: Self, uri: str | None = None) -> None:
try:
# TODO(hairmare): investigate noqa for bug
# https://github.com/radiorabe/python-rabe-cridlib/issues/244
self._start = datetime.strptime( # noqa: DTZ007
self._start = datetime.strptime(
parse_qs(parse_qs(self.fragment)["t"][0])["clock"][0],
"%Y%m%dT%H%M%S.%fZ",
)
).astimezone(UTC)
except KeyError as ex:
raise CRIDMissingMediaFragmentError(self.fragment, uri) from ex
except ValueError as ex: # pragma: no cover
Expand Down
9 changes: 8 additions & 1 deletion cridlib/strategy/past.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

from datetime import datetime

from pytz import timezone

from cridlib.util import get_session

__ARCHIV_BROADCASTS_URL = "https://archiv.rabe.ch/api/broadcasts/"
__ARCHIV_TIMEZONE = "Europe/Zurich"


def get_show(past: datetime) -> str:
Expand All @@ -21,7 +24,11 @@ def get_show(past: datetime) -> str:
Show name from the archive for `past`.

"""
_url = f"{__ARCHIV_BROADCASTS_URL}{past.year}/{past.month:02d}/{past.day:02d}/{past.hour:02d}{past.minute:02d}{past.second:02d}" # noqa: E501
local = past
if past.tzname != __ARCHIV_TIMEZONE:
local = past.astimezone(timezone(__ARCHIV_TIMEZONE))

_url = f"{__ARCHIV_BROADCASTS_URL}{local.year}/{local.month:02d}/{local.day:02d}/{local.hour:02d}{local.minute:02d}{local.second:02d}" # noqa: E501
_resp = get_session().get(_url, timeout=10)
_json = _resp.json()
_data = _json.get("data")
Expand Down
8 changes: 4 additions & 4 deletions tests/test_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ def test_get_past(archiv_mock): # noqa: ARG001
"""Test meth:`get` for past shows."""
with freeze_time("1993-03-02 00:00:00 UTC"):
crid = cridlib.get(
timestamp=datetime(1993, 3, 1, 13, 12, 00, tzinfo=timezone.utc),
timestamp=datetime(1993, 3, 1, 12, 12, 00, tzinfo=timezone.utc),
)
assert crid.version == "v1"
assert crid.show == "test"
assert str(crid) == "crid://rabe.ch/v1/test#t=clock=19930301T131200.00Z"
assert str(crid) == "crid://rabe.ch/v1/test#t=clock=19930301T121200.00Z"

# show with additional local args in fragments
with freeze_time("1993-03-02 00:00:00 UTC"):
crid = cridlib.get(
timestamp=datetime(1993, 3, 1, 13, 12, 00, tzinfo=timezone.utc),
timestamp=datetime(1993, 3, 1, 12, 12, 00, tzinfo=timezone.utc),
fragment="myid=1234",
)
assert crid.version == "v1"
assert crid.show == "test"
assert str(crid) == "crid://rabe.ch/v1/test#t=clock=19930301T131200.00Z&myid=1234"
assert str(crid) == "crid://rabe.ch/v1/test#t=clock=19930301T121200.00Z&myid=1234"


def test_get_future(libretime_mock): # noqa: ARG001
Expand Down
8 changes: 4 additions & 4 deletions tests/test_lib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Test high level cridlib API."""

from datetime import datetime
from datetime import UTC, datetime

import pytest
from freezegun import freeze_time
Expand All @@ -16,7 +16,7 @@
{
"version": "v1",
"show": "test",
"start": datetime(1993, 3, 1, 13, 12),
"start": datetime(1993, 3, 1, 12, 12, tzinfo=UTC),
},
),
(
Expand All @@ -32,18 +32,18 @@
{
"version": "v1",
"show": None,
"start": datetime(1993, 3, 1, 13, 12),
"start": datetime(1993, 3, 1, 12, 12, tzinfo=UTC),
},
),
],
)
def test_crid_roundtrip(crid_str, expected):
with freeze_time("1993-03-01 13:12"):
with freeze_time("1993-03-01 12:12 UTC"):
crid = cridlib.lib.CRID(crid_str)
assert str(crid) == crid_str
assert crid.version == expected["version"]
assert crid.show == expected["show"]
assert crid.start == expected["start"]

Check failure on line 46 in tests/test_lib.py

View workflow job for this annotation

GitHub Actions / pytest / Test python 3.11

test_crid_roundtrip[crid://rabe.ch/v1/test#t=clock=19930301T131200.00Z-expected0] AssertionError: assert FakeDatetime(1993, 3, 1, 13, 12, tzinfo=datetime.timezone.utc) == datetime.datetime(1993, 3, 1, 12, 12, tzinfo=datetime.timezone.utc) + where FakeDatetime(1993, 3, 1, 13, 12, tzinfo=datetime.timezone.utc) = <class 'cridlib.lib.CRID' for 'crid://rabe.ch/v1/test#t=clock=19930301T131200.00Z'>.start

Check failure on line 46 in tests/test_lib.py

View workflow job for this annotation

GitHub Actions / pytest / Test python 3.11

test_crid_roundtrip[crid://rabe.ch/v1#t=clock=19930301T131200.00Z-expected2] AssertionError: assert FakeDatetime(1993, 3, 1, 13, 12, tzinfo=datetime.timezone.utc) == datetime.datetime(1993, 3, 1, 12, 12, tzinfo=datetime.timezone.utc) + where FakeDatetime(1993, 3, 1, 13, 12, tzinfo=datetime.timezone.utc) = <class 'cridlib.lib.CRID' for 'crid://rabe.ch/v1#t=clock=19930301T131200.00Z'>.start

Check failure on line 46 in tests/test_lib.py

View workflow job for this annotation

GitHub Actions / pytest / Test python 3.x

test_crid_roundtrip[crid://rabe.ch/v1/test#t=clock=19930301T131200.00Z-expected0] AssertionError: assert FakeDatetime(1993, 3, 1, 13, 12, tzinfo=datetime.timezone.utc) == datetime.datetime(1993, 3, 1, 12, 12, tzinfo=datetime.timezone.utc) + where FakeDatetime(1993, 3, 1, 13, 12, tzinfo=datetime.timezone.utc) = <class 'cridlib.lib.CRID' for 'crid://rabe.ch/v1/test#t=clock=19930301T131200.00Z'>.start

Check failure on line 46 in tests/test_lib.py

View workflow job for this annotation

GitHub Actions / pytest / Test python 3.x

test_crid_roundtrip[crid://rabe.ch/v1#t=clock=19930301T131200.00Z-expected2] AssertionError: assert FakeDatetime(1993, 3, 1, 13, 12, tzinfo=datetime.timezone.utc) == datetime.datetime(1993, 3, 1, 12, 12, tzinfo=datetime.timezone.utc) + where FakeDatetime(1993, 3, 1, 13, 12, tzinfo=datetime.timezone.utc) = <class 'cridlib.lib.CRID' for 'crid://rabe.ch/v1#t=clock=19930301T131200.00Z'>.start


def test_crid_scheme_mismatch():
Expand Down
Loading