diff --git a/maloja/server.py b/maloja/server.py index c08b29f..4063dec 100644 --- a/maloja/server.py +++ b/maloja/server.py @@ -18,7 +18,7 @@ from doreah import auth # rest of the project from . import database -from .utilities import resolveImage +from .utilities import get_track_image, get_artist_image from .malojauri import uri_to_internal, remove_identical from .globalconf import malojaconfig, apikeystore, data_dir from .jinjaenv.context import jinja_environment @@ -158,8 +158,12 @@ def deprecated_api(pth): def dynamic_image(): keys = FormsDict.decode(request.query) relevant, _, _, _, _ = uri_to_internal(keys) - result = resolveImage(**relevant) - if result == "": return "" + if 'track' in relevant: + result = get_track_image(relevant['track']) + elif 'artist' in relevant: + result = get_artist_image(relevant['artist']) + + if result is None: return "" redirect(result,307) @webserver.route("/images/") diff --git a/maloja/utilities/images.py b/maloja/utilities/images.py index 0227225..39f1bc8 100644 --- a/maloja/utilities/images.py +++ b/maloja/utilities/images.py @@ -1,5 +1,6 @@ from ..globalconf import data_dir, malojaconfig from .. import thirdparty +from .. import database from doreah import caching from doreah.logging import log @@ -13,10 +14,124 @@ from threading import Thread, Timer import re import datetime +import sqlalchemy as sql +DB = {} +engine = sql.create_engine(f"sqlite:///{data_dir['cache']('images.sqlite')}", echo = False) +meta = sql.MetaData() + +DB['artists'] = sql.Table( + 'artists', meta, + sql.Column('id',sql.Integer,primary_key=True), + sql.Column('url',sql.String), + sql.Column('expire',sql.Integer) +) +DB['tracks'] = sql.Table( + 'tracks', meta, + sql.Column('id',sql.Integer,primary_key=True), + sql.Column('url',sql.String), + sql.Column('expire',sql.Integer) +) + +meta.create_all(engine) + +def get_image_from_cache(id,table): + now = int(datetime.datetime.now().timestamp()) + with engine.begin() as conn: + op = DB[table].select( + DB[table].c.url + ).where( + DB[table].c.id==id, + DB[table].c.expire>now + ) + result = conn.execute(op).all() + for row in result: + return row.url + +def set_image_in_cache(id,table,url): + now = int(datetime.datetime.now().timestamp()) + if url is None: + expire = now + (malojaconfig["CACHE_EXPIRE_NEGATIVE"] * 24 * 3600) + else: + expire = now + (malojaconfig["CACHE_EXPIRE_POSITIVE"] * 24 * 3600) + with engine.begin() as conn: + op = DB[table].insert().values( + id=id, + url=url, + expire=expire + ).prefix_with('OR IGNORE') + result = conn.execute(op) + + +def get_track_image(track=None,track_id=None,fast=False): + + if track_id is None: + track_id = database.sqldb.get_track_id(track) + title = track['title'] + artists = track['artists'] + + # check cache + result = get_image_from_cache(track_id,'tracks') + if result is not None: + return result + + # local image + if malojaconfig["USE_LOCAL_IMAGES"]: + images = local_files(artists=artists,title=title) + if len(images) != 0: + result = random.choice(images) + result = urllib.parse.quote(result) + set_image_in_cache(track_id,'tracks',result) + return result + + # forward + if fast: + titlequery = "title=" + urllib.parse.quote(title) + artistquery = "&".join("artist=" + urllib.parse.quote(a) for a in artists) + return (f"/image?{titlequery}&{artistquery}") + + # third party + result = thirdparty.get_image_track_all((artists,title)) + set_image_in_cache(track_id,'tracks',result) + if result is not None: return result + for a in artists: + res = get_artist_image(artist=a,fast=False) + if res != "": return res + return None + + +def get_artist_image(artist=None,artist_id=None,fast=False): + + if artist_id is None: + artist_id = database.sqldb.get_artist_id(artist) + + # check cache + result = get_image_from_cache(artist_id,'artists') + if result is not None: + return result + + # local image + if malojaconfig["USE_LOCAL_IMAGES"]: + images = local_files(artist=artist) + if len(images) != 0: + result = random.choice(images) + result = urllib.parse.quote(result) + set_image_in_cache(artist_id,'artists',result) + return result + + # forward + if fast: + artistquery = "artist=" + urllib.parse.quote(artist) + return (f"/image?{artistquery}") + + # third party + result = thirdparty.get_image_artist_all(artist) + set_image_in_cache(artist_id,'artists',result) + return result + ### Caches cacheage = malojaconfig["CACHE_EXPIRE_POSITIVE"] * 24 * 3600 diff --git a/maloja/web/jinja/artist.jinja b/maloja/web/jinja/artist.jinja index 9aa0c4a..5c03d41 100644 --- a/maloja/web/jinja/artist.jinja +++ b/maloja/web/jinja/artist.jinja @@ -39,10 +39,10 @@ {% if adminmode %}
{% else %} -
+
{% endif %} diff --git a/maloja/web/jinja/charts_artists.jinja b/maloja/web/jinja/charts_artists.jinja index c5b6a13..3b98975 100644 --- a/maloja/web/jinja/charts_artists.jinja +++ b/maloja/web/jinja/charts_artists.jinja @@ -9,7 +9,7 @@ {% set pages = math.ceil(charts.__len__() / amountkeys.perpage) %} {% if charts[0] is defined %} {% set topartist = charts[0].artist %} - {% set img = utilities.getArtistImage(artist=topartist,fast=True) %} + {% set img = utilities.get_artist_image(topartist,fast=True) %} {% else %} {% set img = "/favicon.png" %} {% endif %} diff --git a/maloja/web/jinja/charts_tracks.jinja b/maloja/web/jinja/charts_tracks.jinja index e4779f7..0208c66 100644 --- a/maloja/web/jinja/charts_tracks.jinja +++ b/maloja/web/jinja/charts_tracks.jinja @@ -11,7 +11,7 @@ {% set pages = math.ceil(charts.__len__() / amountkeys.perpage) %} {% if charts[0] is defined %} {% set toptrack = charts[0].track %} - {% set img = utilities.getTrackImage(artists=toptrack.artists,title=toptrack.title,fast=True) %} + {% set img = utilities.get_track_image(toptrack,fast=True) %} {% else %} {% set img = "/favicon.png" %} {% endif %} diff --git a/maloja/web/jinja/partials/charts_artists_tiles.jinja b/maloja/web/jinja/partials/charts_artists_tiles.jinja index bb613e6..f026867 100644 --- a/maloja/web/jinja/partials/charts_artists_tiles.jinja +++ b/maloja/web/jinja/partials/charts_artists_tiles.jinja @@ -23,7 +23,7 @@ {% set rank = entry.rank %} -
+
#{{ rank }} {{ artist }}
diff --git a/maloja/web/jinja/partials/charts_tracks_tiles.jinja b/maloja/web/jinja/partials/charts_tracks_tiles.jinja index ef110c4..2f93b63 100644 --- a/maloja/web/jinja/partials/charts_tracks_tiles.jinja +++ b/maloja/web/jinja/partials/charts_tracks_tiles.jinja @@ -23,7 +23,7 @@ {% set rank = entry.rank %} -
+
#{{ rank }} {{ track.title }}
diff --git a/maloja/web/jinja/performance.jinja b/maloja/web/jinja/performance.jinja index a30eb7d..6a0ea8b 100644 --- a/maloja/web/jinja/performance.jinja +++ b/maloja/web/jinja/performance.jinja @@ -4,9 +4,9 @@ {% import 'snippets/filterdescription.jinja' as filterdesc %} {% if filterkeys.get('track') is not none %} - {% set img = utilities.getTrackImage(artists=filterkeys.track.artists,title=filterkeys.track.title,fast=True) %} + {% set img = utilities.get_track_image(filterkeys.track,fast=True) %} {% elif filterkeys.get('artist') is not none %} - {% set img = utilities.getArtistImage(filterkeys.artist,fast=True) %} + {% set img = utilities.get_artist_image(filterkeys.artist,fast=True) %} {% else %} {% set img = "/favicon.png" %} {% endif %} diff --git a/maloja/web/jinja/pulse.jinja b/maloja/web/jinja/pulse.jinja index 08ac85f..f9d22dc 100644 --- a/maloja/web/jinja/pulse.jinja +++ b/maloja/web/jinja/pulse.jinja @@ -4,9 +4,9 @@ {% import 'snippets/filterdescription.jinja' as filterdesc %} {% if filterkeys.get('track') is not none %} - {% set img = utilities.getTrackImage(artists=filterkeys.track.artists,title=filterkeys.track.title,fast=True) %} + {% set img = utilities.get_track_image(filterkeys.track,fast=True) %} {% elif filterkeys.get('artist') is not none %} - {% set img = utilities.getArtistImage(filterkeys.artist,fast=True) %} + {% set img = utilities.get_artist_image(filterkeys.artist,fast=True) %} {% else %} {% set img = "/favicon.png" %} {% endif %} diff --git a/maloja/web/jinja/scrobbles.jinja b/maloja/web/jinja/scrobbles.jinja index 814cde6..b7b2819 100644 --- a/maloja/web/jinja/scrobbles.jinja +++ b/maloja/web/jinja/scrobbles.jinja @@ -8,11 +8,11 @@ {% set pages = math.ceil(scrobbles.__len__() / amountkeys.perpage) %} {% if filterkeys.get('track') is not none %} - {% set img = utilities.getTrackImage(artists=filterkeys.track.artists,title=filterkeys.track.title,fast=True) %} + {% set img = utilities.get_track_image(filterkeys.track,fast=True) %} {% elif filterkeys.get('artist') is not none %} - {% set img = utilities.getArtistImage(filterkeys.artist,fast=True) %} + {% set img = utilities.get_artist_image(filterkeys.artist,fast=True) %} {% elif scrobbles.__len__() > 0 %} - {% set img = utilities.getTrackImage(artists=scrobbles[0].track.artists,title=scrobbles[0].track.title,fast=True) %} + {% set img = utilities.get_track_image(scrobbles[0].track,fast=True) %} {% else %} {% set img = "/favicon.png" %} {% endif %} diff --git a/maloja/web/jinja/snippets/entityrow.jinja b/maloja/web/jinja/snippets/entityrow.jinja index fb2668f..5b35d15 100644 --- a/maloja/web/jinja/snippets/entityrow.jinja +++ b/maloja/web/jinja/snippets/entityrow.jinja @@ -3,9 +3,9 @@ {% import 'snippets/links.jinja' as links %} {% if 'artists' in entity %} - {% set img = utilities.getTrackImage(artists=entity.artists,title=entity.title,fast=True) %} + {% set img = utilities.get_track_image(entity,fast=True) %} {% else %} - {% set img = utilities.getArtistImage(entity,fast=True) %} + {% set img = utilities.get_artist_image(entity,fast=True) %} {% endif %}
diff --git a/maloja/web/jinja/top_artists.jinja b/maloja/web/jinja/top_artists.jinja index bb0c3d3..bc78203 100644 --- a/maloja/web/jinja/top_artists.jinja +++ b/maloja/web/jinja/top_artists.jinja @@ -6,7 +6,7 @@ {% set entries = dbp.get_top_artists(limitkeys,delimitkeys) %} {% set repr = entries | find_representative('artist','scrobbles') %} -{% set img = "/favicon.png" if repr is none else utilities.getArtistImage(repr.artist) %} +{% set img = "/favicon.png" if repr is none else utilities.get_artist_image(repr.artist) %} {% block content %} diff --git a/maloja/web/jinja/top_tracks.jinja b/maloja/web/jinja/top_tracks.jinja index cdc66cd..99e68a9 100644 --- a/maloja/web/jinja/top_tracks.jinja +++ b/maloja/web/jinja/top_tracks.jinja @@ -6,7 +6,7 @@ {% set entries = dbp.get_top_tracks(filterkeys,limitkeys,delimitkeys) %} {% set repr = entries | find_representative('track','scrobbles') %} -{% set img = "/favicon.png" if repr is none else utilities.getTrackImage(repr.track.artists,repr.track.title) %} +{% set img = "/favicon.png" if repr is none else utilities.get_track_image(repr.track) %} {% block content %} diff --git a/maloja/web/jinja/track.jinja b/maloja/web/jinja/track.jinja index 50abd59..2b586a6 100644 --- a/maloja/web/jinja/track.jinja +++ b/maloja/web/jinja/track.jinja @@ -33,10 +33,10 @@ {% if adminmode %}
{% else %} -
+
{% endif %}