mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-05 04:53:01 +00:00
rewrite album and artist stores using in-mem hashmap stores
This commit is contained in:
+66
-48
@@ -1,9 +1,13 @@
|
||||
from itertools import groupby
|
||||
import json
|
||||
from pprint import pprint
|
||||
import random
|
||||
from typing import Iterable
|
||||
|
||||
from app.db.sqlite.albumcolors import SQLiteAlbumMethods as aldb
|
||||
from app.lib.tagger import create_albums
|
||||
from app.models import Album, Track
|
||||
from app.store.artists import ArtistStore
|
||||
from app.utils.customlist import CustomList
|
||||
from app.utils.remove_duplicates import remove_duplicates
|
||||
|
||||
@@ -14,8 +18,19 @@ from app.utils.progressbar import tqdm
|
||||
ALBUM_LOAD_KEY = ""
|
||||
|
||||
|
||||
class AlbumMapEntry:
|
||||
def __init__(self, album: Album) -> None:
|
||||
self.album = album
|
||||
self.trackhashes: set[str] = set()
|
||||
|
||||
@property
|
||||
def basetitle(self):
|
||||
return self.album.base_title
|
||||
|
||||
|
||||
class AlbumStore:
|
||||
albums: list[Album] = CustomList()
|
||||
albummap: dict[str, AlbumMapEntry] = {}
|
||||
|
||||
@staticmethod
|
||||
def create_album(track: Track):
|
||||
@@ -36,43 +51,28 @@ class AlbumStore:
|
||||
global ALBUM_LOAD_KEY
|
||||
ALBUM_LOAD_KEY = instance_key
|
||||
|
||||
cls.albums = CustomList()
|
||||
|
||||
print("Loading albums... ", end="")
|
||||
tracks = remove_duplicates(TrackStore.tracks)
|
||||
|
||||
cls.albummap = {
|
||||
album.albumhash: AlbumMapEntry(album=album) for album in create_albums()
|
||||
}
|
||||
tracks = remove_duplicates(TrackStore.get_flat_list())
|
||||
tracks = sorted(tracks, key=lambda t: t.albumhash)
|
||||
grouped = groupby(tracks, lambda t: t.albumhash)
|
||||
|
||||
for albumhash, tracks in grouped:
|
||||
tracks = list(tracks)
|
||||
sample = tracks[0]
|
||||
cls.albummap[albumhash].trackhashes = {t.trackhash for t in tracks}
|
||||
|
||||
if sample is None:
|
||||
continue
|
||||
# db_albums: list[tuple] = aldb.get_all_albums()
|
||||
|
||||
count = len(list(tracks))
|
||||
duration = sum(t.duration for t in tracks)
|
||||
created_date = min(t.created_date for t in tracks)
|
||||
# for album in db_albums:
|
||||
# albumhash = album[1]
|
||||
# colors = json.loads(album[2])
|
||||
|
||||
album = AlbumStore.create_album(sample)
|
||||
|
||||
album.get_date_from_tracks(tracks)
|
||||
album.set_count(count)
|
||||
album.set_duration(duration)
|
||||
album.set_created_date(created_date)
|
||||
|
||||
cls.albums.append(album)
|
||||
|
||||
db_albums: list[tuple] = aldb.get_all_albums()
|
||||
|
||||
for album in db_albums:
|
||||
albumhash = album[1]
|
||||
colors = json.loads(album[2])
|
||||
|
||||
for _al in cls.albums:
|
||||
if _al.albumhash == albumhash:
|
||||
_al.set_colors(colors)
|
||||
break
|
||||
# for _al in cls.albums:
|
||||
# if _al.albumhash == albumhash:
|
||||
# _al.set_colors(colors)
|
||||
# break
|
||||
|
||||
print("Done!")
|
||||
|
||||
@@ -98,9 +98,7 @@ class AlbumStore:
|
||||
Returns N albums by the given albumartist, excluding the specified album.
|
||||
"""
|
||||
|
||||
albums = [
|
||||
album for album in cls.albums if artisthash in album.albumartists_hashes
|
||||
]
|
||||
albums = [album for album in cls.albums if artisthash in album.artisthashes]
|
||||
|
||||
albums = [
|
||||
album
|
||||
@@ -126,25 +124,11 @@ class AlbumStore:
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def get_albums_by_hashes(cls, albumhashes: list[str]) -> list[Album]:
|
||||
def get_albums_by_hashes(cls, albumhashes: Iterable[str]) -> list[Album]:
|
||||
"""
|
||||
Returns albums by their hashes.
|
||||
"""
|
||||
albums_str = "-".join(albumhashes)
|
||||
albums = [a for a in cls.albums if a.albumhash in albums_str]
|
||||
|
||||
# sort albums by the order of the hashes
|
||||
albums.sort(key=lambda x: albumhashes.index(x.albumhash))
|
||||
return albums
|
||||
|
||||
@classmethod
|
||||
def get_albums_by_artisthash(cls, artisthash: str) -> list[Album]:
|
||||
"""
|
||||
Returns all albums by the given artist.
|
||||
"""
|
||||
return [
|
||||
album for album in cls.albums if artisthash in album.albumartists_hashes
|
||||
]
|
||||
return [cls.albummap[albumhash].album for albumhash in albumhashes]
|
||||
|
||||
@classmethod
|
||||
def count_albums_by_artisthash(cls, artisthash: str):
|
||||
@@ -174,3 +158,37 @@ class AlbumStore:
|
||||
Removes an album from the store.
|
||||
"""
|
||||
cls.albums = CustomList(a for a in cls.albums if a.albumhash != albumhash)
|
||||
|
||||
@classmethod
|
||||
def get_albums_by_artisthash(cls, hash: str):
|
||||
"""
|
||||
Returns all albums by the given artist hash.
|
||||
"""
|
||||
artist = ArtistStore.artistmap.get(hash)
|
||||
|
||||
if not artist:
|
||||
return []
|
||||
|
||||
return [cls.albummap[albumhash].album for albumhash in artist.albumhashes]
|
||||
|
||||
@classmethod
|
||||
def get_albums_by_artisthashes(cls, hashes: Iterable[str]):
|
||||
"""
|
||||
Returns all albums by the given artist hashes.
|
||||
"""
|
||||
albums = []
|
||||
for hash in hashes:
|
||||
albums.extend(cls.get_albums_by_artisthash(hash))
|
||||
|
||||
return albums
|
||||
|
||||
@classmethod
|
||||
def get_album_tracks(cls, albumhash: str) -> list[Track]:
|
||||
"""
|
||||
Returns all tracks for the given album hash.
|
||||
"""
|
||||
album = cls.albummap.get(albumhash)
|
||||
if not album:
|
||||
return []
|
||||
|
||||
return TrackStore.get_tracks_by_trackhashes(album.trackhashes)
|
||||
|
||||
Reference in New Issue
Block a user