mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-04 20:43:04 +00:00
fix duplicate artist and album color entry in db
+ Remove folder store + Reduce fuzzy search score cutoff from 90% to 75% + use inheritance to init Artist class + misc
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
from io import BytesIO
|
||||
from PIL import Image
|
||||
|
||||
from PIL import Image, UnidentifiedImageError
|
||||
import requests
|
||||
import urllib
|
||||
|
||||
@@ -55,7 +56,10 @@ class DownloadImage:
|
||||
"""
|
||||
Downloads the image from the url.
|
||||
"""
|
||||
return Image.open(BytesIO(requests.get(url, timeout=10).content))
|
||||
try:
|
||||
return Image.open(BytesIO(requests.get(url, timeout=10).content))
|
||||
except UnidentifiedImageError:
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def save_img(img: Image.Image, sm_path: Path, lg_path: Path):
|
||||
|
||||
+33
-35
@@ -12,16 +12,15 @@ from app import settings
|
||||
from app.db.sqlite.albums import SQLiteAlbumMethods as db
|
||||
from app.db.sqlite.artists import SQLiteArtistMethods as adb
|
||||
from app.db.sqlite.utils import SQLiteManager
|
||||
from app.models import Album, Artist
|
||||
|
||||
from app.store.artists import ArtistStore
|
||||
from app.store.albums import AlbumStore
|
||||
|
||||
|
||||
def get_image_colors(image: str) -> list[str]:
|
||||
"""Extracts 2 of the most dominant colors from an image."""
|
||||
def get_image_colors(image: str, count=1) -> list[str]:
|
||||
"""Extracts n number of the most dominant colors from an image."""
|
||||
try:
|
||||
colors = sorted(colorgram.extract(image, 1), key=lambda c: c.hsl.h)
|
||||
colors = sorted(colorgram.extract(image, count), key=lambda c: c.hsl.h)
|
||||
except OSError:
|
||||
return []
|
||||
|
||||
@@ -34,6 +33,16 @@ def get_image_colors(image: str) -> list[str]:
|
||||
return formatted_colors
|
||||
|
||||
|
||||
def process_color(item_hash: str, is_album=True):
|
||||
path = settings.Paths.SM_THUMB_PATH if is_album else settings.Paths.ARTIST_IMG_SM_PATH
|
||||
path = Path(path) / (item_hash + ".webp")
|
||||
|
||||
if not path.exists():
|
||||
return
|
||||
|
||||
return get_image_colors(str(path))
|
||||
|
||||
|
||||
class ProcessAlbumColors:
|
||||
"""
|
||||
Extracts the most dominant color from the album art and saves it to the database.
|
||||
@@ -44,26 +53,22 @@ class ProcessAlbumColors:
|
||||
|
||||
with SQLiteManager() as cur:
|
||||
for album in tqdm(albums, desc="Processing missing album colors"):
|
||||
colors = self.process_color(album)
|
||||
sql = "SELECT COUNT(1) FROM albums WHERE albumhash = ?"
|
||||
cur.execute(sql, (album.albumhash,))
|
||||
count = cur.fetchone()[0]
|
||||
|
||||
if count != 0:
|
||||
continue
|
||||
|
||||
colors = process_color(album.albumhash)
|
||||
|
||||
if colors is None:
|
||||
continue
|
||||
|
||||
album.set_colors(colors)
|
||||
|
||||
color_str = json.dumps(colors)
|
||||
db.insert_one_album(cur, album.albumhash, color_str)
|
||||
|
||||
@staticmethod
|
||||
def process_color(album: Album):
|
||||
path = Path(settings.Paths.SM_THUMB_PATH) / album.image
|
||||
|
||||
if not path.exists():
|
||||
return
|
||||
|
||||
colors = get_image_colors(str(path))
|
||||
return colors
|
||||
|
||||
|
||||
class ProcessArtistColors:
|
||||
"""
|
||||
@@ -73,27 +78,20 @@ class ProcessArtistColors:
|
||||
def __init__(self) -> None:
|
||||
all_artists = [a for a in ArtistStore.artists if len(a.colors) == 0]
|
||||
|
||||
for artist in tqdm(all_artists, desc="Processing missing artist colors"):
|
||||
self.process_color(artist)
|
||||
with SQLiteManager() as cur:
|
||||
for artist in tqdm(all_artists, desc="Processing missing artist colors"):
|
||||
sql = "SELECT COUNT(1) FROM artists WHERE artisthash = ?"
|
||||
|
||||
@staticmethod
|
||||
def process_color(artist: Artist):
|
||||
path = Path(settings.Paths.ARTIST_IMG_SM_PATH) / artist.image
|
||||
cur.execute(sql, (artist.artisthash,))
|
||||
count = cur.fetchone()[0]
|
||||
|
||||
if not path.exists():
|
||||
return
|
||||
if count != 0:
|
||||
continue
|
||||
|
||||
colors = get_image_colors(str(path))
|
||||
colors = process_color(artist.artisthash, is_album=False)
|
||||
|
||||
if len(colors) > 0:
|
||||
adb.insert_one_artist(artisthash=artist.artisthash, colors=colors)
|
||||
ArtistStore.map_artist_color((0, artist.artisthash, json.dumps(colors)))
|
||||
if colors is None:
|
||||
continue
|
||||
|
||||
# TODO: If item color is in db, get it, assign it to the item and continue.
|
||||
# - Format all colors in the format: rgb(123, 123, 123)
|
||||
# - Each digit should be 3 digits long.
|
||||
# - Format all db colors into a master string of the format "-itemhash:colorhash-"
|
||||
# - Find the item hash using index() and get the color using the index + number, where number
|
||||
# is the length of the rgb string + 1
|
||||
# - Assign the color to the item and continue.
|
||||
# - If the color is not in the db, extract it and add it to the db.
|
||||
artist.set_colors(colors)
|
||||
adb.insert_one_artist(cur, artist.artisthash, colors)
|
||||
|
||||
@@ -12,7 +12,6 @@ from app.logger import log
|
||||
from app.models import Album, Artist, Track
|
||||
from app.utils.filesystem import run_fast_scandir
|
||||
|
||||
from app.store.folder import FolderStore
|
||||
from app.store.albums import AlbumStore
|
||||
from app.store.tracks import TrackStore
|
||||
from app.store.artists import ArtistStore
|
||||
@@ -102,7 +101,6 @@ class Populate:
|
||||
track.is_favorite = track.trackhash in fav_tracks
|
||||
|
||||
TrackStore.add_track(track)
|
||||
FolderStore.add_folder(track.folder)
|
||||
|
||||
if not AlbumStore.album_exists(track.albumhash):
|
||||
AlbumStore.add_album(AlbumStore.create_album(track))
|
||||
|
||||
@@ -23,10 +23,10 @@ class Cutoff:
|
||||
Holds all the default cutoff values.
|
||||
"""
|
||||
|
||||
tracks: int = 90
|
||||
albums: int = 90
|
||||
artists: int = 90
|
||||
playlists: int = 90
|
||||
tracks: int = 75
|
||||
albums: int = 75
|
||||
artists: int = 75
|
||||
playlists: int = 75
|
||||
|
||||
|
||||
class Limit:
|
||||
@@ -54,7 +54,6 @@ class SearchTracks:
|
||||
results = process.extract(
|
||||
self.query,
|
||||
track_titles,
|
||||
scorer=fuzz.WRatio,
|
||||
score_cutoff=Cutoff.tracks,
|
||||
limit=Limit.tracks,
|
||||
)
|
||||
@@ -77,7 +76,6 @@ class SearchArtists:
|
||||
results = process.extract(
|
||||
self.query,
|
||||
artists,
|
||||
scorer=fuzz.WRatio,
|
||||
score_cutoff=Cutoff.artists,
|
||||
limit=Limit.artists,
|
||||
)
|
||||
@@ -100,7 +98,6 @@ class SearchAlbums:
|
||||
results = process.extract(
|
||||
self.query,
|
||||
albums,
|
||||
scorer=fuzz.WRatio,
|
||||
score_cutoff=Cutoff.albums,
|
||||
limit=Limit.albums,
|
||||
)
|
||||
@@ -125,7 +122,6 @@ class SearchPlaylists:
|
||||
results = process.extract(
|
||||
self.query,
|
||||
playlists,
|
||||
scorer=fuzz.WRatio,
|
||||
score_cutoff=Cutoff.playlists,
|
||||
limit=Limit.playlists,
|
||||
)
|
||||
@@ -176,7 +172,6 @@ class SearchAll:
|
||||
results = process.extract(
|
||||
query=query,
|
||||
choices=items,
|
||||
scorer=fuzz.WRatio,
|
||||
score_cutoff=Cutoff.tracks,
|
||||
limit=20
|
||||
)
|
||||
|
||||
@@ -17,7 +17,6 @@ from app.db.sqlite.tracks import SQLiteManager
|
||||
from app.db.sqlite.tracks import SQLiteTrackMethods as db
|
||||
from app.db.sqlite.settings import SettingsSQLMethods as sdb
|
||||
|
||||
from app.store.folder import FolderStore
|
||||
from app.store.tracks import TrackStore
|
||||
from app.store.albums import AlbumStore
|
||||
from app.store.artists import ArtistStore
|
||||
@@ -144,8 +143,6 @@ def add_track(filepath: str) -> None:
|
||||
track = Track(**tags)
|
||||
TrackStore.add_track(track)
|
||||
|
||||
FolderStore.add_folder(track.folder)
|
||||
|
||||
if not AlbumStore.album_exists(track.albumhash):
|
||||
album = AlbumStore.create_album(track)
|
||||
AlbumStore.add_album(album)
|
||||
@@ -182,11 +179,6 @@ def remove_track(filepath: str) -> None:
|
||||
if empty_artist:
|
||||
ArtistStore.remove_artist_by_hash(artist.artisthash)
|
||||
|
||||
empty_folder = FolderStore.is_empty_folder(track.folder)
|
||||
|
||||
if empty_folder:
|
||||
FolderStore.remove_folder(track.folder)
|
||||
|
||||
|
||||
class Handler(PatternMatchingEventHandler):
|
||||
files_to_process = []
|
||||
@@ -204,7 +196,6 @@ class Handler(PatternMatchingEventHandler):
|
||||
self,
|
||||
patterns=patterns,
|
||||
ignore_directories=True,
|
||||
case_sensitive=False,
|
||||
)
|
||||
|
||||
def get_abs_path(self, path: str):
|
||||
|
||||
Reference in New Issue
Block a user