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

Added thumbor support

This commit is contained in:
Krateng 2019-12-12 21:24:13 +01:00
parent 091c86a7ce
commit c0ecbfb28f
5 changed files with 40 additions and 8 deletions

View File

@ -7,7 +7,7 @@ author = {
"email":"maloja@krateng.dev",
"github": "krateng"
}
version = 2,1,3
version = 2,1,4
versionstr = ".".join(str(n) for n in version)

View File

@ -16,6 +16,8 @@ SPOTIFY_API_ID = "ASK"
SPOTIFY_API_SECRET = "ASK"
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
THUMBOR_SERVER = None
THUMBOR_SECRET = ""
# Can be 'YouTube', 'YouTube Music', 'Google Play Music', 'Spotify', 'Tidal', 'SoundCloud', 'Deezer', 'Amazon Music'
# Omit or set to none to disable

13
maloja/globalconf.py Normal file
View File

@ -0,0 +1,13 @@
from doreah.settings import get_settings
THUMBOR_SERVER, THUMBOR_SECRET = get_settings("THUMBOR_SERVER","THUMBOR_SECRET")
try:
USE_THUMBOR = THUMBOR_SERVER is not None and THUMBOR_SECRET is not None
if USE_THUMBOR:
from libthumbor import CryptoURL
THUMBOR_GENERATOR = CryptoURL(key=THUMBOR_SECRET)
OWNURL = get_settings("PUBLIC_URL")
assert OWNURL is not None
except:
USE_THUMBOR = False
log("Thumbor could not be initialized. Is libthumbor installed?")

View File

@ -18,6 +18,7 @@ from . import utilities
from .utilities import resolveImage
from .urihandler import uri_to_internal, remove_identical
from . import urihandler
from . import globalconf
# doreah toolkit
from doreah import settings
from doreah.logging import log
@ -120,6 +121,10 @@ def dynamic_image():
@webserver.route("/images/<pth:re:.*\\.png>")
@webserver.route("/images/<pth:re:.*\\.gif>")
def static_image(pth):
if globalconf.USE_THUMBOR:
return static_file("images/" + pth,root="")
type = pth.split(".")[-1]
small_pth = pth + "-small"
if os.path.exists("images/" + small_pth):
response = static_file("images/" + small_pth,root="")
@ -141,6 +146,7 @@ def static_image(pth):
#response = static_file("images/" + pth,root="")
response.set_header("Cache-Control", "public, max-age=86400")
response.set_header("Content-Type", "image/" + type)
return response

View File

@ -16,6 +16,7 @@ from doreah.regular import yearly, daily
from .external import api_request_track, api_request_artist
from .__init__ import version
from . import globalconf
@ -126,10 +127,20 @@ def consistentRulestate(folder,checksums):
#####
if globalconf.USE_THUMBOR:
def thumborize(url):
if url.startswith("/"): url = globalconf.OWNURL + url
encrypted_url = globalconf.THUMBOR_GENERATOR.generate(
width=300,
height=300,
smart=True,
image_url=url
)
return globalconf.THUMBOR_SERVER + encrypted_url
else:
def thumborize(url):
return url
@ -297,7 +308,7 @@ def getArtistImage(artist,fast=False):
if settings.get_settings("USE_LOCAL_IMAGES"):
try:
return local_artist_cache.get(artist)
return thumborize(local_artist_cache.get(artist))
# Local cached image
except:
# Get all local images, select one if present
@ -306,14 +317,14 @@ def getArtistImage(artist,fast=False):
#return random.choice(images)
res = random.choice(images)
local_artist_cache.add(artist,res)
return urllib.parse.quote(res)
return thumborize(urllib.parse.quote(res))
# if no local images (or setting to not use them)
try:
# check cache for foreign image
result = artist_cache.get(artist)
if result is not None: return result
if result is not None: return thumborize(result)
else: return ""
# none means non-existence is cached, return empty
except:
@ -337,7 +348,7 @@ def getArtistImage(artist,fast=False):
#cachedArtists[artist] = result
artist_cache.add(artist,result) #cache_artist(artist,result)
if result is not None: return result
if result is not None: return thumborize(result)
else: return ""
def getTrackImages(trackobjectlist,fast=False):