Database improvements and top tracks web interface

This commit is contained in:
Krateng 2018-12-22 14:06:21 +01:00
parent eb9c9b2f69
commit dfe46d0242
8 changed files with 134 additions and 18 deletions

View File

@ -136,7 +136,8 @@ def test_server():
@dbserver.route("/scrobbles")
def get_scrobbles():
keys = FormsDict.decode(request.query)
r = db_query(artist=keys.get("artist"),track=keys.get("track"),since=keys.get("since"),to=keys.get("to"))
r = db_query(artist=keys.get("artist"),track=keys.get("track"),since=keys.get("since"),to=keys.get("to"),associated=(keys.get("associated")!=None))
r.reverse()
return {"list":r} ##json can't be a list apparently???
@ -172,10 +173,12 @@ def get_charts_artists():
@dbserver.route("/charts/tracks")
def get_charts_tracks():
since = request.query.get("since")
to = request.query.get("to")
keys = FormsDict.decode(request.query)
since = keys.get("since")
to = keys.get("to")
artist = keys.get("artist")
return {"list":db_aggregate(by="TRACK",since=since,to=to)}
return {"list":db_aggregate(by="TRACK",since=since,to=to,artist=artist)}
@dbserver.route("/charts")
def get_charts():
@ -646,7 +649,7 @@ def sync():
# Queries the database
def db_query(artist=None,track=None,since=None,to=None):
def db_query(artist=None,track=None,since=None,to=None,associated=False):
(since, to) = getTimestamps(since,to)
@ -655,8 +658,11 @@ def db_query(artist=None,track=None,since=None,to=None):
artist = ARTISTS.index(artist)
if isinstance(track, str):
track = TRACKS.index(track)
return [getScrobbleObject(s) for s in SCROBBLES if (s[0] == track or track==None) and (artist in TRACKS[s[0]][0] or artist==None) and (since < s[1] < to)]
if associated:
return [getScrobbleObject(s) for s in SCROBBLES if (s[0] == track or track==None) and (artist in coa.getCreditedList(TRACKS[s[0]][0]) or artist==None) and (since < s[1] < to)]
else:
return [getScrobbleObject(s) for s in SCROBBLES if (s[0] == track or track==None) and (artist in TRACKS[s[0]][0] or artist==None) and (since < s[1] < to)]
# pointless to check for artist when track is checked because every track has a fixed set of artists, but it's more elegant this way
@ -665,9 +671,12 @@ def db_query(artist=None,track=None,since=None,to=None):
# Queries that... well... aggregate
def db_aggregate(by=None,since=None,to=None):
def db_aggregate(by=None,since=None,to=None,artist=None):
(since, to) = getTimestamps(since,to)
if isinstance(artist, str):
artist = ARTISTS.index(artist)
if (by=="ARTIST"):
#this is probably a really bad idea
#for a in ARTISTS:
@ -682,12 +691,12 @@ def db_aggregate(by=None,since=None,to=None):
# this either creates the new entry or increments the existing one
charts[a] = charts.setdefault(a,0) + 1
ls = [{"artist":getArtistObject(ARTISTS[a]),"scrobbles":charts[a]} for a in charts]
ls = [{"artist":getArtistObject(ARTISTS[a]),"scrobbles":charts[a],"counting":coa.getAllAssociated(ARTISTS[a])} for a in charts]
return sorted(ls,key=lambda k:k["scrobbles"], reverse=True)
elif (by=="TRACK"):
charts = {}
for s in [scr for scr in SCROBBLES if since < scr[1] < to]:
for s in [scr for scr in SCROBBLES if since < scr[1] < to and (artist==None or (artist in TRACKS[scr[0]][0]))]:
track = s[0]
# this either creates the new entry or increments the existing one
charts[track] = charts.setdefault(track,0) + 1

View File

@ -23,7 +23,7 @@
</tr>
</table>
<h2>Tracks</h2>
<h2><a href='/toptracks?artist=KEY_ENC_ARTISTNAME'>Tracks</a></h2>
KEY_TRACKLIST
</body>

View File

