1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Modularizing HTML generation, Part II

This commit is contained in:
Krateng 2018-12-26 19:20:26 +01:00
parent 3cfa8428ff
commit 3868d8532d
6 changed files with 60 additions and 34 deletions

View File

@ -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 a title is specified, we assume that a specific track (with the exact artist combination) is requested
# if not, duplicate artist arguments are ignored # if not, duplicate artist arguments are ignored
artist = None
# artists to numbers # artists to numbers
artists = set([(ARTISTS.index(a) if isinstance(a,str) else a) for a in artists]) 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 # we only need one artist
elif track==None and len(artists) != 0: elif track==None and len(artists) != 0:
artist = artists.pop() 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 # 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

View File

@ -4,6 +4,28 @@
def artistLink(name): def artistLink(name):
import urllib import urllib
return "<a href='/artist?artist=" + urllib.parse.quote(name) + "'>" + name + "</a>" return "<a href='/artist?artist=" + urllib.parse.quote(name) + "'>" + name + "</a>"
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 "<a href='/track?title=" + urllib.parse.quote(title) + "&" + "&".join(["artist=" + urllib.parse.quote(a) for a in artists]) + "'>" + title + "</a>"
#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 "<div style='width:" + str(pixels) + "%;'></div>"
return "<a href='/scrobbles?" + "&".join(["artist=" + urllib.parse.quote(a) for a in artists]) + "&title=" + urllib.parse.quote(title) + "&" + keysToUrl(timekeys) + "'>" + inner + "</a>"
def scrobblesArtistLink(artist,timekeys,amount=None,pixels=None,associated=False):
import urllib
inner = str(amount) if amount is not None else "<div style='width:" + str(pixels) + "%;'></div>"
askey = "&associated" if associated else ""
return "<a href='/scrobbles?artist=" + urllib.parse.quote(artist) + "&" + keysToUrl(timekeys) + askey + "'>" + inner + "</a>"
# necessary because urllib.parse.urlencode doesnt handle multidicts # necessary because urllib.parse.urlencode doesnt handle multidicts
def keysToUrl(*dicts): def keysToUrl(*dicts):
@ -12,7 +34,7 @@ def keysToUrl(*dicts):
keys = removeIdentical(*dicts) keys = removeIdentical(*dicts)
for k in keys: for k in keys:
values = keys.getall(k) 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 += "&" st += "&"
return st return st
@ -46,15 +68,13 @@ def getTimeDesc(timestamp):
# limit a multidict to only the specified keys # limit a multidict to only the specified keys
# would be a simple constructor expression, but multidicts apparently don't let me do that # 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): def pickKeys(d,*keys):
from bottle import FormsDict 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} return {k:d.get(k) for k in d if k in keys}
else: else:
# create a normal dictionary of lists # create a normal dictionary of lists
newd = {k:d.getall(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}
newd2 = {k:[d.get(k)] for k in d if k in keys and k!="artist"}
# one by one add the list entries to the formsdict # one by one add the list entries to the formsdict
finald = FormsDict() finald = FormsDict()
for k in newd: for k in newd:
@ -62,3 +82,13 @@ def pickKeys(d,*keys):
finald.append(k,v) finald.append(k,v)
return finald 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)

View File

@ -3,7 +3,8 @@ import json
def replacedict(keys,dbport): def replacedict(keys,dbport):
from utilities import getArtistInfo, artistLink from utilities import getArtistInfo
from htmlgenerators import artistLink
info = getArtistInfo(keys["artist"]) info = getArtistInfo(keys["artist"])

View File

@ -4,13 +4,13 @@ import json
def replacedict(keys,dbport): def replacedict(keys,dbport):
from utilities import getArtistInfo 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") timekeys = pickKeys(keys,"since","to","in")
limitkeys = pickKeys(keys,"artist","title") limitkeys = pickKeys(keys,"artist","title","associated")
limitstring = "" limitstring = ""
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/scrobbles?" + keysToUrl(limitkeys,timekeys)) response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/scrobbles?" + keysToUrl(limitkeys,timekeys))
db_data = json.loads(response.read()) db_data = json.loads(response.read())
scrobbles = db_data["list"] scrobbles = db_data["list"]
@ -38,16 +38,10 @@ def replacedict(keys,dbport):
html = "<table class='list'>" html = "<table class='list'>"
for s in scrobbles: for s in scrobbles:
html += "<tr><td class='time'>" html += "<tr>"
timestring = getTimeDesc(s["time"]) html += "<td class='time'>" + getTimeDesc(s["time"]) + "</td>"
html += timestring html += "<td class='artists'>" + artistLinks(s["artists"]) + "</td>"
html += "</td><td class='artists'>" html += "<td class='title'>" + trackLink({"artists":s["artists"],"title":s["title"]}) + "</td></tr>"
html += ", ".join([artistLink(a) for a in s["artists"]])
#artisthtml = ""
#for a in s["artists"]:
# artisthtml += "<a href=/artist?artist=" + urllib.parse.quote(a) + ">" + a + "</a>, "
#html += artisthtml[:-2]
html += "</td><td class='title'>" + s["title"] + "</td></tr>"
html += "</table>" html += "</table>"
return {"KEY_SCROBBLELIST":html,"KEY_SCROBBLES":str(len(scrobbles)),"KEY_IMAGEURL":imgurl,"KEY_LIMITS":limitstring} return {"KEY_SCROBBLELIST":html,"KEY_SCROBBLES":str(len(scrobbles)),"KEY_IMAGEURL":imgurl,"KEY_LIMITS":limitstring}

