From e0fe7e1655b7c1c6f51938ae33fcf9d3a25ec898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Dudzi=C5=84ski?= <56404247+oskvr37@users.noreply.github.com> Date: Wed, 12 Nov 2025 23:37:41 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20matching=20URLs=20when?= =?UTF-8?q?=20using=20`url`=20command=20(#215)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor `from_string` method to improve resource type extraction logic --- tests/cli/test_utils.py | 17 +++++++++++++++++ tiddl/cli/utils/resource.py | 14 +++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/tests/cli/test_utils.py b/tests/cli/test_utils.py index 96b57df..0b40ac5 100644 --- a/tests/cli/test_utils.py +++ b/tests/cli/test_utils.py @@ -58,6 +58,23 @@ def test_from_string_invalid_digit_id( TidalResource.from_string(f"{resource_type}/{invalid_id}") +urls_data = [ + ("https://tidal.com/album/321", "album", "321"), + ("https://tidal.com/album/321/", "album", "321"), + ("https://tidal.com/album/321/u", "album", "321"), + ("https://listen.tidal.com/track/12345", "track", "12345"), + ("https://listen.tidal.com/track/12345/", "track", "12345"), + ("https://listen.tidal.com/track/12345/u", "track", "12345"), +] + + +@pytest.mark.parametrize("url, resource_type, resource_id", urls_data) +def test_url_fromstring(url: str, resource_type: str, resource_id: str): + res = TidalResource.from_string(url) + assert res.type == resource_type + assert res.id == resource_id + + def test_url_property(): res = TidalResource(type="track", id="12345") assert res.url == "https://listen.tidal.com/track/12345" diff --git a/tiddl/cli/utils/resource.py b/tiddl/cli/utils/resource.py index 52af97b..84200cf 100644 --- a/tiddl/cli/utils/resource.py +++ b/tiddl/cli/utils/resource.py @@ -25,12 +25,20 @@ class TidalResource(BaseModel): in the format `resource_type/resource_id` (e.g., `track/12345678`). """ - path = urlparse(string).path - resource_type, resource_id = path.split("/")[-2:] + segments = [seg for seg in urlparse(string).path.split("/") if seg] - if resource_type not in get_args(ResourceTypeLiteral): + resource_type = next( + (seg for seg in segments if seg in get_args(ResourceTypeLiteral)), None + ) + + if not resource_type: raise ValueError(f"Invalid resource type: {resource_type}") + try: + resource_id = segments[segments.index(resource_type) + 1] + except IndexError: + raise ValueError(f"No resource ID found {resource_type=} {string=}") + digit_resource_types: list[ResourceTypeLiteral] = [ "track", "album",