Upgraded URI system to be more explicit and allow for multiple entities

This commit is contained in:
krateng 2023-10-28 13:13:03 +02:00
parent 5524ee5aef
commit a47c0da55f

View File

@ -4,7 +4,7 @@ import urllib
import math import math
# this also sets defaults! # this also sets defaults!
def uri_to_internal(keys,forceTrack=False,forceArtist=False,forceAlbum=False,api=False): def uri_to_internal(keys,accepted_entities=('artist','track','album'),forceTrack=False,forceArtist=False,forceAlbum=False,api=False):
# output: # output:
# 1 keys that define the filtered object like artist or track # 1 keys that define the filtered object like artist or track
@ -15,33 +15,30 @@ def uri_to_internal(keys,forceTrack=False,forceArtist=False,forceAlbum=False,api
# if we force a type, that only means that the other types are not allowed # if we force a type, that only means that the other types are not allowed
# it could still have no type at all (any call that isn't filtering by entity) # it could still have no type at all (any call that isn't filtering by entity)
type = None if forceTrack: accepted_entities = ('track',)
if forceTrack and "title" in keys: type = "track" if forceArtist: accepted_entities = ('artist',)
if forceArtist and "artist" in keys: type = "artist" if forceAlbum: accepted_entities = ('album',)
if forceAlbum and "albumtitle" in keys: type = "album"
if (not forceTrack) and (not forceAlbum) and (not forceArtist) and (not type): # API backwards compatibility
if "title" in keys: type = "track" if "artist" in keys and "artist" not in accepted_entities:
elif "albumtitle" in keys: type = "album" if "track" in accepted_entities:
elif "artist" in keys: type = "artist" keys['trackartist'] = keys['artist']
elif "album" in accepted_entities:
keys['albumartist'] = keys['artist']
# 1 # 1
if type == "track": filterkeys = {}
filterkeys = {"track":{"artists":keys.getall("artist"),"title":keys.get("title")}} if "track" in accepted_entities and "title" in keys:
elif type == "artist": filterkeys.update({"track":{"artists":keys.getall("trackartist"),"title":keys.get("title")}})
filterkeys = {"artist":keys.get("artist")} if "artist" in accepted_entities and "artist" in keys:
filterkeys["associated"] = (keys.get('associated','no').lower() == 'yes') filterkeys.update({"artist": keys.get("artist"), "associated": (keys.get('associated', 'no').lower() == 'yes')})
# associated is only used for filtering by artist, to indicate that we include associated artists # associated is only used for filtering by artist, to indicate that we include associated artists
# for actual artist charts, to show that we want to count them, use 'unified' # for actual artist charts, to show that we want to count them, use 'unified'
elif type == "album": if "album" in accepted_entities and "albumtitle" in keys:
filterkeys = {"album":{"artists":keys.getall("artist"),"albumtitle":keys.get("albumtitle") or keys.get("title")}} filterkeys.update({"album":{"artists":keys.getall("albumartist"),"albumtitle":keys.get("albumtitle")}})
else:
filterkeys = {}
# this can be the case regardless of actual entity filter
# e.g if i get scrobbles of an artist associated tells me i want all scrobbles by associated artists
# but seeing the artist charts (wich have no filterkeys) also is affected by this
# 2 # 2
limitkeys = {} limitkeys = {}
@ -107,13 +104,13 @@ def internal_to_uri(keys):
if "artist" in keys: if "artist" in keys:
urikeys.append("artist",keys["artist"]) urikeys.append("artist",keys["artist"])
if keys.get("associated"): urikeys.append("associated","yes") if keys.get("associated"): urikeys.append("associated","yes")
elif "track" in keys: if "track" in keys:
for a in keys["track"]["artists"]: for a in keys["track"]["artists"]:
urikeys.append("artist",a) urikeys.append("trackartist",a)
urikeys.append("title",keys["track"]["title"]) urikeys.append("title",keys["track"]["title"])
elif "album" in keys: if "album" in keys:
for a in keys["album"].get("artists") or []: for a in keys["album"].get("artists") or []:
urikeys.append("artist",a) urikeys.append("albumartist",a)
urikeys.append("albumtitle",keys["album"]["albumtitle"]) urikeys.append("albumtitle",keys["album"]["albumtitle"])