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
+4 -3
View File
@@ -10,9 +10,10 @@ from requests.exceptions import ConnectionError as ReqConnError, ReadTimeout
from app import settings
from app.models import Artist, Track, Album
from app.db import store
from app.utils.hashing import create_hash
from app.store import artists as artist_store
def get_artist_image_link(artist: str):
"""
@@ -72,8 +73,8 @@ class CheckArtistImages:
with ThreadPoolExecutor() as pool:
list(
tqdm(
pool.map(self.download_image, store.Store.artists),
total=len(store.Store.artists),
pool.map(self.download_image, artist_store.ArtistStore.artists),
total=len(artist_store.ArtistStore.artists),
desc="Downloading artist images",
)
)
+6 -4
View File
@@ -12,9 +12,11 @@ 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.db.store import Store
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."""
@@ -38,7 +40,7 @@ class ProcessAlbumColors:
"""
def __init__(self) -> None:
albums = [a for a in Store.albums if len(a.colors) == 0]
albums = [a for a in AlbumStore.albums if len(a.colors) == 0]
with SQLiteManager() as cur:
for album in tqdm(albums, desc="Processing missing album colors"):
@@ -69,7 +71,7 @@ class ProcessArtistColors:
"""
def __init__(self) -> None:
all_artists = [a for a in Store.artists if len(a.colors) == 0]
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)
@@ -85,7 +87,7 @@ class ProcessArtistColors:
if len(colors) > 0:
adb.insert_one_artist(artisthash=artist.artisthash, colors=colors)
Store.map_artist_color((0, artist.artisthash, json.dumps(colors)))
ArtistStore.map_artist_color((0, artist.artisthash, json.dumps(colors)))
# 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)
+5 -4
View File
@@ -1,13 +1,14 @@
import os
from concurrent.futures import ThreadPoolExecutor
from pprint import pprint
from app.db.store import Store
from app.models import Folder, Track
from app.settings import SUPPORTED_FILES
from app.logger import log
from app.utils.wintools import win_replace_slash
from app.store.store import FolderStore
from app.store.tracks import TrackStore
class GetFilesAndDirs:
"""
@@ -49,12 +50,12 @@ class GetFilesAndDirs:
files_.sort(key=lambda f: f["time"])
files = [f["path"] for f in files_]
tracks = Store.get_tracks_by_filepaths(files)
tracks = TrackStore.get_tracks_by_filepaths(files)
# TODO: Remove this threadpool and modify the get_folder store
# method to accept a list of paths.
with ThreadPoolExecutor() as pool:
iterable = pool.map(Store.get_folder, dirs)
iterable = pool.map(FolderStore.get_folder, dirs)
folders = [i for i in iterable if i is not None]
folders = filter(lambda f: f.has_tracks, folders)
+16 -12
View File
@@ -5,7 +5,6 @@ from app import settings
from app.db.sqlite.tracks import SQLiteTrackMethods
from app.db.sqlite.settings import SettingsSQLMethods as sdb
from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb
from app.db.store import Store
from app.lib.colorlib import ProcessAlbumColors, ProcessArtistColors
from app.lib.taglib import extract_thumb, get_tags
@@ -13,6 +12,11 @@ from app.logger import log
from app.models import Album, Artist, Track
from app.utils.filesystem import run_fast_scandir
from app.store.store import FolderStore
from app.store.albums import AlbumStore
from app.store.tracks import TrackStore
from app.store.artists import ArtistStore
get_all_tracks = SQLiteTrackMethods.get_all_tracks
insert_many_tracks = SQLiteTrackMethods.insert_many_tracks
@@ -97,19 +101,19 @@ class Populate:
track = Track(**tags)
track.is_favorite = track.trackhash in fav_tracks
Store.add_track(track)
Store.add_folder(track.folder)
TrackStore.add_track(track)
FolderStore.add_folder(track.folder)
if not Store.album_exists(track.albumhash):
Store.add_album(Store.create_album(track))
if not AlbumStore.album_exists(track.albumhash):
AlbumStore.add_album(AlbumStore.create_album(track))
for artist in track.artist:
if not Store.artist_exists(artist.artisthash):
Store.add_artist(Artist(artist.name))
if not ArtistStore.artist_exists(artist.artisthash):
ArtistStore.add_artist(Artist(artist.name))
for artist in track.albumartist:
if not Store.artist_exists(artist.artisthash):
Store.add_artist(Artist(artist.name))
if not ArtistStore.artist_exists(artist.artisthash):
ArtistStore.add_artist(Artist(artist.name))
tagged_count += 1
else:
@@ -122,7 +126,7 @@ class Populate:
def get_image(album: Album):
for track in Store.tracks:
for track in TrackStore.tracks:
if track.albumhash == album.albumhash:
extract_thumb(track.filepath, track.image)
break
@@ -133,8 +137,8 @@ class ProcessTrackThumbnails:
with ThreadPoolExecutor(max_workers=4) as pool:
results = list(
tqdm(
pool.map(get_image, Store.albums),
total=len(Store.albums),
pool.map(get_image, AlbumStore.albums),
total=len(AlbumStore.albums),
desc="Extracting track images",
)
)
+10 -7
View File
@@ -8,9 +8,12 @@ from rapidfuzz import fuzz, process
from unidecode import unidecode
from app import models
from app.db.store import Store
from app.utils.remove_duplicates import remove_duplicates
from app.store.albums import AlbumStore
from app.store.artists import ArtistStore
from app.store.tracks import TrackStore
ratio = fuzz.ratio
wratio = fuzz.WRatio
@@ -40,7 +43,7 @@ class Limit:
class SearchTracks:
def __init__(self, query: str) -> None:
self.query = query
self.tracks = Store.tracks
self.tracks = TrackStore.tracks
def __call__(self) -> List[models.Track]:
"""
@@ -63,7 +66,7 @@ class SearchTracks:
class SearchArtists:
def __init__(self, query: str) -> None:
self.query = query
self.artists = Store.artists
self.artists = ArtistStore.artists
def __call__(self) -> list:
"""
@@ -85,7 +88,7 @@ class SearchArtists:
class SearchAlbums:
def __init__(self, query: str) -> None:
self.query = query
self.albums = Store.albums
self.albums = AlbumStore.albums
def __call__(self) -> List[models.Album]:
"""
@@ -160,9 +163,9 @@ class SearchAll:
def collect_all():
all_items: _type = []
all_items.extend(Store.tracks)
all_items.extend(Store.albums)
all_items.extend(Store.artists)
all_items.extend(TrackStore.tracks)
all_items.extend(AlbumStore.albums)
all_items.extend(ArtistStore.artists)
return all_items, get_titles(all_items)
+3 -3
View File
@@ -5,16 +5,16 @@ import os
from tqdm import tqdm
from app.db.store import Store
from app.db.sqlite.tracks import SQLiteTrackMethods as tdb
from app.store.tracks import TrackStore
def validate_tracks() -> None:
"""
Gets all songs under the ~/ directory.
"""
for track in tqdm(Store.tracks, desc="Removing deleted tracks"):
for track in tqdm(TrackStore.tracks, desc="Removing deleted tracks"):
if not os.path.exists(track.filepath):
print(f"Removing {track.filepath}")
Store.tracks.remove(track)
TrackStore.tracks.remove(track)
tdb.remove_track_by_filepath(track.filepath)
+20 -16
View File
@@ -9,7 +9,6 @@ from watchdog.events import PatternMatchingEventHandler
from watchdog.observers import Observer
from app.logger import log
from app.db.store import Store
from app.lib.taglib import get_tags
from app.models import Artist, Track
from app import settings
@@ -18,6 +17,11 @@ 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.store import FolderStore
from app.store.tracks import TrackStore
from app.store.albums import AlbumStore
from app.store.artists import ArtistStore
class Watcher:
"""
@@ -138,19 +142,19 @@ def add_track(filepath: str) -> None:
db.insert_one_track(tags, cur)
track = Track(**tags)
Store().add_track(track)
TrackStore.add_track(track)
Store.add_folder(track.folder)
FolderStore.add_folder(track.folder)
if not Store.album_exists(track.albumhash):
album = Store.create_album(track)
Store.add_album(album)
if not AlbumStore.album_exists(track.albumhash):
album = AlbumStore.create_album(track)
AlbumStore.add_album(album)
artists: list[Artist] = track.artist + track.albumartist # type: ignore
for artist in artists:
if not Store.artist_exists(artist.artisthash):
Store.add_artist(Artist(artist.name))
if not ArtistStore.artist_exists(artist.artisthash):
ArtistStore.add_artist(Artist(artist.name))
def remove_track(filepath: str) -> None:
@@ -158,30 +162,30 @@ def remove_track(filepath: str) -> None:
Removes a track from the music dict.
"""
try:
track = Store.get_tracks_by_filepaths([filepath])[0]
track = TrackStore.get_tracks_by_filepaths([filepath])[0]
except IndexError:
return
db.remove_track_by_filepath(filepath)
Store.remove_track_by_filepath(filepath)
TrackStore.remove_track_by_filepath(filepath)
empty_album = Store.count_tracks_by_hash(track.albumhash) > 0
empty_album = TrackStore.count_tracks_by_hash(track.albumhash) > 0
if empty_album:
Store.remove_album_by_hash(track.albumhash)
AlbumStore.remove_album_by_hash(track.albumhash)
artists: list[Artist] = track.artist + track.albumartist # type: ignore
for artist in artists:
empty_artist = not Store.artist_has_tracks(artist.artisthash)
empty_artist = not ArtistStore.artist_has_tracks(artist.artisthash)
if empty_artist:
Store.remove_artist_by_hash(artist.artisthash)
ArtistStore.remove_artist_by_hash(artist.artisthash)
empty_folder = Store.is_empty_folder(track.folder)
empty_folder = FolderStore.is_empty_folder(track.folder)
if empty_folder:
Store.remove_folder(track.folder)
FolderStore.remove_folder(track.folder)
class Handler(PatternMatchingEventHandler):