🐛 Fixed matching URLs when using url command (#215)

* refactor `from_string` method to improve resource type extraction logic
This commit is contained in:
Oskar Dudziński
2025-11-12 23:37:41 +01:00
committed by GitHub
parent eb0ae38df8
commit e0fe7e1655
2 changed files with 28 additions and 3 deletions
+17
View File
@@ -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"
+11 -3
View File
@@ -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",