import urllib import json def replacedict(keys,dbport): from utilities import getArtistInfo, getTrackInfo from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesTrackLink, keysToUrl, pickKeys, clean clean(keys) 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] limitstring = "" if keys.get("artist") is not None: topartist = keys.get("artist") #limitstring += "by " + ", ".join([artistLink(a) for a in keys.getall("artist")]) limitstring = "by " + artistLink(keys.get("artist")) info = getArtistInfo(topartist) imgurl = info.get("image") else: #topartist = charts[0]["track"]["artists"][0] #for now info = getTrackInfo(charts[0]["track"]["artists"],charts[0]["track"]["title"]) 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 html = "" for e in charts: html += "" html += "" html += "" html += "" html += "" html += "" html += "" i += 1 html += "
#" + str(i) + "" + artistLinks(e["track"]["artists"]) + "" + trackLink(e["track"]) + "" + scrobblesTrackLink(e["track"],timekeys,amount=e["scrobbles"]) + "" + scrobblesTrackLink(e["track"],timekeys,percent=e["scrobbles"]*100/maxbar) + "
" return {"KEY_TOPARTIST_IMAGEURL":imgurl,"KEY_SCROBBLES":str(scrobbles),"KEY_TRACKLIST":html,"KEY_LIMITS":limitstring}