break down store.py into multiple files in a module

+ fix last updated date bug
This commit is contained in:
geoffrey45
2023-03-25 03:05:38 +03:00
parent 43c480cf3e
commit d43dcbff46
26 changed files with 688 additions and 636 deletions
+7 -5
View File
@@ -8,10 +8,12 @@ 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.store import Store
from app.models import FavType, Track
from app.utils.remove_duplicates import remove_duplicates
from app.store.tracks import TrackStore
from app.store.albums import AlbumStore
get_albums_by_albumartist = adb.get_albums_by_albumartist
check_is_fav = favdb.check_is_favorite
@@ -36,12 +38,12 @@ def get_album_tracks_and_info():
return error_msg, 400
error_msg = {"error": "Album not created yet."}
album = Store.get_album_by_hash(albumhash)
album = AlbumStore.get_album_by_hash(albumhash)
if album is None:
return error_msg, 204
tracks = Store.get_tracks_by_albumhash(albumhash)
tracks = TrackStore.get_tracks_by_albumhash(albumhash)
if tracks is None:
return error_msg, 404
@@ -84,7 +86,7 @@ def get_album_tracks(albumhash: str):
"""
Returns all the tracks in the given album, sorted by disc and track number.
"""
tracks = Store.get_tracks_by_albumhash(albumhash)
tracks = TrackStore.get_tracks_by_albumhash(albumhash)
tracks = [asdict(t) for t in tracks]
for t in tracks:
@@ -112,7 +114,7 @@ def get_artist_albums():
albums = [
{
"artisthash": a,
"albums": Store.get_albums_by_albumartist(a, limit, exclude=exclude),
"albums": AlbumStore.get_albums_by_albumartist(a, limit, exclude=exclude),
}
for a in albumartists
]
+16 -13
View File
@@ -7,11 +7,14 @@ from collections import deque
from flask import Blueprint, request
from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb
from app.db.store import Store
from app.models import Album, FavType, Track
from app.utils.remove_duplicates import remove_duplicates
from app.requests.artists import fetch_similar_artists
from app.store.albums import AlbumStore
from app.store.tracks import TrackStore
from app.store.artists import ArtistStore
api = Blueprint("artist", __name__, url_prefix="/")
@@ -107,10 +110,10 @@ class ArtistsCache:
"""
entry = [a for a in cls.artists if a.artisthash == artisthash][0]
albums = [Store.get_album_by_hash(h) for h in entry.albumhashes]
albums = [AlbumStore.get_album_by_hash(h) for h in entry.albumhashes]
entry.albums = [album for album in albums if album is not None]
store_albums = Store.get_albums_by_artisthash(artisthash)
store_albums = AlbumStore.get_albums_by_artisthash(artisthash)
all_albums_hash = "-".join([a.albumhash for a in entry.albums])
@@ -130,7 +133,7 @@ class ArtistsCache:
for album in entry.albums:
album.check_type()
album_tracks = Store.get_tracks_by_albumhash(album.albumhash)
album_tracks = TrackStore.get_tracks_by_albumhash(album.albumhash)
album_tracks = remove_duplicates(album_tracks)
album.get_date_from_tracks(album_tracks)
@@ -143,7 +146,7 @@ def add_albums_to_cache(artisthash: str):
"""
Fetches albums and adds them to the cache.
"""
tracks = Store.get_tracks_by_artist(artisthash)
tracks = TrackStore.get_tracks_by_artist(artisthash)
if len(tracks) == 0:
return False
@@ -171,7 +174,7 @@ def get_artist(artisthash: str):
limit = int(limit)
artist = Store.get_artist_by_hash(artisthash)
artist = ArtistStore.get_artist_by_hash(artisthash)
if artist is None:
return {"error": "Artist not found"}, 404
@@ -181,17 +184,17 @@ def get_artist(artisthash: str):
if tracks_cached:
tracks = ArtistsCache.get_tracks(artisthash)
else:
tracks = Store.get_tracks_by_artist(artisthash)
tracks = TrackStore.get_tracks_by_artist(artisthash)
albumhashes = set(t.albumhash for t in tracks)
hashes_from_albums = set(
a.albumhash for a in Store.get_albums_by_artisthash(artisthash)
a.albumhash for a in AlbumStore.get_albums_by_artisthash(artisthash)
)
albumhashes = albumhashes.union(hashes_from_albums)
ArtistsCache.add_entry(artisthash, albumhashes, tracks)
tcount = len(tracks)
acount = Store.count_albums_by_artisthash(artisthash)
acount = AlbumStore.count_albums_by_artisthash(artisthash)
if acount == 0 and tcount < 10:
limit = tcount
@@ -253,7 +256,7 @@ def get_artist_albums(artisthash: str):
appearances = remove_EPs_and_singles(appearances)
artist = Store.get_artist_by_hash(artisthash)
artist = ArtistStore.get_artist_by_hash(artisthash)
if return_all is not None:
limit = len(all_albums)
@@ -273,7 +276,7 @@ def get_all_artist_tracks(artisthash: str):
"""
Returns all artists by a given artist.
"""
tracks = Store.get_tracks_by_artist(artisthash)
tracks = TrackStore.get_tracks_by_artist(artisthash)
return {"tracks": tracks}
@@ -290,13 +293,13 @@ def get_similar_artists(artisthash: str):
limit = int(limit)
artist = Store.get_artist_by_hash(artisthash)
artist = ArtistStore.get_artist_by_hash(artisthash)
if artist is None:
return {"error": "Artist not found"}, 404
similar_hashes = fetch_similar_artists(artist.name)
similar = Store.get_artists_by_hashes(similar_hashes)
similar = ArtistStore.get_artists_by_hashes(similar_hashes)
if len(similar) > limit:
similar = random.sample(similar, limit)
+1 -1
View File
@@ -1,5 +1,5 @@
from flask import Blueprint
from app.db.store import Store
from app.store.albums import AlbumStore as Store
api = Blueprint("colors", __name__, url_prefix="/colors")
+12 -9
View File
@@ -1,10 +1,13 @@
from flask import Blueprint, request
from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb
from app.db.store import Store
from app.models import FavType
from app.utils.bisection import UseBisection
from app.store.artists import ArtistStore
from app.store.albums import AlbumStore
from app.store.tracks import TrackStore
api = Blueprint("favorite", __name__, url_prefix="/")
@@ -28,7 +31,7 @@ def add_favorite():
favdb.insert_one_favorite(itemtype, itemhash)
if itemtype == FavType.track:
Store.add_fav_track(itemhash)
TrackStore.make_track_fav(itemhash)
return {"msg": "Added to favorites"}
@@ -49,7 +52,7 @@ def remove_favorite():
favdb.delete_favorite(itemtype, itemhash)
if itemtype == FavType.track:
Store.remove_fav_track(itemhash)
TrackStore.remove_track_from_fav(itemhash)
return {"msg": "Removed from favorites"}
@@ -67,7 +70,7 @@ def get_favorite_albums():
albumhashes = [a[1] for a in albums]
albumhashes.reverse()
src_albums = sorted(Store.albums, key=lambda x: x.albumhash)
src_albums = sorted(AlbumStore.albums, key=lambda x: x.albumhash)
fav_albums = UseBisection(src_albums, "albumhash", albumhashes)()
fav_albums = remove_none(fav_albums)
@@ -90,7 +93,7 @@ def get_favorite_tracks():
tracks = favdb.get_fav_tracks()
trackhashes = [t[1] for t in tracks]
trackhashes.reverse()
src_tracks = sorted(Store.tracks, key=lambda x: x.trackhash)
src_tracks = sorted(TrackStore.tracks, key=lambda x: x.trackhash)
tracks = UseBisection(src_tracks, "trackhash", trackhashes)()
tracks = remove_none(tracks)
@@ -114,7 +117,7 @@ def get_favorite_artists():
artisthashes = [a[1] for a in artists]
artisthashes.reverse()
src_artists = sorted(Store.artists, key=lambda x: x.artisthash)
src_artists = sorted(ArtistStore.artists, key=lambda x: x.artisthash)
artists = UseBisection(src_artists, "artisthash", artisthashes)()
artists = remove_none(artists)
@@ -176,9 +179,9 @@ def get_all_favorites():
if fav[2] == FavType.artist:
artists.append(fav[1])
src_tracks = sorted(Store.tracks, key=lambda x: x.trackhash)
src_albums = sorted(Store.albums, key=lambda x: x.albumhash)
src_artists = sorted(Store.artists, key=lambda x: x.artisthash)
src_tracks = sorted(TrackStore.tracks, key=lambda x: x.trackhash)
src_albums = sorted(AlbumStore.albums, key=lambda x: x.albumhash)
src_artists = sorted(ArtistStore.artists, key=lambda x: x.artisthash)
tracks = UseBisection(src_tracks, "trackhash", tracks)()
albums = UseBisection(src_albums, "albumhash", albums)()
+10 -8
View File
@@ -7,13 +7,15 @@ from datetime import datetime
from flask import Blueprint, request
from PIL import UnidentifiedImageError
from app import models, serializer
from app import models
from app.db.sqlite.playlists import SQLitePlaylistMethods
from app.db.store import Store
from app.lib import playlistlib
from app.utils.generators import create_new_date
from app.utils.dates import date_string_to_time_passed, create_new_date
from app.utils.remove_duplicates import remove_duplicates
from app.store.tracks import TrackStore
from app.store.albums import AlbumStore
api = Blueprint("playlist", __name__, url_prefix="/")
PL = SQLitePlaylistMethods
@@ -42,7 +44,7 @@ def duplicate_images(images: list):
def get_first_4_images(trackhashes: list[str]) -> list[dict['str', str]]:
tracks = Store.get_tracks_by_trackhashes(trackhashes)
tracks = TrackStore.get_tracks_by_trackhashes(trackhashes)
albums = []
for track in tracks:
@@ -51,7 +53,7 @@ def get_first_4_images(trackhashes: list[str]) -> list[dict['str', str]]:
if len(albums) == 4:
break
albums = Store.get_albums_by_hashes(albums)
albums = AlbumStore.get_albums_by_hashes(albums)
images = [
{
'image': album.image,
@@ -155,11 +157,11 @@ def get_playlist(playlistid: str):
if playlist is None:
return {"msg": "Playlist not found"}, 404
tracks = Store.get_tracks_by_trackhashes(list(playlist.trackhashes))
tracks = TrackStore.get_tracks_by_trackhashes(list(playlist.trackhashes))
tracks = remove_duplicates(tracks)
duration = sum(t.duration for t in tracks)
playlist.last_updated = serializer.date_string_to_time_passed(playlist.last_updated)
playlist.last_updated = date_string_to_time_passed(playlist.last_updated)
playlist.duration = duration
@@ -223,7 +225,7 @@ def update_playlist_info(playlistid: str):
update_playlist(int(playlistid), playlist)
playlist = models.Playlist(*p_tuple)
playlist.last_updated = serializer.date_string_to_time_passed(playlist.last_updated)
playlist.last_updated = date_string_to_time_passed(playlist.last_updated)
return {
"data": playlist,
+4 -2
View File
@@ -6,9 +6,11 @@ from unidecode import unidecode
from flask import Blueprint, request
from app import models
from app.db.store import Store
from app.lib import searchlib
from app.store.tracks import TrackStore
api = Blueprint("search", __name__, url_prefix="/")
SEARCH_COUNT = 12
@@ -38,7 +40,7 @@ class Search:
Calls :class:`SearchTracks` which returns the tracks that fuzzily match
the search terms. Then adds them to the `SearchResults` store.
"""
self.tracks = Store.tracks
self.tracks = TrackStore.tracks
tracks = searchlib.SearchTracks(self.query)()
SearchResults.tracks = tracks
+10 -6
View File
@@ -3,12 +3,16 @@ from app import settings
from app.logger import log
from app.lib import populate
from app.db.store import Store
from app.lib.watchdogg import Watcher as WatchDog
from app.db.sqlite.settings import SettingsSQLMethods as sdb
from app.utils.generators import get_random_str
from app.utils.threading import background
from app.store.store import FolderStore
from app.store.albums import AlbumStore
from app.store.tracks import TrackStore
from app.store.artists import ArtistStore
api = Blueprint("settings", __name__, url_prefix="/")
@@ -22,10 +26,10 @@ def reload_everything():
"""
Reloads all stores using the current database items
"""
Store.load_all_tracks()
Store.process_folders()
Store.load_albums()
Store.load_artists()
TrackStore.load_all_tracks()
FolderStore.process_folders()
AlbumStore.load_albums()
ArtistStore.load_artists()
@background
@@ -34,7 +38,7 @@ def rebuild_store(db_dirs: list[str]):
Restarts the watchdog and rebuilds the music library.
"""
log.info("Rebuilding library...")
Store.remove_tracks_by_dir_except(db_dirs)
TrackStore.remove_tracks_by_dir_except(db_dirs)
reload_everything()
key = get_random_str()
+2 -2
View File
@@ -5,7 +5,7 @@ import os
from flask import Blueprint, send_file, request
from app.db.store import Store
from app.store.tracks import TrackStore
api = Blueprint("track", __name__, url_prefix="/")
@@ -31,7 +31,7 @@ def send_track_file(trackhash: str):
if trackhash is None:
return msg, 404
tracks = Store.get_tracks_by_trackhashes([trackhash])
tracks = TrackStore.get_tracks_by_trackhashes([trackhash])
for track in tracks:
if track is None: