update supported audio files in settings.py

+ add win_replace_slash function to format win path strings
+ misc
This commit is contained in:
geoffrey45
2023-01-30 15:59:28 +03:00
parent 93a04ba041
commit 7e15680f26
15 changed files with 268 additions and 96 deletions
+19 -6
View File
@@ -4,6 +4,8 @@ from concurrent.futures import ThreadPoolExecutor
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 import win_replace_slash
class GetFilesAndDirs:
@@ -26,14 +28,25 @@ class GetFilesAndDirs:
ext = os.path.splitext(entry.name)[1].lower()
if entry.is_dir() and not entry.name.startswith("."):
dirs.append(entry.path)
dirs.append(win_replace_slash(entry.path))
elif entry.is_file() and ext in SUPPORTED_FILES:
files.append(entry.path)
files.append(win_replace_slash(entry.path))
# sort files by modified time
files.sort(
key=lambda f: os.path.getmtime(f) # pylint: disable=unnecessary-lambda
)
files_ = []
for file in files:
try:
files_.append(
{
"path": file,
"time": os.path.getmtime(file),
}
)
except OSError as e:
log.error(e)
files_.sort(key=lambda f: f["time"])
files = [f["path"] for f in files_]
tracks = Store.get_tracks_by_filepaths(files)
+5 -2
View File
@@ -43,7 +43,10 @@ class Populate:
if len(dirs_to_scan) == 0:
log.warning(
"The root directory is not configured. Open the app in your web browser to configure."
(
"The root directory is not configured. "
+ "Open the app in your webbrowser to configure."
)
)
return
@@ -85,7 +88,7 @@ class Populate:
for file in tqdm(untagged, desc="Reading files"):
if POPULATE_KEY != key:
raise PopulateCancelledError('Populate key changed')
raise PopulateCancelledError("Populate key changed")
tags = get_tags(file)
+31 -9
View File
@@ -1,13 +1,17 @@
import os
import datetime
import os
from io import BytesIO
from tinytag import TinyTag
from PIL import Image, UnidentifiedImageError
from tinytag import TinyTag
from app import settings
from app.utils import create_hash
from app.utils import (
create_hash,
parse_artist_from_filename,
parse_title_from_filename,
win_replace_slash,
)
def parse_album_art(filepath: str):
@@ -81,7 +85,7 @@ def get_tags(filepath: str):
try:
tags = TinyTag.get(filepath)
except: # pylint: disable=bare-except
except: # noqa: E722
return None
no_albumartist: bool = (tags.albumartist == "") or (tags.albumartist is None)
@@ -97,9 +101,22 @@ def get_tags(filepath: str):
for tag in to_filename:
p = getattr(tags, tag)
if p == "" or p is None:
setattr(tags, tag, filename)
maybe = parse_title_from_filename(filename)
setattr(tags, tag, maybe)
to_check = ["album", "artist", "year", "albumartist"]
parse = ["artist", "albumartist"]
for tag in parse:
p = getattr(tags, tag)
if p == "" or p is None:
maybe = parse_artist_from_filename(filename)
if maybe != []:
setattr(tags, tag, ", ".join(maybe))
else:
setattr(tags, tag, "Unknown")
to_check = ["album", "year", "albumartist"]
for prop in to_check:
p = getattr(tags, prop)
if (p is None) or (p == ""):
@@ -127,10 +144,10 @@ def get_tags(filepath: str):
tags.albumhash = create_hash(tags.album, tags.albumartist)
tags.trackhash = create_hash(tags.artist, tags.album, tags.title)
tags.image = f"{tags.albumhash}.webp"
tags.folder = os.path.dirname(filepath)
tags.folder = win_replace_slash(os.path.dirname(filepath))
tags.date = extract_date(tags.year)
tags.filepath = filepath
tags.filepath = win_replace_slash(filepath)
tags.filetype = filetype
tags = tags.__dict__
@@ -157,3 +174,8 @@ def get_tags(filepath: str):
del tags[tag]
return tags
for tag in to_delete:
del tags[tag]
return tags
+7 -3
View File
@@ -63,7 +63,10 @@ class Watcher:
dir_map = [d for d in dir_map if d["realpath"] != d["original"]]
if len(dirs) > 0 and dirs[0] == "$home":
# if len(dirs) > 0 and dirs[0] == "$home":
# dirs = [settings.USER_HOME_DIR]
if any([d == "$home" for d in dirs]):
dirs = [settings.USER_HOME_DIR]
event_handler = Handler(root_dirs=dirs, dir_map=dir_map)
@@ -83,7 +86,7 @@ class Watcher:
try:
self.observer.start()
log.info("Started watchdog")
except FileNotFoundError:
except (FileNotFoundError, PermissionError):
log.error(
"WatchdogError: Failed to start watchdog, root directories could not be resolved."
)
@@ -189,10 +192,11 @@ class Handler(PatternMatchingEventHandler):
def __init__(self, root_dirs: list[str], dir_map: dict[str:str]):
self.root_dirs = root_dirs
self.dir_map = dir_map
patterns = [f"*{f}" for f in settings.SUPPORTED_FILES]
PatternMatchingEventHandler.__init__(
self,
patterns=["*.flac", "*.mp3"],
patterns=patterns,
ignore_directories=True,
case_sensitive=False,
)