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

Added 'pulse' web view

This commit is contained in:
Krateng 2019-02-18 15:52:08 +01:00
parent b65d65326c
commit b17cfa21b4
4 changed files with 110 additions and 1 deletions

View File

@ -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):

View File

@ -24,6 +24,12 @@ def scrobblesArtistLink(artist,timekeys,amount=None,percent=None,associated=Fals
inner = str(amount) if amount is not None else "<div style='width:" + str(percent) + "%;'></div>"
askey = "&associated" if associated else ""
return "<a href='/scrobbles?artist=" + urllib.parse.quote(artist) + "&" + keysToUrl(timekeys) + askey + "'>" + inner + "</a>"
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 "<div style='width:" + str(percent) + "%;'></div>"
return "<a href='/scrobbles?" + keysToUrl(timekeys) + "'>" + inner + "</a>"
# necessary because urllib.parse.urlencode doesnt handle multidicts
def keysToUrl(*dicts):

28
website/pulse.html Normal file
View File

@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Maloja - Pulse</title>
<link rel="stylesheet" href="maloja.css" />
</head>
<body>
<table class="top_info">
<tr>
<td class="image">
<div style="background-image:url('KEY_IMAGEURL')"></div>
</td>
<td class="text">
<h1>Pulse</h1><br/>
<span>KEY_LIMITS</span>
<!--<p class="stats">KEY_SCROBBLES Scrobbles</p>-->
</td>
</tr>
</table>
KEY_PULSE_TABLE
</body>
</html>

72
website/pulse.py Normal file
View File

@ -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 += " <span class='extra'>including " + artistLinks(moreartists) + "</span>"
# 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 = "<table class='list'>"
for t in terms:
fromstr = "/".join([str(e) for e in t["from"]])
tostr = "/".join([str(e) for e in t["to"]])
html += "<tr>"
html += "<td>" + fromstr + "</td>"
html += "<td>" + tostr + "</td>"
html += "<td class='amount'>" + scrobblesLink({"since":fromstr,"to":tostr},amount=t["scrobbles"],**limitkey) + "</td>"
html += "<td class='bar'>" + scrobblesLink({"since":fromstr,"to":tostr},percent=t["scrobbles"]*100/maxbar,**limitkey) + "</td>"
html += "</tr>"
i += 1
html += "</table>"
replace = {"KEY_PULSE_TABLE":html,"KEY_IMAGEURL":imgurl,"KEY_LIMITS":limitstring}
return (replace,pushresources)