From b17cfa21b4638060f9eda6b328e878f508951463 Mon Sep 17 00:00:00 2001 From: Krateng Date: Mon, 18 Feb 2019 15:52:08 +0100 Subject: [PATCH] Added 'pulse' web view --- database.py | 5 +++- htmlgenerators.py | 6 ++++ website/pulse.html | 28 ++++++++++++++++++ website/pulse.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 website/pulse.html create mode 100644 website/pulse.py diff --git a/database.py b/database.py index dc748c8..48d6eb6 100644 --- a/database.py +++ b/database.py @@ -303,6 +303,8 @@ def get_pulse_external(): ckeys = {} ckeys["since"], ckeys["to"], ckeys["within"] = keys.get("since"), keys.get("to"), keys.get("in") ckeys["step"], ckeys["trail"] = keys.get("step"), int_or_none(keys.get("trail")) + ckeys["artists"], ckeys["title"] = keys.getall("artist"), keys.get("title") + ckeys["associated"] = (keys.get("associated")!=None) if ckeys["step"] is not None: [ckeys["step"],ckeys["stepn"]] = (ckeys["step"].split("-") + [1])[:2] # makes the multiplier 1 if not assigned if "stepn" in ckeys: ckeys["stepn"] = int(ckeys["stepn"]) @@ -323,7 +325,8 @@ def get_pulse(step="month",stepn=1,trail=3,**keys): d_current = d_start while True: d_current_end = getNext(d_current,step,stepn * trail) - res = db_aggregate(since=d_current,to=d_current_end) + #res = db_aggregate(since=d_current,to=d_current_end) + res = len(db_query(since=d_current,to=d_current_end,**{k:keys[k] for k in keys if k in ["artists","title","associated"]})) results.append({"from":d_current,"to":d_current_end,"scrobbles":res}) d_current = getNext(d_current,step,stepn) if isPast(d_current_end,d_end): diff --git a/htmlgenerators.py b/htmlgenerators.py index 66d16d9..9ea124a 100644 --- a/htmlgenerators.py +++ b/htmlgenerators.py @@ -24,6 +24,12 @@ def scrobblesArtistLink(artist,timekeys,amount=None,percent=None,associated=Fals inner = str(amount) if amount is not None else "
" askey = "&associated" if associated else "" return "" + inner + "" + +def scrobblesLink(timekeys,amount=None,percent=None,artist=None,track=None,associated=False): + if track is not None: return scrobblesTrackLink(track,timekeys,amount,percent) + if artist is not None: return scrobblesArtistLink(artist,timekeys,amount,percent,associated) + inner = str(amount) if amount is not None else "
" + return "" + inner + "" # necessary because urllib.parse.urlencode doesnt handle multidicts def keysToUrl(*dicts): diff --git a/website/pulse.html b/website/pulse.html new file mode 100644 index 0000000..71745bf --- /dev/null +++ b/website/pulse.html @@ -0,0 +1,28 @@ + + + + + + Maloja - Pulse + + + + + + + + + +
+
+
+

Pulse


+ KEY_LIMITS + + +
+ + KEY_PULSE_TABLE + + + diff --git a/website/pulse.py b/website/pulse.py new file mode 100644 index 0000000..c3536bc --- /dev/null +++ b/website/pulse.py @@ -0,0 +1,72 @@ +import urllib +import json + + +def instructions(keys,dbport): + from utilities import getArtistInfo, getTrackInfo + from htmlgenerators import getTimeDesc, artistLink, artistLinks, trackLink, scrobblesLink, keysToUrl, pickKeys, clean + + clean(keys) + timekeys = pickKeys(keys,"since","to","in","step","trail") + limitkeys = pickKeys(keys,"artist","title","associated") + + # Get scrobble data + response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/pulse?" + keysToUrl(limitkeys,timekeys)) + db_data = json.loads(response.read()) + terms = db_data["list"] + + # describe the scope (and creating a key for the relevant artist or track) + limitstring = "" + limitkey = {} + if keys.get("title") is not None: + limitkey["track"] = {"artists":keys.getall("artist"),"title":keys.get("title")} + limitstring += "of " + trackLink(limitkey["track"]) + " " + limitstring += "by " + artistLinks(keys.getall("artist")) + + elif keys.get("artist") is not None: + limitkey["artist"], limitkey["associated"] = keys.get("artist"), (keys.get("associated")!=None) + limitstring += "of " + artistLink(keys.get("artist")) + if keys.get("associated") is not None: + response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/artistinfo?artist=" + urllib.parse.quote(keys["artist"])) + db_data = json.loads(response.read()) + moreartists = db_data["associated"] + if moreartists != []: + limitstring += " including " + artistLinks(moreartists) + "" + + + # get image + if limitkeys.get("title") is not None: + imgurl = getTrackInfo(limitkeys.getall("artist"),limitkeys.get("title")).get("image") + elif keys.get("artist") is not None: + imgurl = getArtistInfo(keys.get("artist")).get("image") + #elif (len(scrobbles) != 0): + # imgurl = getTrackInfo(scrobbles[0]["artists"],scrobbles[0]["title"]).get("image") + # #imgurl = getArtistInfo(scrobbles[0]["artists"][0]).get("image") + else: + imgurl = "" + + pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else [] + + + + # build list + maxbar = max([t["scrobbles"] for t in terms]) + + i = 1 + html = "" + for t in terms: + fromstr = "/".join([str(e) for e in t["from"]]) + tostr = "/".join([str(e) for e in t["to"]]) + html += "" + html += "" + html += "" + html += "" + html += "" + html += "" + i += 1 + html += "
" + fromstr + "" + tostr + "" + scrobblesLink({"since":fromstr,"to":tostr},amount=t["scrobbles"],**limitkey) + "" + scrobblesLink({"since":fromstr,"to":tostr},percent=t["scrobbles"]*100/maxbar,**limitkey) + "
" + + replace = {"KEY_PULSE_TABLE":html,"KEY_IMAGEURL":imgurl,"KEY_LIMITS":limitstring} + + return (replace,pushresources) +