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

Modularizing HTML generation, Part III

This commit is contained in:
Krateng 2018-12-27 03:09:29 +01:00
parent 3868d8532d
commit 5987997075
6 changed files with 41 additions and 43 deletions

View File

@ -133,6 +133,10 @@ def test_server():
response.status = 205
return
# 204 Database server is up and operational
# 205 Database server is up, but DB is not fully built or is inconsistent
# 403 Database server is up, but provided API key is not valid
@dbserver.route("/scrobbles")
def get_scrobbles():
keys = FormsDict.decode(request.query)

View File

@ -15,13 +15,13 @@ def trackLink(track):
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):
def scrobblesTrackLink(track,timekeys,amount=None,percent=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):
def scrobblesArtistLink(artist,timekeys,amount=None,percent=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 ""

View File

@ -4,9 +4,9 @@ import json
def replacedict(keys,dbport):
from utilities import getArtistInfo
from htmlgenerators import artistLink
from htmlgenerators import clean, artistLink, artistLinks, trackLink, scrobblesTrackLink
clean(keys)
info = getArtistInfo(keys["artist"])
imgurl = info.get("image")
desc = info.get("info")
@ -24,22 +24,7 @@ def replacedict(keys,dbport):
included = db_data.get("associated")
if included is not None and included != []:
includestr = "associated: "
#for a in included:
includestr += ", ".join([artistLink(a) for a in included]) #"<a href=/artist?artist=" + urllib.parse.quote(a) + ">" + a + "</a>, "
#includestr = includestr[:-2]
# response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/tracks?artist=" + urllib.parse.quote(keys["artist"]))
# db_data = json.loads(response.read())
#
# html = "<table class='list'>"
# for e in db_data["list"]:
# html += "<tr>"
# html += "<td class='artists'>"
# links = [artistLink(a) for a in e["artists"]]
# html += ", ".join(links)
# html += "</td><td class='title'>" + e["title"] + "</td>"
# html += "</tr>"
# html += "</table>"
includestr += artistLinks(included)
@ -51,12 +36,10 @@ def replacedict(keys,dbport):
html = "<table class='list'>"
for e in db_data["list"]:
html += "<tr>"
html += "<td class='artists'>"
links = [artistLink(a) for a in e["track"]["artists"]]
html += ", ".join(links)
html += "</td><td class='title'>" + e["track"]["title"] + "</td>"
html += "</td><td class='amount'>" + str(e["scrobbles"]) + "</td>"
html += "<td class='bar'><div style='width:" + str(e["scrobbles"]/maxbar * 100) + "%;'></div></td>"
html += "<td class='artists'>" + artistLinks(e["track"]["artists"]) + "</td>"
html += "<td>" + trackLink(e["track"]) + "</td>"
html += "<td class='amount'>" + scrobblesTrackLink(e["track"],{},amount=e["scrobbles"]) + "</td>"
html += "<td class='bar'>" + scrobblesTrackLink(e["track"],{},pixels=e["scrobbles"]*100/maxbar) + "</td>"
html += "</tr>"
html += "</table>"

View File

@ -10,38 +10,44 @@ def replacedict(keys,dbport):
timekeys = pickKeys(keys,"since","to","in")
limitkeys = pickKeys(keys,"artist","title","associated")
limitstring = ""
# Get scrobble data
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/scrobbles?" + keysToUrl(limitkeys,timekeys))
db_data = json.loads(response.read())
scrobbles = db_data["list"]
# describe the scope
limitstring = ""
if keys.get("title") is not None:
limitstring += "of " + keys.get("title") + " "
limitstring += "by " + ", ".join([artistLink(a) for a in keys.getall("artist")])
latestartist = keys.get("artist")
limitstring += "of " + trackLink({"title":keys.get("title"),"artists":keys.getall("artist")}) + " "
limitstring += "by " + artistLinks(keys.getall("artist"))
elif keys.get("artist") is not None:
latestartist = keys.get("artist")
limitstring += "by " + artistLink(keys.get("artist")) #if we dont specifiy a title, we filter by one artist, which means only one artist is allowed
limitstring += "by " + artistLink(keys.get("artist"))
if keys.get("associated") is not None:
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/artistinfo?artist=" + urllib.parse.quote(keys["artist"]))
db_data = json.loads(response.read())
moreartists = [artistLink(a) for a in db_data["associated"]]
moreartists = db_data["associated"]
if moreartists != []:
limitstring += " <span class='extra'>including " + ", ".join(moreartists) + "</span>"
limitstring += " <span class='extra'>including " + artistLinks(moreartists) + "</span>"
# get representative artist for image
if keys.get("artist") is not None:
imgurl = getArtistInfo(keys.get("artist")).get("image")
elif (len(scrobbles) != 0):
imgurl = getArtistInfo(scrobbles[0]["artists"][0]).get("image")
else:
latestartist = scrobbles[0]["artists"][0]
imgurl = ""
info = getArtistInfo(latestartist)
imgurl = info.get("image")
# build list
html = "<table class='list'>"
for s in scrobbles:
html += "<tr>"
html += "<td class='time'>" + getTimeDesc(s["time"]) + "</td>"
html += "<td class='artists'>" + artistLinks(s["artists"]) + "</td>"
html += "<td class='title'>" + trackLink({"artists":s["artists"],"title":s["title"]}) + "</td></tr>"
html += "<td class='title'>" + trackLink({"artists":s["artists"],"title":s["title"]}) + "</td>"
html += "</tr>"
html += "</table>"
return {"KEY_SCROBBLELIST":html,"KEY_SCROBBLES":str(len(scrobbles)),"KEY_IMAGEURL":imgurl,"KEY_LIMITS":limitstring}

View File

@ -10,6 +10,7 @@ def replacedict(keys,dbport):
timekeys = pickKeys(keys,"since","to","in")
limitkeys = pickKeys(keys)
# get chart data
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/charts/artists?" + keysToUrl(timekeys,limitkeys))
db_data = json.loads(response.read())
charts = db_data["list"][:50]
@ -18,13 +19,14 @@ def replacedict(keys,dbport):
info = getArtistInfo(topartist)
imgurl = info.get("image")
# get total amount of scrobbles
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/scrobbles?" + keysToUrl(timekeys,limitkeys))
db_data = json.loads(response.read())
scrobblelist = db_data["list"]
scrobbles = len(scrobblelist)
# build list
maxbar = charts[0]["scrobbles"]
i = 1
@ -37,7 +39,7 @@ def replacedict(keys,dbport):
html += " <span class='extra'>incl. " + ", ".join([artistLink(a) for a in e["counting"]]) + "</span>"
html += "</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 += "<td class='bar'>" + scrobblesArtistLink(e["artist"],timekeys,percent=e["scrobbles"]*100/maxbar,associated=True) + "</td>"
html += "</tr>"
i += 1
html += "</table>"

View File

@ -10,6 +10,7 @@ def replacedict(keys,dbport):
timekeys = pickKeys(keys,"since","to","in")
limitkeys = pickKeys(keys,"artist")
# get chart data
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/charts/tracks?" + keysToUrl(timekeys,limitkeys))
db_data = json.loads(response.read())
charts = db_data["list"][:50]
@ -26,12 +27,14 @@ def replacedict(keys,dbport):
imgurl = info.get("image")
# get total amount of scrobbles
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/scrobbles?" + keysToUrl(timekeys,limitkeys))
db_data = json.loads(response.read())
scrobblelist = db_data["list"]
scrobbles = len(scrobblelist)
# build list
maxbar = charts[0]["scrobbles"]
i = 1
@ -42,7 +45,7 @@ def replacedict(keys,dbport):
html += "<td class='artists'>" + artistLinks(e["track"]["artists"]) + "</td>"
html += "<td class='title'>" + trackLink(e["track"]) + "</td>"
html += "<td class='amount'>" + scrobblesTrackLink(e["track"],timekeys,amount=e["scrobbles"]) + "</td>"
html += "<td class='bar'>" + scrobblesTrackLink(e["track"],timekeys,pixels=e["scrobbles"]*100/maxbar) + "</td>"
html += "<td class='bar'>" + scrobblesTrackLink(e["track"],timekeys,percent=e["scrobbles"]*100/maxbar) + "</td>"
html += "</tr>"
i += 1
html += "</table>"