extract original thumbnail image

This commit is contained in:
mungai-njoroge
2023-07-06 13:52:32 +03:00
parent 6ef3cc3545
commit 65d21d07da
10 changed files with 47 additions and 27 deletions
+7 -7
View File
@@ -2,21 +2,21 @@
Contains all the album routes. Contains all the album routes.
""" """
from dataclasses import asdict
import random import random
from dataclasses import asdict
from flask import Blueprint, request from flask import Blueprint, request
from app.db.sqlite.albums import SQLiteAlbumMethods as adb
from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb
from app.db.sqlite.lastfm.similar_artists import \
SQLiteLastFMSimilarArtists as lastfmdb
from app.models import FavType, Track from app.models import FavType, Track
from app.serializers.album import serialize_for_card
from app.serializers.track import track_serializer from app.serializers.track import track_serializer
from app.store.albums import AlbumStore from app.store.albums import AlbumStore
from app.store.tracks import TrackStore from app.store.tracks import TrackStore
from app.db.sqlite.albums import SQLiteAlbumMethods as adb
from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb
from app.db.sqlite.lastfm.similar_artists import SQLiteLastFMSimilarArtists as lastfmdb
from app.utils.hashing import create_hash from app.utils.hashing import create_hash
from app.serializers.album import serialize_for_card
from app.utils.remove_duplicates import remove_duplicates from app.utils.remove_duplicates import remove_duplicates
get_albums_by_albumartist = adb.get_albums_by_albumartist get_albums_by_albumartist = adb.get_albums_by_albumartist
@@ -210,4 +210,4 @@ def get_similar_albums():
except ValueError: except ValueError:
pass pass
return {"albums": [serialize_for_card(a) for a in albums]} return {"albums": [serialize_for_card(a) for a in albums[:limit]]}
-1
View File
@@ -321,7 +321,6 @@ def get_similar_artists(artisthash: str):
similar = ArtistStore.get_artists_by_hashes(result.get_artist_hash_set()) similar = ArtistStore.get_artists_by_hashes(result.get_artist_hash_set())
# print(similar)
if len(similar) > limit: if len(similar) > limit:
similar = random.sample(similar, limit) similar = random.sample(similar, limit)
+11
View File
@@ -22,6 +22,17 @@ def send_fallback_img(filename: str = "default.webp"):
return send_from_directory(path, filename) return send_from_directory(path, filename)
@api.route("/t/o/<imgpath>")
def send_original_thumbnail(imgpath: str):
path = Paths.get_original_thumb_path()
fpath = Path(path) / imgpath
if fpath.exists():
return send_from_directory(path, imgpath)
return send_fallback_img()
@api.route("/t/<imgpath>") @api.route("/t/<imgpath>")
def send_lg_thumbnail(imgpath: str): def send_lg_thumbnail(imgpath: str):
path = Paths.get_lg_thumb_path() path = Paths.get_lg_thumb_path()
-1
View File
@@ -241,7 +241,6 @@ def save_similar_artists(artist: Artist):
if len(artist_.similar_artist_hashes) == 0: if len(artist_.similar_artist_hashes) == 0:
return return
print(artist.artisthash, artist.name)
lastfmdb.insert_one(artist_) lastfmdb.insert_one(artist_)
-1
View File
@@ -140,7 +140,6 @@ def get_titles(items: _type):
text = item.og_title text = item.og_title
elif isinstance(item, models.Album): elif isinstance(item, models.Album):
text = item.title text = item.title
# print(text)
elif isinstance(item, models.Artist): elif isinstance(item, models.Artist):
text = item.name text = item.name
else: else:
+6 -4
View File
@@ -28,18 +28,20 @@ def extract_thumb(filepath: str, webp_path: str) -> bool:
""" """
Extracts the thumbnail from an audio file. Returns the path to the thumbnail. Extracts the thumbnail from an audio file. Returns the path to the thumbnail.
""" """
img_path = os.path.join(Paths.get_lg_thumb_path(), webp_path) original_img_path = os.path.join(Paths.get_original_thumb_path(), webp_path)
lg_img_path = os.path.join(Paths.get_lg_thumb_path(), webp_path)
sm_img_path = os.path.join(Paths.get_sm_thumb_path(), webp_path) sm_img_path = os.path.join(Paths.get_sm_thumb_path(), webp_path)
tsize = Defaults.THUMB_SIZE tsize = Defaults.THUMB_SIZE
sm_tsize = Defaults.SM_THUMB_SIZE sm_tsize = Defaults.SM_THUMB_SIZE
def save_image(img: Image.Image): def save_image(img: Image.Image):
img.save(original_img_path, "webp")
img.resize((tsize, tsize), Image.ANTIALIAS).save(lg_img_path, "webp")
img.resize((sm_tsize, sm_tsize), Image.ANTIALIAS).save(sm_img_path, "webp") img.resize((sm_tsize, sm_tsize), Image.ANTIALIAS).save(sm_img_path, "webp")
img.resize((tsize, tsize), Image.ANTIALIAS).save(img_path, "webp")
if os.path.exists(img_path): if os.path.exists(lg_img_path):
img_size = os.path.getsize(img_path) img_size = os.path.getsize(lg_img_path)
if img_size > 0: if img_size > 0:
return True return True
+2 -2
View File
@@ -6,7 +6,7 @@ import requests
from app import settings from app import settings
from app.utils.hashing import create_hash from app.utils.hashing import create_hash
from requests import ConnectionError, ReadTimeout from requests import ConnectionError, HTTPError, ReadTimeout
import urllib.parse import urllib.parse
@@ -19,7 +19,7 @@ def fetch_similar_artists(name: str):
try: try:
response = requests.get(url, timeout=10) response = requests.get(url, timeout=10)
response.raise_for_status() response.raise_for_status()
except (ConnectionError, ReadTimeout): except (ConnectionError, ReadTimeout, HTTPError):
return [] return []
data = response.json() data = response.json()
+2
View File
@@ -16,6 +16,8 @@ def track_serializer(track: Track, _remove: set = {}, retain_disc=False) -> dict
to_remove.union("disc", "track") to_remove.union("disc", "track")
to_remove.update(key for key in album_dict.keys() if key.startswith("is_")) to_remove.update(key for key in album_dict.keys() if key.startswith("is_"))
to_remove.remove('is_favorite')
for key in to_remove: for key in to_remove:
album_dict.pop(key, None) album_dict.pop(key, None)
+17 -11
View File
@@ -27,7 +27,9 @@ class Paths:
@classmethod @classmethod
def get_config_folder(cls): def get_config_folder(cls):
return "swingmusic" if cls.get_config_dir() != cls.USER_HOME_DIR else ".swingmusic" return (
"swingmusic" if cls.get_config_dir() != cls.USER_HOME_DIR else ".swingmusic"
)
@classmethod @classmethod
def get_app_dir(cls): def get_app_dir(cls):
@@ -65,6 +67,10 @@ class Paths:
def get_lg_thumb_path(cls): def get_lg_thumb_path(cls):
return join(cls.get_thumbs_path(), "large") return join(cls.get_thumbs_path(), "large")
@classmethod
def get_original_thumb_path(cls):
return join(cls.get_thumbs_path(), "original")
@classmethod @classmethod
def get_assets_path(cls): def get_assets_path(cls):
return join(Paths.get_app_dir(), "assets") return join(Paths.get_app_dir(), "assets")
@@ -73,8 +79,8 @@ class Paths:
# defaults # defaults
class Defaults: class Defaults:
THUMB_SIZE = 400 THUMB_SIZE = 400
SM_THUMB_SIZE = 64 SM_THUMB_SIZE = 144
SM_ARTIST_IMG_SIZE = 64 SM_ARTIST_IMG_SIZE = 144
""" """
The size of extracted images in pixels The size of extracted images in pixels
""" """
@@ -166,14 +172,14 @@ class FromFlags:
class ParserFlags(Enum): class ParserFlags(Enum):
EXTRACT_FEAT = 'EXTRACT_FEAT' EXTRACT_FEAT = "EXTRACT_FEAT"
REMOVE_PROD = 'REMOVE_PROD' REMOVE_PROD = "REMOVE_PROD"
CLEAN_ALBUM_TITLE = 'CLEAN_ALBUM_TITLE' CLEAN_ALBUM_TITLE = "CLEAN_ALBUM_TITLE"
SHOW_ALBUM_VERSION = 'SHOW_ALBUM_VERSION' SHOW_ALBUM_VERSION = "SHOW_ALBUM_VERSION"
REMOVE_REMASTER_FROM_TRACK = 'REMOVE_REMASTER_FROM_TRACK' REMOVE_REMASTER_FROM_TRACK = "REMOVE_REMASTER_FROM_TRACK"
DO_PERIODIC_SCANS = 'DO_PERIODIC_SCANS' DO_PERIODIC_SCANS = "DO_PERIODIC_SCANS"
PERIODIC_SCAN_INTERVAL = 'PERIODIC_SCAN_INTERVAL' PERIODIC_SCAN_INTERVAL = "PERIODIC_SCAN_INTERVAL"
MERGE_ALBUM_VERSIONS = 'MERGE_ALBUM_VERSIONS' MERGE_ALBUM_VERSIONS = "MERGE_ALBUM_VERSIONS"
def get_flag(flag: ParserFlags) -> bool: def get_flag(flag: ParserFlags) -> bool:
+2
View File
@@ -63,6 +63,7 @@ def create_config_dir() -> None:
thumb_path = os.path.join("images", "thumbnails") thumb_path = os.path.join("images", "thumbnails")
small_thumb_path = os.path.join(thumb_path, "small") small_thumb_path = os.path.join(thumb_path, "small")
large_thumb_path = os.path.join(thumb_path, "large") large_thumb_path = os.path.join(thumb_path, "large")
original_thumb_path = os.path.join(thumb_path, "original")
artist_img_path = os.path.join("images", "artists") artist_img_path = os.path.join("images", "artists")
small_artist_img_path = os.path.join(artist_img_path, "small") small_artist_img_path = os.path.join(artist_img_path, "small")
@@ -76,6 +77,7 @@ def create_config_dir() -> None:
thumb_path, thumb_path,
small_thumb_path, small_thumb_path,
large_thumb_path, large_thumb_path,
original_thumb_path,
artist_img_path, artist_img_path,
small_artist_img_path, small_artist_img_path,
large_artist_img_path, large_artist_img_path,