mirror of
https://github.com/krateng/maloja.git
synced 2023-08-10 21:12:55 +03:00
Minor cleanup of image functions
This commit is contained in:
parent
a52c494e4b
commit
59eaa2264a
@ -219,15 +219,9 @@ def login():
|
|||||||
return auth.get_login_page()
|
return auth.get_login_page()
|
||||||
|
|
||||||
@webserver.route("/<name>.<ext>")
|
@webserver.route("/<name>.<ext>")
|
||||||
def static(name,ext):
|
|
||||||
assert ext in ["txt","ico","jpeg","jpg","png","less","js"]
|
|
||||||
response = static_file(ext + "/" + name + "." + ext,root=STATICFOLDER)
|
|
||||||
response.set_header("Cache-Control", "public, max-age=3600")
|
|
||||||
return response
|
|
||||||
|
|
||||||
@webserver.route("/media/<name>.<ext>")
|
@webserver.route("/media/<name>.<ext>")
|
||||||
def static(name,ext):
|
def static(name,ext):
|
||||||
assert ext in ["ico","jpeg","jpg","png"]
|
assert ext in ["txt","ico","jpeg","jpg","png","less","js"]
|
||||||
response = static_file(ext + "/" + name + "." + ext,root=STATICFOLDER)
|
response = static_file(ext + "/" + name + "." + ext,root=STATICFOLDER)
|
||||||
response.set_header("Cache-Control", "public, max-age=3600")
|
response.set_header("Cache-Control", "public, max-age=3600")
|
||||||
return response
|
return response
|
||||||
@ -319,13 +313,12 @@ signal.signal(signal.SIGTERM, graceful_exit)
|
|||||||
|
|
||||||
|
|
||||||
def run_server():
|
def run_server():
|
||||||
|
|
||||||
Thread(target=database.start_db).start()
|
|
||||||
## start database
|
|
||||||
|
|
||||||
|
|
||||||
log("Starting up Maloja server...")
|
log("Starting up Maloja server...")
|
||||||
|
|
||||||
|
## start database
|
||||||
|
Thread(target=database.start_db).start()
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
#run(webserver, host=HOST, port=MAIN_PORT, server='waitress')
|
#run(webserver, host=HOST, port=MAIN_PORT, server='waitress')
|
||||||
waitress.serve(webserver, host=HOST, port=MAIN_PORT, threads=THREADS)
|
waitress.serve(webserver, host=HOST, port=MAIN_PORT, threads=THREADS)
|
||||||
|
@ -139,26 +139,27 @@ local_track_cache = caching.Cache(maxage=local_cache_age)
|
|||||||
|
|
||||||
def getTrackImage(artists,title,fast=False):
|
def getTrackImage(artists,title,fast=False):
|
||||||
|
|
||||||
if settings.get_settings("USE_LOCAL_IMAGES"):
|
hashable_track = (frozenset(artists),title)
|
||||||
|
|
||||||
|
# Prio 1: Local image
|
||||||
|
if settings.get_settings("USE_LOCAL_IMAGES"):
|
||||||
try:
|
try:
|
||||||
return local_track_cache.get((frozenset(artists),title))
|
return thumborize(local_track_cache.get(hashable_track))
|
||||||
except:
|
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)
|
|
||||||
res = random.choice(images)
|
res = random.choice(images)
|
||||||
local_track_cache.add((frozenset(artists),title),res)
|
local_track_cache.add(hashable_track,res)
|
||||||
return urllib.parse.quote(res)
|
return urllib.parse.quote(res)
|
||||||
|
|
||||||
|
|
||||||
|
# Prio 2: Cached remote link
|
||||||
try:
|
try:
|
||||||
# check our cache
|
result = track_cache.get(hashable_track)
|
||||||
# if we have cached the nonexistence of that image, we immediately return the redirect to the artist and let the resolver handle it
|
if result is not None: return thumborize(result)
|
||||||
|
# if we have cached the nonexistence of that image, we immediately return
|
||||||
|
# the redirect to the artist and let the resolver handle it
|
||||||
# (even if we're not in a fast lookup right now)
|
# (even if we're not in a fast lookup right now)
|
||||||
#result = cachedTracks[(frozenset(artists),title)]
|
|
||||||
result = track_cache.get((frozenset(artists),title)) #track_from_cache(artists,title)
|
|
||||||
if result is not None: return result
|
|
||||||
for a in artists:
|
for a in artists:
|
||||||
res = getArtistImage(artist=a,fast=True)
|
res = getArtistImage(artist=a,fast=True)
|
||||||
if res != "": return res
|
if res != "": return res
|
||||||
@ -166,25 +167,19 @@ def getTrackImage(artists,title,fast=False):
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# do we have an api key?
|
|
||||||
# apikey = settings.get_settings("LASTFM_API_KEY")
|
|
||||||
# if apikey is None: return "" # DO NOT CACHE THAT
|
|
||||||
|
|
||||||
|
# fast request will not go further than this, but now generate redirect link
|
||||||
# fast request only retuns cached and local results, generates redirect link for rest
|
|
||||||
if fast:
|
if fast:
|
||||||
return ("/image?title=" + urllib.parse.quote(title) + "&" + "&".join(
|
return ("/image?title=" + urllib.parse.quote(title) + "&" + "&".join(
|
||||||
"artist=" + urllib.parse.quote(a) for a in artists))
|
"artist=" + urllib.parse.quote(a) for a in artists))
|
||||||
|
|
||||||
# non-fast lookup (essentially only the resolver lookup)
|
|
||||||
|
# Prio 3 (only non-fast lookup): actually call third parties
|
||||||
result = thirdparty.get_image_track_all((artists,title))
|
result = thirdparty.get_image_track_all((artists,title))
|
||||||
|
|
||||||
# cache results (even negative ones)
|
# cache results (even negative ones)
|
||||||
#cachedTracks[(frozenset(artists),title)] = result
|
track_cache.add(hashable_track,result)
|
||||||
track_cache.add((frozenset(artists),title),result) #cache_track(artists,title,result)
|
|
||||||
|
|
||||||
# return either result or redirect to artist
|
# return either result or redirect to artist
|
||||||
if result is not None: return result
|
if result is not None: return thumborize(result)
|
||||||
for a in artists:
|
for a in artists:
|
||||||
res = getArtistImage(artist=a,fast=False)
|
res = getArtistImage(artist=a,fast=False)
|
||||||
if res != "": return res
|
if res != "": return res
|
||||||
@ -193,24 +188,20 @@ def getTrackImage(artists,title,fast=False):
|
|||||||
|
|
||||||
def getArtistImage(artist,fast=False):
|
def getArtistImage(artist,fast=False):
|
||||||
|
|
||||||
|
# Prio 1: Local image
|
||||||
if settings.get_settings("USE_LOCAL_IMAGES"):
|
if settings.get_settings("USE_LOCAL_IMAGES"):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return thumborize(local_artist_cache.get(artist))
|
return thumborize(local_artist_cache.get(artist))
|
||||||
# Local cached image
|
|
||||||
except:
|
except:
|
||||||
# Get all local images, select one if present
|
|
||||||
images = local_files(artist=artist)
|
images = local_files(artist=artist)
|
||||||
if len(images) != 0:
|
if len(images) != 0:
|
||||||
#return random.choice(images)
|
|
||||||
res = random.choice(images)
|
res = random.choice(images)
|
||||||
local_artist_cache.add(artist,res)
|
local_artist_cache.add(artist,res)
|
||||||
return thumborize(urllib.parse.quote(res))
|
return thumborize(urllib.parse.quote(res))
|
||||||
|
|
||||||
|
|
||||||
# if no local images (or setting to not use them)
|
# Prio 2: Cached remote link
|
||||||
try:
|
try:
|
||||||
# check cache for foreign image
|
|
||||||
result = artist_cache.get(artist)
|
result = artist_cache.get(artist)
|
||||||
if result is not None: return thumborize(result)
|
if result is not None: return thumborize(result)
|
||||||
else: return ""
|
else: return ""
|
||||||
@ -220,22 +211,14 @@ def getArtistImage(artist,fast=False):
|
|||||||
# no cache entry, go on
|
# no cache entry, go on
|
||||||
|
|
||||||
|
|
||||||
|
# fast request will not go further than this, but now generate redirect link
|
||||||
# do we have an api key?
|
|
||||||
# apikey = settings.get_settings("LASTFM_API_KEY")
|
|
||||||
# if apikey is None: return "" # DO NOT CACHE THAT
|
|
||||||
|
|
||||||
|
|
||||||
# fast request only retuns cached and local results, generates redirect link for rest
|
|
||||||
if fast: return "/image?artist=" + urllib.parse.quote(artist)
|
if fast: return "/image?artist=" + urllib.parse.quote(artist)
|
||||||
|
|
||||||
# non-fast lookup (essentially only the resolver lookup)
|
|
||||||
|
# Prio 3 (only non-fast lookup): actually call third parties
|
||||||
result = thirdparty.get_image_artist_all(artist)
|
result = thirdparty.get_image_artist_all(artist)
|
||||||
|
|
||||||
# cache results (even negative ones)
|
# cache results (even negative ones)
|
||||||
#cachedArtists[artist] = result
|
|
||||||
artist_cache.add(artist,result) #cache_artist(artist,result)
|
artist_cache.add(artist,result) #cache_artist(artist,result)
|
||||||
|
|
||||||
if result is not None: return thumborize(result)
|
if result is not None: return thumborize(result)
|
||||||
else: return ""
|
else: return ""
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user