diff --git a/server.py b/server.py index 6ad1025..701e069 100755 --- a/server.py +++ b/server.py @@ -25,7 +25,8 @@ webserver = Bottle() @webserver.route("") @webserver.route("/") def mainpage(): - return static_html("start") + response = static_html("start") + return response # this is the fallback option. If you run this service behind a reverse proxy, it is recommended to rewrite /db/ requests to the port of the db server @@ -114,12 +115,19 @@ def static(name): @webserver.route("/") def static_html(name): - + linkheaders = ["; rel=preload; as=style"] keys = removeIdentical(FormsDict.decode(request.query)) # If a python file exists, it provides the replacement dict for the html file if os.path.exists("website/" + name + ".py"): - txt_keys = SourceFileLoader(name,"website/" + name + ".py").load_module().replacedict(keys,DATABASE_PORT) + #txt_keys = SourceFileLoader(name,"website/" + name + ".py").load_module().replacedict(keys,DATABASE_PORT) + txt_keys,resources = SourceFileLoader(name,"website/" + name + ".py").load_module().instructions(keys,DATABASE_PORT) + + # add headers for server push + for resource in resources: + linkheaders.append("<" + resource["file"] + ">; rel=preload; as=" + resource["type"]) + + # apply key substitutions with open("website/" + name + ".html") as htmlfile: html = htmlfile.read() for k in txt_keys: @@ -128,11 +136,14 @@ def static_html(name): for element in txt_keys[k]: html = html.replace(k,element,1) else: - html = html.replace(k,txt_keys[k]) + html = html.replace(k,txt_keys[k]) + + response.set_header("Link",",".join(linkheaders)) return html # Otherwise, we just serve the html file + response.set_header("Link",",".join(linkheaders)) return static_file("website/" + name + ".html",root="") #set graceful shutdown diff --git a/website/artist.py b/website/artist.py index c3903e0..1459212 100644 --- a/website/artist.py +++ b/website/artist.py @@ -2,7 +2,7 @@ import urllib import json -def replacedict(keys,dbport): +def instructions(keys,dbport): from utilities import getArtistInfo from htmlgenerators import clean, artistLink, artistLinks, trackLink, scrobblesTrackLink @@ -10,6 +10,7 @@ def replacedict(keys,dbport): info = getArtistInfo(keys["artist"]) imgurl = info.get("image") #desc = info.get("info") + pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else [] response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/artistinfo?artist=" + urllib.parse.quote(keys["artist"])) db_data = json.loads(response.read()) @@ -43,5 +44,8 @@ def replacedict(keys,dbport): html += "" html += "" + - return {"KEY_ARTISTNAME":keys["artist"],"KEY_ENC_ARTISTNAME":urllib.parse.quote(keys["artist"]),"KEY_IMAGEURL":imgurl, "KEY_DESCRIPTION":"","KEY_TRACKLIST":html,"KEY_SCROBBLES":scrobbles,"KEY_POSITION":pos,"KEY_ASSOCIATED":includestr} + replace = {"KEY_ARTISTNAME":keys["artist"],"KEY_ENC_ARTISTNAME":urllib.parse.quote(keys["artist"]),"KEY_IMAGEURL":imgurl, "KEY_DESCRIPTION":"","KEY_TRACKLIST":html,"KEY_SCROBBLES":scrobbles,"KEY_POSITION":pos,"KEY_ASSOCIATED":includestr} + + return (replace,pushresources) diff --git a/website/issues.py b/website/issues.py index 18c3893..9cf191c 100644 --- a/website/issues.py +++ b/website/issues.py @@ -2,7 +2,7 @@ import urllib import json from htmlgenerators import artistLink -def replacedict(keys,dbport): +def instructions(keys,dbport): response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/issues") db_data = json.loads(response.read()) @@ -42,4 +42,4 @@ def replacedict(keys,dbport): html += "" - return {"KEY_ISSUESLIST":html,"KEY_ISSUES":str(i)} + return ({"KEY_ISSUESLIST":html,"KEY_ISSUES":str(i)},[]) diff --git a/website/scrobbles.py b/website/scrobbles.py index c36c132..a08a05b 100644 --- a/website/scrobbles.py +++ b/website/scrobbles.py @@ -2,7 +2,7 @@ import urllib import json -def replacedict(keys,dbport): +def instructions(keys,dbport): from utilities import getArtistInfo, getTrackInfo from htmlgenerators import getTimeDesc, artistLink, artistLinks, trackLink, keysToUrl, pickKeys, clean @@ -41,6 +41,8 @@ def replacedict(keys,dbport): #imgurl = getArtistInfo(scrobbles[0]["artists"][0]).get("image") else: imgurl = "" + + pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else [] # build list @@ -54,5 +56,7 @@ def replacedict(keys,dbport): html += "" html += "" - return {"KEY_SCROBBLELIST":html,"KEY_SCROBBLES":str(len(scrobbles)),"KEY_IMAGEURL":imgurl,"KEY_LIMITS":limitstring} + replace = {"KEY_SCROBBLELIST":html,"KEY_SCROBBLES":str(len(scrobbles)),"KEY_IMAGEURL":imgurl,"KEY_LIMITS":limitstring} + + return (replace,pushresources) diff --git a/website/start.py b/website/start.py index 0469a5d..8f71f37 100644 --- a/website/start.py +++ b/website/start.py @@ -12,13 +12,15 @@ def getpictures(ls,result,tracks=False): for element in getArtistsInfo(ls): result.append(element.get("image")) -def replacedict(keys,dbport): +def instructions(keys,dbport): from utilities import getArtistsInfo, getTracksInfo from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesArtistLink, keysToUrl, pickKeys, clean, getTimeDesc max_show = 15 posrange = ["#" + str(i) for i in range(1,max_show)] + + #clean(keys) #timekeys = pickKeys(keys,"since","to","in") #limitkeys = pickKeys(keys) @@ -91,11 +93,14 @@ def replacedict(keys,dbport): t1.join() t2.join() t3.join() + + pushresources = [{"file":img,"type":"image"} for img in artistimages + trackimages + scrobbleimages if img.startswith("/")] - - return {"KEY_ARTISTIMAGE":artistimages,"KEY_ARTISTNAME":artisttitles,"KEY_ARTISTLINK":artistlinks,"KEY_POSITION_ARTIST":posrange, + replace = {"KEY_ARTISTIMAGE":artistimages,"KEY_ARTISTNAME":artisttitles,"KEY_ARTISTLINK":artistlinks,"KEY_POSITION_ARTIST":posrange, "KEY_TRACKIMAGE":trackimages,"KEY_TRACKNAME":tracktitles,"KEY_TRACKLINK":tracklinks,"KEY_POSITION_TRACK":posrange, "KEY_SCROBBLES_TODAY":scrobbles_today,"KEY_SCROBBLES_MONTH":scrobbles_month,"KEY_SCROBBLES_YEAR":scrobbles_year,"KEY_SCROBBLES_TOTAL":scrobbles_total, "KEY_SCROBBLE_TIME":scrobbletimes,"KEY_SCROBBLE_ARTISTS":scrobbleartists,"KEY_SCROBBLE_TITLE":scrobbletracklinks,"KEY_SCROBBLE_IMAGE":scrobbleimages} + + return (replace,pushresources) diff --git a/website/topartists.py b/website/topartists.py index 0f6e07a..45387fa 100644 --- a/website/topartists.py +++ b/website/topartists.py @@ -2,7 +2,7 @@ import urllib import json -def replacedict(keys,dbport): +def instructions(keys,dbport): from utilities import getArtistInfo from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesArtistLink, keysToUrl, pickKeys, clean @@ -18,6 +18,7 @@ def replacedict(keys,dbport): info = getArtistInfo(topartist) imgurl = info.get("image") + pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else [] # get total amount of scrobbles response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/scrobbles?" + keysToUrl(timekeys,limitkeys)) @@ -44,5 +45,6 @@ def replacedict(keys,dbport): i += 1 html += "" - return {"KEY_TOPARTIST_IMAGEURL":imgurl,"KEY_SCROBBLES":str(scrobbles),"KEY_ARTISTLIST":html} + replace = {"KEY_TOPARTIST_IMAGEURL":imgurl,"KEY_SCROBBLES":str(scrobbles),"KEY_ARTISTLIST":html} + return (replace,pushresources) diff --git a/website/toptracks.py b/website/toptracks.py index 5887d32..e888360 100644 --- a/website/toptracks.py +++ b/website/toptracks.py @@ -2,7 +2,7 @@ import urllib import json -def replacedict(keys,dbport): +def instructions(keys,dbport): from utilities import getArtistInfo, getTrackInfo from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesTrackLink, keysToUrl, pickKeys, clean @@ -26,6 +26,8 @@ def replacedict(keys,dbport): #topartist = charts[0]["track"]["artists"][0] #for now info = getTrackInfo(charts[0]["track"]["artists"],charts[0]["track"]["title"]) imgurl = info.get("image") + + pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else [] @@ -52,5 +54,7 @@ def replacedict(keys,dbport): i += 1 html += "" - return {"KEY_TOPARTIST_IMAGEURL":imgurl,"KEY_SCROBBLES":str(scrobbles),"KEY_TRACKLIST":html,"KEY_LIMITS":limitstring} + replace = {"KEY_TOPARTIST_IMAGEURL":imgurl,"KEY_SCROBBLES":str(scrobbles),"KEY_TRACKLIST":html,"KEY_LIMITS":limitstring} + + return (replace,pushresources) diff --git a/website/track.py b/website/track.py index 126282f..9728e1a 100644 --- a/website/track.py +++ b/website/track.py @@ -2,7 +2,7 @@ import urllib import json -def replacedict(keys,dbport): +def instructions(keys,dbport): from utilities import getArtistInfo, getTrackInfo from htmlgenerators import clean, artistLink, artistLinks, trackLink, scrobblesTrackLink, keysToUrl, pickKeys, getTimeDesc @@ -10,7 +10,7 @@ def replacedict(keys,dbport): limitkeys = pickKeys(keys,"artist","title") info = getTrackInfo(keys.getall("artist"),keys.get("title")) imgurl = info.get("image") - desc = info.get("info") + 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()) @@ -34,4 +34,7 @@ def replacedict(keys,dbport): html += "" - return {"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} + 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} + + return (replace,pushresources)