mirror of
https://github.com/oskvr37/tiddl.git
synced 2026-06-13 04:05:08 +03:00
🐛 Fixed matching URLs when using url command (#215)
* refactor `from_string` method to improve resource type extraction logic
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user