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

Significant rework of the architecture

* URL keys and internal keys are now being separated more cleanly
* HTML generation is split up into submodules that can be used by serveral websites
* HTML generation now talks directly to the database in most cases instead of calling the database server for json data
This commit is contained in:
Krateng
2019-02-20 18:22:45 +01:00
parent a4b95969d5
commit 5f19e7b38e
15 changed files with 461 additions and 415 deletions

View File

@@ -4,47 +4,60 @@ import json
def instructions(keys,dbport):
from utilities import getArtistInfo
from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesArtistLink, keysToUrl, pickKeys, clean
from htmlgenerators import KeySplit
from htmlmodules import module_artistcharts
clean(keys)
timekeys = pickKeys(keys,"since","to","in")
limitkeys = pickKeys(keys)
# clean(keys)
# timekeys = pickKeys(keys,"since","to","in")
# limitkeys = pickKeys(keys)
_, timekeys, _, amountkeys = KeySplit(keys)
# get chart data
response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/charts/artists?" + keysToUrl(timekeys,limitkeys))
db_data = json.loads(response.read())
charts = db_data["list"][:50]
topartist = charts[0]["artist"]
# response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/charts/artists?" + keysToUrl(timekeys,limitkeys))
# db_data = json.loads(response.read())
# charts = db_data["list"][:50]
# topartist = charts[0]["artist"]
# info = getArtistInfo(topartist)
# imgurl = info.get("image")
info = getArtistInfo(topartist)
imgurl = info.get("image")
pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else []
# get total amount of scrobbles
response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/scrobbles?" + keysToUrl(timekeys,limitkeys))
db_data = json.loads(response.read())
scrobblelist = db_data["list"]
scrobbles = len(scrobblelist)
#response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/scrobbles?" + keysToUrl(timekeys,limitkeys))
#db_data = json.loads(response.read())
#scrobblelist = db_data["list"]
#scrobbles = len(scrobblelist)
html_charts, rep = module_artistcharts(**amountkeys,**timekeys)
if rep is not None:
imgurl = getArtistInfo(rep).get("image")
else:
imgurl = ""
pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else []
# build list
maxbar = charts[0]["scrobbles"]
i = 1
html = "<table class='list'>"
for e in charts:
html += "<tr>"
html += "<td class='rank'>#" + str(i) + "</td>"
html += "<td class='artist'>" + artistLink(e["artist"])
if (e["counting"] != []):
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,percent=e["scrobbles"]*100/maxbar,associated=True) + "</td>"
html += "</tr>"
i += 1
html += "</table>"
# maxbar = charts[0]["scrobbles"]
#
# i = 1
# html = "<table class='list'>"
# for e in charts:
# html += "<tr>"
# html += "<td class='rank'>#" + str(i) + "</td>"
# html += "<td class='artist'>" + artistLink(e["artist"])
# if (e["counting"] != []):
# 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,percent=e["scrobbles"]*100/maxbar,associated=True) + "</td>"
# html += "</tr>"
# i += 1
# html += "</table>"
replace = {"KEY_TOPARTIST_IMAGEURL":imgurl,"KEY_SCROBBLES":str(scrobbles),"KEY_ARTISTLIST":html}
replace = {"KEY_TOPARTIST_IMAGEURL":imgurl,"KEY_ARTISTLIST":html_charts}
return (replace,pushresources)