From 75a70a10b7577ded5b392f984595b531e3599b32 Mon Sep 17 00:00:00 2001 From: Krateng Date: Fri, 30 Nov 2018 13:39:12 +0100 Subject: [PATCH] Implemented scrobbling via POST --- database.py | 30 ++++++++++++++++++++++++---- scrobbler-vivaldi-plex/background.js | 4 ++-- server.py | 11 ++++++++-- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/database.py b/database.py index 0d808d5..6f5017d 100644 --- a/database.py +++ b/database.py @@ -1,4 +1,4 @@ -from bottle import route, run, template, static_file, request, response, FormsDict +from bottle import route, get, post, run, template, static_file, request, response, FormsDict from importlib.machinery import SourceFileLoader import urllib import waitress @@ -96,7 +96,8 @@ def get_scrobbles(): def get_tracks(): artist = request.query.get("artist") - artistid = ARTISTS.index(artist) + if artist is not None: + artistid = ARTISTS.index(artist) # Option 1 ls = [getTrackObject(t) for t in TRACKS if (artistid in t[0]) or (artistid==None)] @@ -122,8 +123,8 @@ def get_charts(): #results = db_query(since=since,to=to) #return {"list":results} -@route("/newscrobble") -def post_scrobble(): +@get("/newscrobble") +def pseudo_post_scrobble(): keys = FormsDict.decode(request.query) # The Dal★Shabet handler artists = keys.get("artist") title = keys.get("title") @@ -143,6 +144,27 @@ def post_scrobble(): return "" +@post("/newscrobble") +def post_scrobble(): + keys = FormsDict.decode(request.forms) # The Dal★Shabet handler + artists = keys.get("artist") + title = keys.get("title") + try: + time = int(keys.get("time")) + except: + time = int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp()) + (artists,title) = c.fullclean(artists,title) + + ## this is necessary for localhost testing + response.set_header("Access-Control-Allow-Origin","*") + + createScrobble(artists,title,time) + + if (time - lastsync) > 3600: + sync() + + return "" + @route("/sync") def abouttoshutdown(): sync() diff --git a/scrobbler-vivaldi-plex/background.js b/scrobbler-vivaldi-plex/background.js index 24faa14..c8814db 100644 --- a/scrobbler-vivaldi-plex/background.js +++ b/scrobbler-vivaldi-plex/background.js @@ -211,8 +211,8 @@ function scrobble(artist,title,seconds) { artiststring = encodeURIComponent(artist) titlestring = encodeURIComponent(title) var xhttp = new XMLHttpRequest(); - xhttp.open("GET","http://localhost:42010/db/newscrobble?artist=" + artiststring + "&title=" + titlestring + "&duration=" + seconds,true); - xhttp.send() + xhttp.open("POST","http://localhost:42010/db/newscrobble",true); + xhttp.send("artist=" + artiststring + "&title=" + titlestring + "&duration=" + seconds) } function setUpdate() { diff --git a/server.py b/server.py index 8a7c8be..2b94f9c 100755 --- a/server.py +++ b/server.py @@ -1,4 +1,4 @@ -from bottle import route, run, template, static_file, request, response, FormsDict +from bottle import route, get, post, run, template, static_file, request, response, FormsDict from importlib.machinery import SourceFileLoader import _thread import waitress @@ -22,7 +22,7 @@ def mainpage(): # 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 # e.g. location /db { rewrite ^/db(.*)$ $1 break; proxy_pass http://yoururl:12349; } -@route("/db/") +@get("/db/") def database(pth): keys = FormsDict.decode(request.query) # The Dal★Shabet handler keystring = "?" @@ -33,6 +33,13 @@ def database(pth): response.set_header("Access-Control-Allow-Origin","*") #print("Returning " + "http://localhost:" + str(DATABASE_PORT) + "/" + pth) return contents + +@post("/db/") +def database(pth): + contents = urllib.request.urlopen("http://localhost:" + str(DATABASE_PORT) + "/" + pth,request.body).read() + response.content_type = "application/json" + response.set_header("Access-Control-Allow-Origin","*") + return contents @route("/exit") def shutdown():