From 1a3a172089ed6720fdf219f7895be14ace305862 Mon Sep 17 00:00:00 2001 From: Krateng Date: Wed, 9 Jan 2019 23:29:01 +0100 Subject: [PATCH] Various Fixes --- cleanup.py | 2 + database.py | 3 +- server.py | 3 +- utilities.py | 106 ++++++++----------------------------------- website/scrobbles.py | 3 +- 5 files changed, 26 insertions(+), 91 deletions(-) diff --git a/cleanup.py b/cleanup.py index 83b8e4d..2527f2c 100644 --- a/cleanup.py +++ b/cleanup.py @@ -2,6 +2,8 @@ import re import utilities # need to do this as a class so it can retain loaded settings from file +# apparently this is not true +# I'm dumb class CleanerAgent: def __init__(self): diff --git a/database.py b/database.py index e998b36..6d73e31 100644 --- a/database.py +++ b/database.py @@ -519,6 +519,7 @@ def issues(): @dbserver.post("/rebuild") def rebuild(): + keys = FormsDict.decode(request.forms) apikey = keys.pop("key",None) if (checkAPIkey(apikey)): global db_rulestate @@ -612,7 +613,7 @@ def sync(): global lastsync lastsync = int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp()) - print("Database saved to disk.") + log("Database saved to disk.") diff --git a/server.py b/server.py index c794b7c..6540f08 100755 --- a/server.py +++ b/server.py @@ -3,6 +3,7 @@ from bottle import Bottle, route, get, post, error, run, template, static_file, request, response, FormsDict from importlib.machinery import SourceFileLoader from htmlgenerators import removeIdentical +from utilities import * import _thread import waitress import urllib.request @@ -68,7 +69,7 @@ def shutdown(): def graceful_exit(sig=None,frame=None): urllib.request.urlopen("http://localhost:" + str(DATABASE_PORT) + "/sync") - print("Server shutting down...") + log("Server shutting down...") sys.exit() diff --git a/utilities.py b/utilities.py index dac7f49..1582640 100644 --- a/utilities.py +++ b/utilities.py @@ -138,7 +138,10 @@ def addEntries(filename,al): ### Logging def log(msg): - print(msg) + import inspect + module = inspect.getmodule(inspect.stack()[1][0]).__name__ + if module == "__main__": module = "mainserver" + print("[" + module + "] " + msg) # best function ever @@ -178,16 +181,17 @@ def apirequest(artists=None,artist=None,title=None): except: pass - # if nothing worked, just return the artist image if len(artists) == 1: - return {"image":apirequest(artist=artists[0])["image"]} - #return {"image":None,"desc":None} + #return {"image":apirequest(artist=artists[0])["image"]} + return {"image":None} # try the same track with every single artist for a in artists: rec = apirequest(artists=[a],title=title) if rec["image"] is not None: return rec + + return {"image":None} # ARTISTS else: @@ -202,6 +206,7 @@ def apirequest(artists=None,artist=None,title=None): return {"image":""} +# I think I've only just understood modules cachedTracks = {} cachedArtists = {} @@ -231,33 +236,13 @@ def getTrackInfo(artists,title): pass result = apirequest(artists=artists,title=title) - cachedTracks[(frozenset(artists),title)] = result["image"] - return result - -# with open("apikey","r") as keyfile: -# apikey = keyfile.read().replace("\n","") -# -# -# for a in artists: -# try: -# url = "https://ws.audioscrobbler.com/2.0/?method=track.getinfo&track=" + urllib.parse.quote(title) + "&artist=" + urllib.parse.quote(a) + "&api_key=" + apikey + "&format=json" -# response = urllib.request.urlopen(url) -# lastfm_data = json.loads(response.read()) -# imgurl = lastfm_data["track"]["album"]["image"][2]["#text"] -# break -# except: -# pass -# -# try: -# return {"image":imgurl} -# except: -# for a in artists: -# try: -# return {"image":getArtistInfo(a)["image"]} -# except: -# pass -# -# return {"image":""} + if result.get("image") is not None: + cachedTracks[(frozenset(artists),title)] = result["image"] + return result + else: + result = getArtistInfo(artist=artists[0]) + cachedTracks[(frozenset(artists),title)] = result["image"] + return result def getArtistInfo(artist): import re @@ -280,25 +265,7 @@ def getArtistInfo(artist): imgurl = "/" + filepath + ".jpeg" return {"image":imgurl} - #check if cached image exists -# elif os.path.exists(filepath_cache + ".png"): -# imgurl = "/" + filepath_cache + ".png" -# elif os.path.exists(filepath_cache + ".jpg"): -# imgurl = "/" + filepath_cache + ".jpg" -# elif os.path.exists(filepath_cache + ".jpeg"): -# imgurl = "/" + filepath_cache + ".jpeg" - - - # check if custom desc exists -# if os.path.exists(filepath + ".txt"): -# with open(filepath + ".txt","r") as descfile: -# desc = descfile.read().replace("\n","") -# -# #check if cached desc exists -# elif os.path.exists(filepath_cache + ".txt"): -# with open(filepath_cache + ".txt","r") as descfile: -# desc = descfile.read().replace("\n","") - + try: return {"image":cachedArtists[artist]} except: @@ -307,43 +274,6 @@ def getArtistInfo(artist): result = apirequest(artist=artist) cachedArtists[artist] = result["image"] return result - -# # if we neither have a custom image nor a cached version, we return the address from lastfm, but cache that image for later use -# with open("apikey","r") as keyfile: -# apikey = keyfile.read().replace("\n","") -# -# -# try: -# url = "https://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=" + urllib.parse.quote(artist) + "&api_key=" + apikey + "&format=json" -# response = urllib.request.urlopen(url) -# lastfm_data = json.loads(response.read()) -# try: -# imgurl -# except NameError: -# imgurl = lastfm_data["artist"]["image"][2]["#text"] -# if imgurl == "": -# imgurl = "/info/artists/default.jpg" -# else: -# _thread.start_new_thread(cacheImage,(imgurl,"info/artists_cache",filename)) -# try: -# desc -# except NameError: -# desc = lastfm_data["artist"]["bio"]["summary"].split("(1) ")[-1] -# with open(filepath_cache + ".txt","w") as descfile: -# descfile.write(desc) -# # this feels so dirty -# -# -# return {"image":imgurl,"info":desc} -# except: -# return {"image":"/info/artists/default.jpg","info":"No information available"} - - - -#def cacheImage(url,path,filename): -# import urllib.request -# response = urllib.request.urlopen(url) -# target = path + "/" + filename + "." + response.info().get_content_subtype() -# urllib.request.urlretrieve(url,target) + diff --git a/website/scrobbles.py b/website/scrobbles.py index 2679915..2076d31 100644 --- a/website/scrobbles.py +++ b/website/scrobbles.py @@ -37,7 +37,8 @@ def replacedict(keys,dbport): elif keys.get("artist") is not None: imgurl = getArtistInfo(keys.get("artist")).get("image") elif (len(scrobbles) != 0): - imgurl = getArtistInfo(scrobbles[0]["artists"][0]).get("image") + imgurl = getTrackInfo(scrobbles[0]["artists"],scrobbles[0]["title"]).get("image") + #imgurl = getArtistInfo(scrobbles[0]["artists"][0]).get("image") else: imgurl = ""