fix: album favorite state, artist and album colors

+ fix: unserialized artist result
+ misc
This commit is contained in:
cwilvx
2024-08-02 12:25:55 +03:00
parent 16db3e1ad2
commit 0463c80070
16 changed files with 195 additions and 165 deletions
+59 -56
View File
@@ -2,19 +2,16 @@
Contains everything that deals with image color extraction.
"""
import json
from pathlib import Path
import colorgram
from app import settings
from app.db.sqlite.albumcolors import SQLiteAlbumMethods as aldb
from app.db.sqlite.artistcolors import SQLiteArtistMethods as adb
from app.db.sqlite.utils import SQLiteManager
from app.db.userdata import ArtistData
from app.db.userdata import LibDataTable
from app.logger import log
from app.lib.errors import PopulateCancelledError
from app.store.albums import AlbumStore
from app.store.artists import ArtistStore
from app.utils.progressbar import tqdm
@@ -52,47 +49,45 @@ def process_color(item_hash: str, is_album=True):
return get_image_colors(str(path))
# class ProcessAlbumColors:
# """
# Extracts the most dominant color from the album art and saves it to the database.
# """
class ProcessAlbumColors:
"""
Extracts the most dominant color from the album art and saves it to the database.
"""
# def __init__(self, instance_key: str) -> None:
# global PROCESS_ALBUM_COLORS_KEY
# PROCESS_ALBUM_COLORS_KEY = instance_key
def __init__(self, instance_key: str) -> None:
global PROCESS_ALBUM_COLORS_KEY
PROCESS_ALBUM_COLORS_KEY = instance_key
# albums = [
# a
# for a in AlbumStore.albums
# if a is not None and a.colors is not None and len(a.colors) == 0
# ]
albums = [a for a in AlbumStore.get_flat_list() if not a.color]
# with SQLiteManager() as cur:
# try:
# for album in tqdm(albums, desc="Processing missing album colors"):
# if PROCESS_ALBUM_COLORS_KEY != instance_key:
# raise PopulateCancelledError(
# "A newer 'ProcessAlbumColors' instance is running. Stopping this one."
# )
for album in tqdm(albums, desc="Processing missing album colors"):
albumhash = album.albumhash
if PROCESS_ALBUM_COLORS_KEY != instance_key:
raise PopulateCancelledError(
"A newer 'ProcessAlbumColors' instance is running. Stopping this one."
)
# # TODO: Stop hitting the database for every album.
# # Instead, fetch all the data from the database and
# # check from memory.
albumrecord = LibDataTable.find_one(albumhash, type="album")
if albumrecord is not None and albumrecord.color is not None:
continue
# exists = aldb.exists(album.albumhash, cur=cur)
# if exists:
# continue
colors = process_color(albumhash)
# colors = process_color(album.albumhash)
if colors is None:
continue
# if colors is None:
# continue
album = AlbumStore.albummap.get(albumhash)
# album.set_colors(colors)
# color_str = json.dumps(colors)
# aldb.insert_one_album(cur, album.albumhash, color_str)
# finally:
# cur.close()
if album:
album.set_color(colors[0])
# INFO: Write to the database.
if albumrecord is None:
LibDataTable.insert_one(
{"itemhash": albumhash, "color": colors[0], "itemtype": "album"}
)
else:
LibDataTable.update_one(albumhash, {"color": colors[0]})
class ProcessArtistColors:
@@ -101,29 +96,37 @@ class ProcessArtistColors:
"""
def __init__(self, instance_key: str) -> None:
all_artists = ArtistStore.get_flat_list()
all_artists = [a for a in ArtistStore.get_flat_list() if not a.color]
global PROCESS_ARTIST_COLORS_KEY
PROCESS_ARTIST_COLORS_KEY = instance_key
try:
for artist in tqdm(all_artists, desc="Processing missing artist colors"):
if PROCESS_ARTIST_COLORS_KEY != instance_key:
raise PopulateCancelledError(
"A newer 'ProcessArtistColors' instance is running. Stopping this one."
)
for artist in tqdm(all_artists, desc="Processing missing artist colors"):
artisthash = artist.artisthash
if PROCESS_ARTIST_COLORS_KEY != instance_key:
raise PopulateCancelledError(
"A newer 'ProcessArtistColors' instance is running. Stopping this one."
)
# exists = adb.exists(artist.artisthash, cur=cur)
artist = ArtistData.find_one(artist.artisthash)
if artist and artist.color is not None:
continue
record = LibDataTable.find_one(artisthash, "artist")
colors = process_color(artist.artisthash, is_album=False)
if (record is not None) and (record.color is not None):
continue
if colors is None:
continue
colors = process_color(artisthash, is_album=False)
artist.set_colors(colors)
adb.insert_one_artist(cur, artist.artisthash, colors)
finally:
cur.close()
if colors is None:
continue
artist = ArtistStore.artistmap.get(artisthash)
if artist:
artist.set_color(colors[0])
# INFO: Write to the database.
if record is None:
LibDataTable.insert_one(
{"itemhash": artisthash, "color": colors[0], "itemtype": "artist"}
)
else:
LibDataTable.update_one(artisthash, {"color": colors[0]})
-4
View File
@@ -96,10 +96,6 @@ def check_folder_type(group_: dict):
key: str = group_["folder"]
tracks: list[Track] = group_["tracks"]
time: float = group_["time"]
print(f"Checking folder: {key}")
print(f"Tracks: {len(tracks)}")
existing_artist_hashes: set[str] = set(ArtistStore.artistmap.keys())
existing_album_hashes: set[str] = set(AlbumStore.albummap.keys())
+4 -2
View File
@@ -1,4 +1,4 @@
from app.lib.mapstuff import map_favorites, map_scrobble_data
from app.lib.mapstuff import map_album_colors, map_artist_colors, map_favorites, map_scrobble_data
from app.lib.populate import CordinateMedia
from app.lib.tagger import IndexTracks
from app.store.folder import FolderStore
@@ -16,7 +16,9 @@ class IndexEverything:
FolderStore.load_filepaths()
map_scrobble_data()
map_favorites()
# CordinateMedia(instance_key=str(time()))
map_artist_colors()
map_album_colors()
CordinateMedia(instance_key=str(time()))
gc.collect()
+21 -1
View File
@@ -1,4 +1,4 @@
from app.db.userdata import FavoritesTable, ScrobbleTable
from app.db.userdata import LibDataTable, FavoritesTable, ScrobbleTable
from app.store.albums import AlbumStore
from app.store.artists import ArtistStore
from app.store.tracks import TrackStore
@@ -66,3 +66,23 @@ def map_favorites():
track = TrackStore.trackhashmap.get(entry.hash)
if track:
track.toggle_favorite_user(entry.userid)
def map_artist_colors():
colors = LibDataTable.get_all_colors(type="artist")
for color in colors:
artist = ArtistStore.artistmap.get(color["itemhash"])
if artist:
artist.set_color(color["color"])
def map_album_colors():
colors = LibDataTable.get_all_colors(type="album")
for color in colors:
album = AlbumStore.albummap.get(color["itemhash"])
if album:
album.set_color(color["color"])
+2 -1
View File
@@ -8,7 +8,7 @@ from requests import ReadTimeout
from app import settings
from app.db.sqlite.tracks import SQLiteTrackMethods
from app.lib.artistlib import CheckArtistImages
from app.lib.colorlib import ProcessArtistColors
from app.lib.colorlib import ProcessAlbumColors, ProcessArtistColors
from app.lib.errors import PopulateCancelledError
from app.lib.taglib import extract_thumb
from app.logger import log
@@ -40,6 +40,7 @@ class CordinateMedia:
try:
ProcessTrackThumbnails(instance_key)
ProcessAlbumColors(instance_key)
ProcessArtistColors(instance_key)
except PopulateCancelledError as e:
log.warn(e)
+15 -10
View File
@@ -3,7 +3,6 @@ from app import settings
from app.config import UserConfig
from app.db.libdata import TrackTable
# from app.lib.populate import CordinateMedia
from app.lib.taglib import extract_thumb, get_tags
from app.models.album import Album
from app.models.artist import Artist
@@ -29,7 +28,6 @@ class IndexTracks:
global POPULATE_KEY
POPULATE_KEY = instance_key
# dirs_to_scan = sdb.get_root_dirs()
dirs_to_scan = UserConfig().rootDirs
if len(dirs_to_scan) == 0:
@@ -159,12 +157,12 @@ def create_albums():
"playcount": track.playcount,
"playduration": track.playduration,
"title": track.album,
"trackcount": 1,
"tracks": {track.trackhash},
"extra": {},
}
else:
album = albums[track.albumhash]
album["trackcount"] += 1
album["tracks"].add(track.trackhash)
album["playcount"] += track.playcount
album["playduration"] += track.playduration
album["lastplayed"] = max(album["lastplayed"], track.lastplayed)
@@ -186,8 +184,12 @@ def create_albums():
album["base_title"], _ = get_base_album_title(album["og_title"])
del genres
trackhashes = album.pop("tracks")
album["trackcount"] = len(trackhashes)
return [Album(**album) for album in albums.values()]
albums[album["albumhash"]] = (Album(**album), trackhashes)
return list(albums.values())
# class IndexArtists:
@@ -225,7 +227,7 @@ def create_artists():
"extra": {},
}
else:
artist = artists[thisartist["artisthash"]]
artist: dict = artists[thisartist["artisthash"]]
artist["duration"] += track.duration
artist["playcount"] += track.playcount
artist["playduration"] += track.playduration
@@ -235,6 +237,8 @@ def create_artists():
artist["created_date"] = min(artist["created_date"], track.last_mod)
artist["names"].add(thisartist["name"])
artist.setdefault("albums", set())
if thisartist.get("in_track", True):
artist["tracks"].add(track.trackhash)
@@ -257,12 +261,13 @@ def create_artists():
# INFO: Delete temporary keys
del artist["names"]
del artist["tracks"]
del artist["albums"]
tracks = artist.pop("tracks")
albums = artist.pop("albums")
# INFO: Delete local variables
del genres
return [Artist(**artist) for artist in artists.values()]
artists[artist["artisthash"]] = (Artist(**artist), tracks, albums)
return list(artists.values())