diff --git a/database.py b/database.py index 61f9362..880d46b 100644 --- a/database.py +++ b/database.py @@ -610,6 +610,8 @@ def db_query(artists=None,title=None,track=None,since=None,to=None,associated=Fa # if a title is specified, we assume that a specific track (with the exact artist combination) is requested # if not, duplicate artist arguments are ignored + artist = None + # artists to numbers artists = set([(ARTISTS.index(a) if isinstance(a,str) else a) for a in artists]) @@ -622,8 +624,6 @@ def db_query(artists=None,title=None,track=None,since=None,to=None,associated=Fa # we only need one artist elif track==None and len(artists) != 0: artist = artists.pop() - else: - artist = None # right now we always request everything by name, maybe we don't actually need the request by number, but i'll leave it in for now diff --git a/htmlgenerators.py b/htmlgenerators.py index c8d819c..64cc6f5 100644 --- a/htmlgenerators.py +++ b/htmlgenerators.py @@ -4,6 +4,28 @@ def artistLink(name): import urllib return "" + name + "" + +def artistLinks(artists): + return ", ".join([artistLink(a) for a in artists]) + +#def trackLink(artists,title): +def trackLink(track): + artists,title = track["artists"],track["title"] + import urllib + return "" + title + "" + +#def scrobblesTrackLink(artists,title,timekeys,amount=None,pixels=None): +def scrobblesTrackLink(track,timekeys,amount=None,pixels=None): + artists,title = track["artists"],track["title"] + import urllib + inner = str(amount) if amount is not None else "
" + return "" + inner + "" + +def scrobblesArtistLink(artist,timekeys,amount=None,pixels=None,associated=False): + import urllib + inner = str(amount) if amount is not None else "
" + askey = "&associated" if associated else "" + return "" + inner + "" # necessary because urllib.parse.urlencode doesnt handle multidicts def keysToUrl(*dicts): @@ -12,7 +34,7 @@ def keysToUrl(*dicts): keys = removeIdentical(*dicts) for k in keys: values = keys.getall(k) - st += "&".join([urllib.parse.urlencode({k:v}) for v in values]) + st += "&".join([urllib.parse.urlencode({k:v},safe="/") for v in values]) st += "&" return st @@ -46,15 +68,13 @@ def getTimeDesc(timestamp): # limit a multidict to only the specified keys # would be a simple constructor expression, but multidicts apparently don't let me do that -# hardcoding this to only allow multi values for a key in one case: artist when there is also a title specified def pickKeys(d,*keys): from bottle import FormsDict - if isinstance(d,dict) or not "title" in d: + if isinstance(d,dict): return {k:d.get(k) for k in d if k in keys} else: # create a normal dictionary of lists - newd = {k:d.getall(k) for k in d if k in keys and k=="artist"} - newd2 = {k:[d.get(k)] for k in d if k in keys and k!="artist"} + newd = {k:d.getall(k) for k in d if k in keys} # one by one add the list entries to the formsdict finald = FormsDict() for k in newd: @@ -62,3 +82,13 @@ def pickKeys(d,*keys): finald.append(k,v) return finald + +# removes all duplicate keys, except artists when a title is specified +def clean(d): + from bottle import FormsDict + if isinstance(d,dict): + return + else: + for k in d: + if (k != "artist") or "title" not in d: + d[k] = d.pop(k) diff --git a/website/artist.py b/website/artist.py index c7189a3..08f5aa8 100644 --- a/website/artist.py +++ b/website/artist.py @@ -3,7 +3,8 @@ import json def replacedict(keys,dbport): - from utilities import getArtistInfo, artistLink + from utilities import getArtistInfo + from htmlgenerators import artistLink info = getArtistInfo(keys["artist"]) diff --git a/website/scrobbles.py b/website/scrobbles.py index 614dece..20d154d 100644 --- a/website/scrobbles.py +++ b/website/scrobbles.py @@ -4,13 +4,13 @@ import json def replacedict(keys,dbport): from utilities import getArtistInfo - from htmlgenerators import getTimeDesc, artistLink, keysToUrl, pickKeys + from htmlgenerators import getTimeDesc, artistLink, artistLinks, trackLink, keysToUrl, pickKeys, clean + clean(keys) timekeys = pickKeys(keys,"since","to","in") - limitkeys = pickKeys(keys,"artist","title") + limitkeys = pickKeys(keys,"artist","title","associated") limitstring = "" - response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/scrobbles?" + keysToUrl(limitkeys,timekeys)) db_data = json.loads(response.read()) scrobbles = db_data["list"] @@ -38,16 +38,10 @@ def replacedict(keys,dbport): html = "" for s in scrobbles: - html += "" + html += "" + html += "" + html += "" + html += "" html += "
" - timestring = getTimeDesc(s["time"]) - html += timestring - html += "" - html += ", ".join([artistLink(a) for a in s["artists"]]) - #artisthtml = "" - #for a in s["artists"]: - # artisthtml += "" + a + ", " - #html += artisthtml[:-2] - html += "" + s["title"] + "
" + getTimeDesc(s["time"]) + "" + artistLinks(s["artists"]) + "" + trackLink({"artists":s["artists"],"title":s["title"]}) + "
" return {"KEY_SCROBBLELIST":html,"KEY_SCROBBLES":str(len(scrobbles)),"KEY_IMAGEURL":imgurl,"KEY_LIMITS":limitstring} diff --git a/website/topartists.py b/website/topartists.py index 50289d1..03f951a 100644 --- a/website/topartists.py +++ b/website/topartists.py @@ -4,8 +4,9 @@ import json def replacedict(keys,dbport): from utilities import getArtistInfo - from htmlgenerators import artistLink, keysToUrl, pickKeys + from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesArtistLink, keysToUrl, pickKeys, clean + clean(keys) timekeys = pickKeys(keys,"since","to","in") limitkeys = pickKeys(keys) @@ -30,13 +31,13 @@ def replacedict(keys,dbport): html = "" for e in charts: html += "" - html += "" + html += "" - html += "" + html += "" + html += "" + html += "" html += "" i += 1 html += "
#" + str(i) + "" - #html += "" + e["artist"] + "" - html += artistLink(e["artist"]) + html += "#" + str(i) + "" + artistLink(e["artist"]) if (e["counting"] != []): html += " incl. " + ", ".join([artistLink(a) for a in e["counting"]]) + "" - html += "" + str(e["scrobbles"]) + "
" + scrobblesArtistLink(e["artist"],timekeys,amount=e["scrobbles"],associated=True) + "" + scrobblesArtistLink(e["artist"],timekeys,pixels=e["scrobbles"]*100/maxbar,associated=True) + "
" diff --git a/website/toptracks.py b/website/toptracks.py index d672ec8..b8337bd 100644 --- a/website/toptracks.py +++ b/website/toptracks.py @@ -4,8 +4,9 @@ import json def replacedict(keys,dbport): from utilities import getArtistInfo - from htmlgenerators import artistLink, keysToUrl, pickKeys + from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesTrackLink, keysToUrl, pickKeys, clean + clean(keys) timekeys = pickKeys(keys,"since","to","in") limitkeys = pickKeys(keys,"artist") @@ -37,12 +38,11 @@ def replacedict(keys,dbport): html = "" for e in charts: html += "" - html += "" - html += "" + html += "" + html += "" + html += "" + html += "" + html += "" html += "" i += 1 html += "
#" + str(i) + "" - html += ", ".join([artistLink(a) for a in e["track"]["artists"]]) - html += "" + e["track"]["title"] - html += "" + str(e["scrobbles"]) + "
" - html += "
#" + str(i) + "" + artistLinks(e["track"]["artists"]) + "" + trackLink(e["track"]) + "" + scrobblesTrackLink(e["track"],timekeys,amount=e["scrobbles"]) + "" + scrobblesTrackLink(e["track"],timekeys,pixels=e["scrobbles"]*100/maxbar) + "
"