View File

@ -4,8 +4,9 @@ import json
def replacedict(keys,dbport): def replacedict(keys,dbport):
from utilities import getArtistInfo 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") timekeys = pickKeys(keys,"since","to","in")
limitkeys = pickKeys(keys) limitkeys = pickKeys(keys)
@ -30,13 +31,13 @@ def replacedict(keys,dbport):
html = "<table class='list'>" html = "<table class='list'>"
for e in charts: for e in charts:
html += "<tr>" html += "<tr>"
html += "<td class='rank'>#" + str(i) + "</td><td class='artist'>" html += "<td class='rank'>#" + str(i) + "</td>"
#html += "<a href=/artist?artist=" + urllib.parse.quote(e["artist"]) + ">" + e["artist"] + "</a>" html += "<td class='artist'>" + artistLink(e["artist"])
html += artistLink(e["artist"])
if (e["counting"] != []): if (e["counting"] != []):
html += " <span class='extra'>incl. " + ", ".join([artistLink(a) for a in e["counting"]]) + "</span>" html += " <span class='extra'>incl. " + ", ".join([artistLink(a) for a in e["counting"]]) + "</span>"
html += "</td><td class='amount'><a href='/scrobbles?artist=" + urllib.parse.quote(e["artist"]) + "&associated&" + keysToUrl(timekeys) + "'>" + str(e["scrobbles"]) + "</a></td>" html += "</td>"
html += "<td class='bar'><a href='/scrobbles?artist=" + urllib.parse.quote(e["artist"]) + "&associated&" + keysToUrl(timekeys) + "'><div style='width:" + str(e["scrobbles"]/maxbar * 100) + "%;'></div></a></td>" html += "<td class='amount'>" + scrobblesArtistLink(e["artist"],timekeys,amount=e["scrobbles"],associated=True) + "</td>"
html += "<td class='bar'>" + scrobblesArtistLink(e["artist"],timekeys,pixels=e["scrobbles"]*100/maxbar,associated=True) + "</td>"
html += "</tr>" html += "</tr>"
i += 1 i += 1
html += "</table>" html += "</table>"

View File

@ -4,8 +4,9 @@ import json
def replacedict(keys,dbport): def replacedict(keys,dbport):
from utilities import getArtistInfo 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") timekeys = pickKeys(keys,"since","to","in")
limitkeys = pickKeys(keys,"artist") limitkeys = pickKeys(keys,"artist")
@ -37,12 +38,11 @@ def replacedict(keys,dbport):
html = "<table class='list'>" html = "<table class='list'>"
for e in charts: for e in charts:
html += "<tr>" html += "<tr>"
html += "<td class='rank'>#" + str(i) + "</td><td class='artists'>" html += "<td class='rank'>#" + str(i) + "</td>"
html += ", ".join([artistLink(a) for a in e["track"]["artists"]]) html += "<td class='artists'>" + artistLinks(e["track"]["artists"]) + "</td>"
html += "</td><td class='title'>" + e["track"]["title"] html += "<td class='title'>" + trackLink(e["track"]) + "</td>"
html += "</td><td class='amount'><a href='/scrobbles?" + "&".join(["artist=" + urllib.parse.quote(a) for a in e["track"]["artists"]]) + "&title=" + urllib.parse.quote(e["track"]["title"]) + "&" + keysToUrl(timekeys) + "'>" + str(e["scrobbles"]) + "</a></td>" html += "<td class='amount'>" + scrobblesTrackLink(e["track"],timekeys,amount=e["scrobbles"]) + "</td>"
html += "<td class='bar'><a href='/scrobbles?" + "&".join(["artist=" + urllib.parse.quote(a) for a in e["track"]["artists"]]) + "&title=" + urllib.parse.quote(e["track"]["title"]) + "&" + keysToUrl(timekeys) + "'><div style='width:" + str(e["scrobbles"]/maxbar * 100) + "%;'></div></a>" html += "<td class='bar'>" + scrobblesTrackLink(e["track"],timekeys,pixels=e["scrobbles"]*100/maxbar) + "</td>"
html += "</td>"
html += "</tr>" html += "</tr>"
i += 1 i += 1
html += "</table>" html += "</table>"