1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Added setting for local image rotation

This commit is contained in:
Krateng 2019-04-03 16:43:09 +02:00
parent a7963ee679
commit c67aced296
2 changed files with 36 additions and 18 deletions

View File

@ -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_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 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

View File

@ -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): def getTrackImage(artists,title,fast=False):
obj = (frozenset(artists),title) # 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) # 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)) # if filename == "": filename = str(hash(obj))
filepath = "images/tracks/" + filename # filepath = "images/tracks/" + filename
try:
return local_track_cache.get((frozenset(artists),title))
except:
images = local_files(artists=artists,title=title) images = local_files(artists=artists,title=title)
if len(images) != 0: if len(images) != 0:
#return random.choice(images) #return random.choice(images)
return urllib.parse.quote(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 # check if custom image exists
@ -308,19 +317,24 @@ def getTrackImage(artists,title,fast=False):
return "" return ""
def getArtistImage(artist,fast=False): def getArtistImage(artist,fast=False):
obj = artist # obj = artist
filename = re.sub("[^a-zA-Z0-9]","",artist) # filename = re.sub("[^a-zA-Z0-9]","",artist)
if filename == "": filename = str(hash(obj)) # if filename == "": filename = str(hash(obj))
filepath = "images/artists/" + filename # filepath = "images/artists/" + filename
#filepath_cache = "info/artists_cache/" + filename # #filepath_cache = "info/artists_cache/" + filename
try:
return local_artist_cache.get(artist)
except:
images = local_files(artist=artist) images = local_files(artist=artist)
if len(images) != 0: if len(images) != 0:
#return random.choice(images) #return random.choice(images)
return urllib.parse.quote(random.choice(images)) res = random.choice(images)
local_artist_cache.add(artist,res)
return urllib.parse.quote(res)
# check if custom image exists # check if custom image exists
# if os.path.exists(filepath + ".png"): # if os.path.exists(filepath + ".png"):