From dfe46d024222b405bf96a84291e10ae5b0ca0f72 Mon Sep 17 00:00:00 2001 From: Krateng Date: Sat, 22 Dec 2018 14:06:21 +0100 Subject: [PATCH] Database improvements and top tracks web interface --- database.py | 29 ++++++++++++++++--------- website/artist.html | 2 +- website/artist.py | 26 +++++++++++++++++++---- website/maloja.css | 5 +++++ website/scrobbles.py | 7 +++++- website/topartists.py | 6 ++++-- website/toptracks.html | 29 +++++++++++++++++++++++++ website/toptracks.py | 48 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 134 insertions(+), 18 deletions(-) create mode 100644 website/toptracks.html create mode 100644 website/toptracks.py diff --git a/database.py b/database.py index b102597..3d41106 100644 --- a/database.py +++ b/database.py @@ -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 diff --git a/website/artist.html b/website/artist.html index 6ca0ef4..da5de5f 100644 --- a/website/artist.html +++ b/website/artist.html @@ -23,7 +23,7 @@ -

Tracks

+

Tracks

KEY_TRACKLIST diff --git a/website/artist.py b/website/artist.py index 002acbe..c7189a3 100644 --- a/website/artist.py +++ b/website/artist.py @@ -27,19 +27,37 @@ def replacedict(keys,dbport): includestr += ", ".join([artistLink(a) for a in included]) #"" + 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 = "" +# for e in db_data["list"]: +# html += "" +# html += "" +# html += "" +# html += "
" +# links = [artistLink(a) for a in e["artists"]] +# html += ", ".join(links) +# html += "" + e["title"] + "
" + + + + + 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 = "" for e in db_data["list"]: html += "" html += "" + html += "" + html += "" + html += "" html += "" html += "
" - links = [artistLink(a) for a in e["artists"]] + links = [artistLink(a) for a in e["track"]["artists"]] html += ", ".join(links) - html += "" + e["title"] + "" + e["track"]["title"] + "" + str(e["scrobbles"]) + "
" - 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} diff --git a/website/maloja.css b/website/maloja.css index 83cf0cb..c77ca3b 100644 --- a/website/maloja.css +++ b/website/maloja.css @@ -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; } diff --git a/website/scrobbles.py b/website/scrobbles.py index b3cddbb..7436ae1 100644 --- a/website/scrobbles.py +++ b/website/scrobbles.py @@ -18,7 +18,12 @@ def replacedict(keys,dbport): limitstring = "" if keys.get("artist") is not None: #limitstring += "by " + keys.get("artist") + " " - 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 += " including " + ", ".join(moreartists) + "" response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/scrobbles?" + extrakeys) db_data = json.loads(response.read()) diff --git a/website/topartists.py b/website/topartists.py index 815fc59..826876a 100644 --- a/website/topartists.py +++ b/website/topartists.py @@ -32,8 +32,10 @@ def replacedict(keys,dbport): html += "#" + str(i) + "" #html += "" + e["artist"] + "" html += artistLink(e["artist"]) - html += "" + str(e["scrobbles"]) + "" - html += "
" + if (e["counting"] != []): + html += " incl. " + ", ".join([artistLink(a) for a in e["counting"]]) + "" + html += "" + str(e["scrobbles"]) + "" + html += "
" html += "" i += 1 html += "" diff --git a/website/toptracks.html b/website/toptracks.html new file mode 100644 index 0000000..8ef9d6b --- /dev/null +++ b/website/toptracks.html @@ -0,0 +1,29 @@ + + + + + + Maloja - Top Tracks in KEY_RANGE + + + + + + + + + +
+
+
+

Top Tracks


+ KEY_LIMITS +

KEY_SCROBBLES Scrobbles

+ +
+ + + KEY_TRACKLIST + + + diff --git a/website/toptracks.py b/website/toptracks.py new file mode 100644 index 0000000..479bdf6 --- /dev/null +++ b/website/toptracks.py @@ -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 = "" + for e in charts: + html += "" + html += "" + html += "" + html += "" + i += 1 + html += "
#" + str(i) + "" + html += ", ".join([artistLink(a) for a in e["track"]["artists"]]) + html += "" + e["track"]["title"] + html += "" + str(e["scrobbles"]) + "
" + html += "
" + + return {"KEY_TOPARTIST_IMAGEURL":imgurl,"KEY_SCROBBLES":str(scrobbles),"KEY_TRACKLIST":html,"KEY_LIMITS":limitstring} +