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

49 lines
1.9 KiB
Python
Raw Normal View History

2018-12-19 18:11:10 +03:00
import urllib
import json
def replacedict(keys,dbport):
2018-12-26 19:42:55 +03:00
from utilities import getArtistInfo
2018-12-26 21:20:26 +03:00
from htmlgenerators import getTimeDesc, artistLink, artistLinks, trackLink, keysToUrl, pickKeys, clean
2018-12-19 18:11:10 +03:00
2018-12-26 21:20:26 +03:00
clean(keys)
2018-12-26 19:42:55 +03:00
timekeys = pickKeys(keys,"since","to","in")
2018-12-26 21:20:26 +03:00
limitkeys = pickKeys(keys,"artist","title","associated")
2018-12-24 23:25:09 +03:00
2018-12-19 18:11:10 +03:00
limitstring = ""
2018-12-26 19:42:55 +03:00
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/scrobbles?" + keysToUrl(limitkeys,timekeys))
2018-12-23 01:19:52 +03:00
db_data = json.loads(response.read())
scrobbles = db_data["list"]
2018-12-24 23:25:09 +03:00
if keys.get("title") is not None:
limitstring += "of " + keys.get("title") + " "
limitstring += "by " + ", ".join([artistLink(a) for a in keys.getall("artist")])
latestartist = keys.get("artist")
elif keys.get("artist") is not None:
2018-12-23 01:19:52 +03:00
latestartist = keys.get("artist")
2018-12-24 23:25:09 +03:00
limitstring += "by " + artistLink(keys.get("artist")) #if we dont specifiy a title, we filter by one artist, which means only one artist is allowed
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())
2018-12-23 01:19:52 +03:00
moreartists = [artistLink(a) for a in db_data["associated"]]
if moreartists != []:
limitstring += " <span class='extra'>including " + ", ".join(moreartists) + "</span>"
else:
latestartist = scrobbles[0]["artists"][0]
2018-12-19 18:11:10 +03:00
2018-12-23 01:19:52 +03:00
info = getArtistInfo(latestartist)
imgurl = info.get("image")
2018-12-22 02:31:27 +03:00
html = "<table class='list'>"
2018-12-19 18:11:10 +03:00
for s in scrobbles:
2018-12-26 21:20:26 +03:00
html += "<tr>"
html += "<td class='time'>" + getTimeDesc(s["time"]) + "</td>"
html += "<td class='artists'>" + artistLinks(s["artists"]) + "</td>"
html += "<td class='title'>" + trackLink({"artists":s["artists"],"title":s["title"]}) + "</td></tr>"
2018-12-19 18:11:10 +03:00
html += "</table>"
return {"KEY_SCROBBLELIST":html,"KEY_SCROBBLES":str(len(scrobbles)),"KEY_IMAGEURL":imgurl,"KEY_LIMITS":limitstring}