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:
geoffrey45
2023-03-26 18:01:26 +03:00
parent 357afeb700
commit 5487dad27b
18 changed files with 102 additions and 333 deletions
+33 -35
View File
@@ -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)