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

49 lines
1.6 KiB
Python
Raw Normal View History

2018-12-17 18:33:26 +03:00
import urllib
import json
def replacedict(keys,dbport):
2018-12-26 19:42:55 +03:00
from utilities import getArtistInfo
2018-12-26 21:20:26 +03:00
from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesArtistLink, keysToUrl, pickKeys, clean
2018-12-17 18:33:26 +03:00
2018-12-26 21:20:26 +03:00
clean(keys)
2018-12-26 19:42:55 +03:00
timekeys = pickKeys(keys,"since","to","in")
limitkeys = pickKeys(keys)
2018-12-17 18:33:26 +03:00
2018-12-27 05:09:29 +03:00
# get chart data
2018-12-26 19:42:55 +03:00
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/charts/artists?" + keysToUrl(timekeys,limitkeys))
2018-12-17 18:33:26 +03:00
db_data = json.loads(response.read())
charts = db_data["list"][:50]
topartist = charts[0]["artist"]
info = getArtistInfo(topartist)
imgurl = info.get("image")
2018-12-27 05:09:29 +03:00
# get total amount of scrobbles
2018-12-26 19:42:55 +03:00
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/scrobbles?" + keysToUrl(timekeys,limitkeys))
2018-12-17 18:33:26 +03:00
db_data = json.loads(response.read())
scrobblelist = db_data["list"]
scrobbles = len(scrobblelist)
2018-12-27 05:09:29 +03:00
# build list
2018-12-22 02:31:27 +03:00
maxbar = charts[0]["scrobbles"]
2018-12-17 18:33:26 +03:00
2018-12-22 02:31:27 +03:00
i = 1
html = "<table class='list'>"
2018-12-17 18:33:26 +03:00
for e in charts:
2018-12-22 02:31:27 +03:00
html += "<tr>"
2018-12-26 21:20:26 +03:00
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>"
2018-12-26 21:20:26 +03:00
html += "</td>"
html += "<td class='amount'>" + scrobblesArtistLink(e["artist"],timekeys,amount=e["scrobbles"],associated=True) + "</td>"
2018-12-27 05:09:29 +03:00
html += "<td class='bar'>" + scrobblesArtistLink(e["artist"],timekeys,percent=e["scrobbles"]*100/maxbar,associated=True) + "</td>"
2018-12-22 02:31:27 +03:00
html += "</tr>"
i += 1
2018-12-17 18:33:26 +03:00
html += "</table>"
return {"KEY_TOPARTIST_IMAGEURL":imgurl,"KEY_SCROBBLES":str(scrobbles),"KEY_ARTISTLIST":html}