Added scrobbles view

This commit is contained in:
Krateng 2018-12-19 16:11:10 +01:00
parent dace8f1c44
commit 3591f437ed
3 changed files with 51 additions and 0 deletions

View File

@ -118,6 +118,7 @@ def test_server():
def get_scrobbles():
keys = request.query
r = db_query(artist=keys.get("artist"),track=keys.get("track"),since=keys.get("since"),to=keys.get("to"))
r.reverse()
return {"list":r} ##json can't be a list apparently???
@ -427,6 +428,8 @@ def build_db():
readScrobble(artists,title,time)
SCROBBLES.sort(key = lambda tup: tup[1])

View File

@ -147,3 +147,10 @@ def cacheImage(url,path,filename):
response = urllib.request.urlopen(url)
target = path + "/" + filename + "." + response.info().get_content_subtype()
urllib.request.urlretrieve(url,target)
def getTimeDesc(timestamp):
import datetime
tim = datetime.datetime.utcfromtimestamp(timestamp)
return tim.strftime("%d. %b %Y %I:%M %p")

41
website/scrobbles.py Normal file
View File

@ -0,0 +1,41 @@
import urllib
import json
def replacedict(keys,dbport):
from utilities import getArtistInfo, getTimeDesc
#hand down the since and from arguments
extrakeys = urllib.parse.urlencode(keys)
if keys.get("artist") is not None:
info = getArtistInfo(keys.get("artist"))
imgurl = info.get("image")
else:
imgurl = "" #for now
limitstring = ""
if keys.get("artist") is not None:
limitstring += "by " + keys.get("artist") + " "
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/scrobbles?" + extrakeys)
db_data = json.loads(response.read())
scrobbles = db_data["list"]
html = "<table>"
for s in scrobbles:
html += "<tr><td>"
timestring = getTimeDesc(s["time"])
html += timestring
html += "</td><td>"
artisthtml = ""
for a in s["artists"]:
artisthtml += "<a href=/artist?artist=" + urllib.parse.quote(a) + ">" + a + "</a>, "
html += artisthtml[:-2]
html += "</td><td>" + s["title"] + "</td></tr>"
html += "</table>"
return {"KEY_SCROBBLELIST":html,"KEY_SCROBBLES":str(len(scrobbles)),"KEY_IMAGEURL":imgurl,"KEY_LIMITS":limitstring}