mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-05 04:53:01 +00:00
feat: exit the Populate function when another one is started
+ add test for the extract_fetured_artists_from_title function
This commit is contained in:
+20
-9
@@ -4,7 +4,7 @@ from app import settings
|
||||
from app.logger import log
|
||||
from app.lib import populate
|
||||
from app.db.store import Store
|
||||
from app.utils import background
|
||||
from app.utils import background, get_random_str
|
||||
from app.lib.watchdogg import Watcher as WatchDog
|
||||
from app.db.sqlite.settings import SettingsSQLMethods as sdb
|
||||
|
||||
@@ -17,21 +17,32 @@ def get_child_dirs(parent: str, children: list[str]):
|
||||
return [_dir for _dir in children if _dir.startswith(parent) and _dir != parent]
|
||||
|
||||
|
||||
@background
|
||||
def rebuild_store(db_dirs: list[str]):
|
||||
def reload_everything():
|
||||
"""
|
||||
Restarts the watchdog and rebuilds the music library.
|
||||
Reloads all stores using the current database items
|
||||
"""
|
||||
|
||||
log.info("Rebuilding library...")
|
||||
Store.remove_tracks_by_dir_except(db_dirs)
|
||||
|
||||
Store.load_all_tracks()
|
||||
Store.process_folders()
|
||||
Store.load_albums()
|
||||
Store.load_artists()
|
||||
|
||||
populate.Populate()
|
||||
|
||||
@background
|
||||
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)
|
||||
reload_everything()
|
||||
|
||||
key = get_random_str()
|
||||
try:
|
||||
populate.Populate(key=key)
|
||||
except populate.PopulateCancelledError:
|
||||
reload_everything()
|
||||
return
|
||||
|
||||
WatchDog().restart()
|
||||
|
||||
log.info("Rebuilding library... ✅")
|
||||
|
||||
+5
-2
@@ -8,7 +8,7 @@ from requests import ReadTimeout
|
||||
from app import utils
|
||||
from app.lib.artistlib import CheckArtistImages
|
||||
from app.lib.colorlib import ProcessAlbumColors, ProcessArtistColors
|
||||
from app.lib.populate import Populate, ProcessTrackThumbnails
|
||||
from app.lib.populate import Populate, ProcessTrackThumbnails, PopulateCancelledError
|
||||
from app.lib.trackslib import validate_tracks
|
||||
from app.logger import log
|
||||
|
||||
@@ -23,8 +23,11 @@ def run_periodic_checks():
|
||||
validate_tracks()
|
||||
|
||||
while True:
|
||||
try:
|
||||
Populate(key=utils.get_random_str())
|
||||
except PopulateCancelledError:
|
||||
pass
|
||||
|
||||
Populate()
|
||||
ProcessTrackThumbnails()
|
||||
ProcessAlbumColors()
|
||||
ProcessArtistColors()
|
||||
|
||||
+15
-3
@@ -16,6 +16,12 @@ from app.utils import run_fast_scandir
|
||||
get_all_tracks = SQLiteTrackMethods.get_all_tracks
|
||||
insert_many_tracks = SQLiteTrackMethods.insert_many_tracks
|
||||
|
||||
POPULATE_KEY = ""
|
||||
|
||||
|
||||
class PopulateCancelledError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class Populate:
|
||||
"""
|
||||
@@ -25,7 +31,10 @@ class Populate:
|
||||
also checks if the album art exists in the image path, if not tries to extract it.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
def __init__(self, key: str) -> None:
|
||||
global POPULATE_KEY
|
||||
POPULATE_KEY = key
|
||||
|
||||
tracks = get_all_tracks()
|
||||
tracks = list(tracks)
|
||||
|
||||
@@ -54,7 +63,7 @@ class Populate:
|
||||
log.info("All clear, no unread files.")
|
||||
return
|
||||
|
||||
self.tag_untagged(untagged)
|
||||
self.tag_untagged(untagged, key)
|
||||
|
||||
@staticmethod
|
||||
def filter_untagged(tracks: list[Track], files: list[str]):
|
||||
@@ -62,7 +71,7 @@ class Populate:
|
||||
return set(files) - set(tagged_files)
|
||||
|
||||
@staticmethod
|
||||
def tag_untagged(untagged: set[str]):
|
||||
def tag_untagged(untagged: set[str], key: str):
|
||||
log.info("Found %s new tracks", len(untagged))
|
||||
tagged_tracks: list[dict] = []
|
||||
tagged_count = 0
|
||||
@@ -71,6 +80,9 @@ class Populate:
|
||||
fav_tracks = "-".join([t[1] for t in fav_tracks])
|
||||
|
||||
for file in tqdm(untagged, desc="Reading files"):
|
||||
if POPULATE_KEY != key:
|
||||
raise PopulateCancelledError('Populate key changed')
|
||||
|
||||
tags = get_tags(file)
|
||||
|
||||
if tags is not None:
|
||||
|
||||
@@ -40,7 +40,6 @@ class Watcher:
|
||||
while trials < 10:
|
||||
try:
|
||||
dirs = sdb.get_root_dirs()
|
||||
print(dirs)
|
||||
dir_map = [
|
||||
{"original": d, "realpath": os.path.realpath(d)} for d in dirs
|
||||
]
|
||||
|
||||
+17
-9
@@ -1,7 +1,9 @@
|
||||
"""
|
||||
This module contains mini functions for the server.
|
||||
"""
|
||||
import random
|
||||
import re
|
||||
import string
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
|
||||
@@ -32,16 +34,19 @@ def background(func):
|
||||
return background_func
|
||||
|
||||
|
||||
def run_fast_scandir(__dir: str, full=False) -> tuple[list[str], list[str]]:
|
||||
def run_fast_scandir(_dir: str, full=False) -> tuple[list[str], list[str]]:
|
||||
"""
|
||||
Scans a directory for files with a specific extension. Returns a list of files and folders in the directory.
|
||||
"""
|
||||
|
||||
if _dir == "":
|
||||
return [], []
|
||||
|
||||
subfolders = []
|
||||
files = []
|
||||
|
||||
try:
|
||||
for _files in os.scandir(__dir):
|
||||
for _files in os.scandir(_dir):
|
||||
if _files.is_dir() and not _files.name.startswith("."):
|
||||
subfolders.append(_files.path)
|
||||
if _files.is_file():
|
||||
@@ -54,7 +59,7 @@ def run_fast_scandir(__dir: str, full=False) -> tuple[list[str], list[str]]:
|
||||
sub_dirs, _files = run_fast_scandir(_dir, full=True)
|
||||
subfolders.extend(sub_dirs)
|
||||
files.extend(_files)
|
||||
except PermissionError:
|
||||
except (PermissionError, FileNotFoundError, ValueError):
|
||||
return [], []
|
||||
|
||||
return subfolders, files
|
||||
@@ -177,13 +182,11 @@ def get_artists_from_tracks(tracks: list[models.Track]) -> set[str]:
|
||||
def get_albumartists(albums: list[models.Album]) -> set[str]:
|
||||
artists = set()
|
||||
|
||||
# master_artist_list = [a.albumartists for a in albums]
|
||||
for album in albums:
|
||||
albumartists = [a.name for a in album.albumartists] # type: ignore
|
||||
|
||||
artists.update(albumartists)
|
||||
|
||||
# return [models.Artist(a) for a in artists]
|
||||
return artists
|
||||
|
||||
|
||||
@@ -231,7 +234,10 @@ def get_home_res_path(filename: str):
|
||||
"""
|
||||
Returns a path to resources in the home directory of this project. Used to resolve resources in builds.
|
||||
"""
|
||||
return (CWD / ".." / filename).resolve()
|
||||
try:
|
||||
return (CWD / ".." / filename).resolve()
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def get_ip():
|
||||
@@ -258,9 +264,6 @@ def split_artists(src: str):
|
||||
return [a.strip() for a in artists]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def extract_featured_artists_from_title(title: str) -> list[str]:
|
||||
"""
|
||||
Extracts featured artists from a song title using regex.
|
||||
@@ -276,3 +279,8 @@ def extract_featured_artists_from_title(title: str) -> list[str]:
|
||||
return artists
|
||||
|
||||
|
||||
def get_random_str(length=5):
|
||||
"""
|
||||
Generates a random string of length `length`.
|
||||
"""
|
||||
return "".join(random.choices(string.ascii_letters + string.digits, k=length))
|
||||
|
||||
Reference in New Issue
Block a user