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

61 lines
2.0 KiB
Python
Raw Normal View History

import urllib
import json
def instructions(keys,dbport):
2018-12-29 02:08:00 +03:00
from utilities import getArtistInfo, getTrackInfo
2018-12-26 21:20:26 +03:00
from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesTrackLink, keysToUrl, pickKeys, clean
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,"artist")
2018-12-23 01:19:52 +03:00
2018-12-27 05:09:29 +03:00
# get chart data
response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/charts/tracks?" + keysToUrl(timekeys,limitkeys))
db_data = json.loads(response.read())
charts = db_data["list"][:50]
limitstring = ""
2018-12-23 01:19:52 +03:00
if keys.get("artist") is not None:
topartist = keys.get("artist")
2018-12-24 23:25:09 +03:00
#limitstring += "by " + ", ".join([artistLink(a) for a in keys.getall("artist")])
limitstring = "by " + artistLink(keys.get("artist"))
2018-12-29 02:08:00 +03:00
info = getArtistInfo(topartist)
imgurl = info.get("image")
else:
2018-12-29 02:08:00 +03:00
#topartist = charts[0]["track"]["artists"][0] #for now
info = getTrackInfo(charts[0]["track"]["artists"],charts[0]["track"]["title"])
imgurl = info.get("image")
pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else []
2018-12-27 05:09:29 +03:00
# 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)
2018-12-27 05:09:29 +03:00
# build list
maxbar = charts[0]["scrobbles"]
i = 1
html = "<table class='list'>"
for e in charts:
html += "<tr>"
2018-12-26 21:20:26 +03:00
html += "<td class='rank'>#" + str(i) + "</td>"
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>"
2018-12-27 05:09:29 +03:00
html += "<td class='bar'>" + scrobblesTrackLink(e["track"],timekeys,percent=e["scrobbles"]*100/maxbar) + "</td>"
html += "</tr>"
i += 1
html += "</table>"
replace = {"KEY_TOPARTIST_IMAGEURL":imgurl,"KEY_SCROBBLES":str(scrobbles),"KEY_TRACKLIST":html,"KEY_LIMITS":limitstring}
return (replace,pushresources)