import urllib
import json
def instructions(keys,dbport):
from utilities import getArtistInfo, getTrackInfo
from htmlgenerators import clean, artistLink, artistLinks, trackLink, scrobblesTrackLink, keysToUrl, pickKeys, getTimeDesc, getRangeDesc, scrobblesLink
clean(keys)
limitkeys = pickKeys(keys,"artist","title")
trackobject = {"artists":limitkeys.getall("artist"),"title":limitkeys.get("title")}
info = getTrackInfo(keys.getall("artist"),keys.get("title"))
imgurl = info.get("image")
pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else []
response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/trackinfo?" + keysToUrl(limitkeys))
db_data = json.loads(response.read())
scrobblesnum = str(db_data["scrobbles"])
pos = "#" + str(db_data["position"])
response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/scrobbles?" + keysToUrl(limitkeys))
db_data = json.loads(response.read())
scrobbles = db_data["list"]
# build list
html = "
"
for s in scrobbles:
html += ""
html += "| " + getTimeDesc(s["time"]) + " | "
html += "" + artistLinks(s["artists"]) + " | "
html += "" + trackLink({"artists":s["artists"],"title":s["title"]}) + " | "
html += "
"
html += "
"
# pulse
response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/pulse?step=year&trail=1&" + keysToUrl(limitkeys))
db_data = json.loads(response.read())
terms = db_data["list"]
# build list
maxbar = max([t["scrobbles"] for t in terms])
html_pulse = ""
for t in terms:
fromstr = "/".join([str(e) for e in t["from"]])
tostr = "/".join([str(e) for e in t["to"]])
html_pulse += ""
#html += "| " + fromstr + " | "
#html += "" + tostr + " | "
html_pulse += "" + getRangeDesc(t["from"],t["to"]) + " | "
html_pulse += "" + scrobblesLink({"since":fromstr,"to":tostr},amount=t["scrobbles"],track=trackobject) + " | "
html_pulse += "" + scrobblesLink({"since":fromstr,"to":tostr},percent=t["scrobbles"]*100/maxbar,track=trackobject) + " | "
html_pulse += "
"
html_pulse += "
"
replace = {"KEY_TRACKTITLE":limitkeys.get("title"),"KEY_ARTISTS":artistLinks(limitkeys.getall("artist")),"KEY_SCROBBLES":scrobblesnum,"KEY_IMAGEURL":imgurl,
"KEY_SCROBBLELINK":keysToUrl(limitkeys),"KEY_SCROBBLELIST":html,"KEY_POSITION":pos,"KEY_PULSE":html_pulse}
return (replace,pushresources)