From c67aced29684fcf3ebc4cb5be94f260aa735e70d Mon Sep 17 00:00:00 2001 From: Krateng Date: Wed, 3 Apr 2019 16:43:09 +0200 Subject: [PATCH] Added setting for local image rotation --- settings/default.ini | 4 ++++ utilities.py | 50 ++++++++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/settings/default.ini b/settings/default.ini index 142c78a..cafa95d 100644 --- a/settings/default.ini +++ b/settings/default.ini @@ -11,3 +11,7 @@ LASTFM_API_KEY = "ASK" # "ASK" signifies that the user has not yet indicated to CACHE_EXPIRE_NEGATIVE = 30 # after how many days negative results should be tried again CACHE_EXPIRE_POSITIVE = 300 # after how many days positive results should be refreshed + +[Local Images] + +LOCAL_IMAGE_ROTATE = 3600 # when multiple images are present locally, how many seconds we wait between rotation diff --git a/utilities.py b/utilities.py index 74ee641..33c0ec6 100644 --- a/utilities.py +++ b/utilities.py @@ -240,18 +240,27 @@ def local_files(artist=None,artists=None,title=None): +# these caches are there so we don't check all files every thime, but return the same one +local_cache_age = settings.get_settings("LOCAL_IMAGE_ROTATE") +local_artist_cache = caching.Cache(maxage=local_cache_age) +local_track_cache = caching.Cache(maxage=local_cache_age) def getTrackImage(artists,title,fast=False): - obj = (frozenset(artists),title) - filename = "-".join([re.sub("[^a-zA-Z0-9]","",artist) for artist in sorted(artists)]) + "_" + re.sub("[^a-zA-Z0-9]","",title) - if filename == "": filename = str(hash(obj)) - filepath = "images/tracks/" + filename +# obj = (frozenset(artists),title) +# filename = "-".join([re.sub("[^a-zA-Z0-9]","",artist) for artist in sorted(artists)]) + "_" + re.sub("[^a-zA-Z0-9]","",title) +# if filename == "": filename = str(hash(obj)) +# filepath = "images/tracks/" + filename - images = local_files(artists=artists,title=title) - if len(images) != 0: - #return random.choice(images) - return urllib.parse.quote(random.choice(images)) + try: + return local_track_cache.get((frozenset(artists),title)) + except: + images = local_files(artists=artists,title=title) + if len(images) != 0: + #return random.choice(images) + res = random.choice(images) + local_track_cache.add((frozenset(artists),title),res) + return urllib.parse.quote(res) # check if custom image exists @@ -308,19 +317,24 @@ def getTrackImage(artists,title,fast=False): return "" - def getArtistImage(artist,fast=False): - obj = artist - filename = re.sub("[^a-zA-Z0-9]","",artist) - if filename == "": filename = str(hash(obj)) - filepath = "images/artists/" + filename - #filepath_cache = "info/artists_cache/" + filename +# obj = artist +# filename = re.sub("[^a-zA-Z0-9]","",artist) +# if filename == "": filename = str(hash(obj)) +# filepath = "images/artists/" + filename +# #filepath_cache = "info/artists_cache/" + filename + + try: + return local_artist_cache.get(artist) + except: + images = local_files(artist=artist) + if len(images) != 0: + #return random.choice(images) + res = random.choice(images) + local_artist_cache.add(artist,res) + return urllib.parse.quote(res) - images = local_files(artist=artist) - if len(images) != 0: - #return random.choice(images) - return urllib.parse.quote(random.choice(images)) # check if custom image exists # if os.path.exists(filepath + ".png"):