@ -27,19 +27,37 @@ def replacedict(keys,dbport):
includestr += ", ".join([artistLink(a) for a in included]) #"<a href=/artist?artist=" + urllib.parse.quote(a) + ">" + a + "</a>, "
#includestr = includestr[:-2]
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/tracks?artist=" + urllib.parse.quote(keys["artist"]))
# response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/tracks?artist=" + urllib.parse.quote(keys["artist"]))
# db_data = json.loads(response.read())
#
# html = "<table class='list'>"
# for e in db_data["list"]:
# html += "<tr>"
# html += "<td class='artists'>"
# links = [artistLink(a) for a in e["artists"]]
# html += ", ".join(links)
# html += "</td><td class='title'>" + e["title"] + "</td>"
# html += "</tr>"
# html += "</table>"
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/charts/tracks?artist=" + urllib.parse.quote(keys["artist"]))
db_data = json.loads(response.read())
maxbar = db_data["list"][0]["scrobbles"]
html = "<table class='list'>"
for e in db_data["list"]:
html += "<tr>"
html += "<td class='artists'>"
links = [artistLink(a) for a in e["artists"]]
links = [artistLink(a) for a in e["track"]["artists"]]
html += ", ".join(links)
html += "</td><td class='title'>" + e["title"] + "</td>"
html += "</td><td class='title'>" + e["track"]["title"] + "</td>"
html += "</td><td class='amount'>" + str(e["scrobbles"]) + "</td>"
html += "<td class='bar'><div style='width:" + str(e["scrobbles"]/maxbar * 100) + "%;'></div></td>"
html += "</tr>"
html += "</table>"
return {"KEY_ARTISTNAME":keys["artist"],"KEY_ENC_ARTISTNAME":urllib.parse.quote(keys["artist"]),"KEY_IMAGEURL":imgurl, "KEY_DESCRIPTION":desc,"KEY_TRACKLIST":html,"KEY_SCROBBLES":scrobbles,"KEY_POSITION":pos,"KEY_ASSOCIATED":includestr}

View File

@ -51,6 +51,11 @@ table.top_info td.text .stats {
text-align:right;
color:grey;
}
.extra {
color:gray; /*sue me*/
font-size:80%;
}
a {
cursor:pointer;
}

View File

@ -18,7 +18,12 @@ def replacedict(keys,dbport):
limitstring = ""
if keys.get("artist") is not None:
#limitstring += "by <a href='/artist?artist=" + urllib.parse.quote(keys.get("artist")) + "'>" + keys.get("artist") + "</a> "
limitstring += "by " + artistLink(keys.get("artist"))
limitstring += "by " + artistLink(keys.get("artist"))
if keys.get("associated") is not None:
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/artistinfo?artist=" + urllib.parse.quote(keys["artist"]))
db_data = json.loads(response.read())
moreartists = db_data["associated"]
limitstring += " <span class='extra'>including " + ", ".join(moreartists) + "</span>"
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/scrobbles?" + extrakeys)
db_data = json.loads(response.read())

View File

@ -32,8 +32,10 @@ def replacedict(keys,dbport):
html += "<td class='rank'>#" + str(i) + "</td><td class='artist'>"
#html += "<a href=/artist?artist=" + urllib.parse.quote(e["artist"]) + ">" + e["artist"] + "</a>"
html += artistLink(e["artist"])
html += "</td><td class='amount'><a href='/scrobbles?artist=" + urllib.parse.quote(e["artist"]) + "&" + extrakeys + "'>" + str(e["scrobbles"]) + "</a></td>"
html += "<td class='bar'><a href='/scrobbles?artist=" + urllib.parse.quote(e["artist"]) + "&" + extrakeys + "'><div style='width:" + str(e["scrobbles"]/maxbar * 100) + "%;'></div></a></td>"
if (e["counting"] != []):
html += " <span class='extra'>incl. " + ", ".join([artistLink(a) for a in e["counting"]]) + "</span>"
html += "</td><td class='amount'><a href='/scrobbles?artist=" + urllib.parse.quote(e["artist"]) + "&associated&" + extrakeys + "'>" + str(e["scrobbles"]) + "</a></td>"
html += "<td class='bar'><a href='/scrobbles?artist=" + urllib.parse.quote(e["artist"]) + "&associated&" + extrakeys + "'><div style='width:" + str(e["scrobbles"]/maxbar * 100) + "%;'></div></a></td>"
html += "</tr>"
i += 1
html += "</table>"

29
website/toptracks.html Normal file
View File

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

48
website/toptracks.py Normal file
View File

@ -0,0 +1,48 @@
import urllib
import json
def replacedict(keys,dbport):
from utilities import getArtistInfo, artistLink
#hand down the since and from arguments
extrakeys = urllib.parse.urlencode(keys,quote_via=urllib.parse.quote,safe="/")
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/charts/tracks?" + extrakeys)
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 " + artistLink(keys.get("artist"))
else:
topartist = charts[0]["track"]["artists"][0] #for now
info = getArtistInfo(topartist)
imgurl = info.get("image")
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/scrobbles?" + extrakeys)
db_data = json.loads(response.read())
scrobblelist = db_data["list"]
scrobbles = len(scrobblelist)
maxbar = charts[0]["scrobbles"]
i = 1
html = "<table class='list'>"
for e in charts:
html += "<tr>"
html += "<td class='rank'>#" + str(i) + "</td><td class='artists'>"
html += ", ".join([artistLink(a) for a in e["track"]["artists"]])
html += "</td><td class='title'>" + e["track"]["title"]
html += "</td><td class='amount'>" + str(e["scrobbles"]) + "</td>"
html += "<td class='bar'><div style='width:" + str(e["scrobbles"]/maxbar * 100) + "%;'></div>"
html += "</td>"
html += "</tr>"
i += 1
html += "</table>"
return {"KEY_TOPARTIST_IMAGEURL":imgurl,"KEY_SCROBBLES":str(scrobbles),"KEY_TRACKLIST":html,"KEY_LIMITS":limitstring}