finalize thumbnail validators

- add logger colors
This commit is contained in:
geoffrey45
2022-06-22 11:11:13 +03:00
parent a92e1be799
commit 5fb920f958
6 changed files with 76 additions and 31 deletions
+7 -5
View File
@@ -12,6 +12,8 @@ from PIL import Image
from app import helpers, settings
from app.lib import watchdoge
from app.lib.albumslib import ValidateThumbs
from app.lib import trackslib
from app.lib.populate import CreateAlbums, Populate
@helpers.background
@@ -21,13 +23,13 @@ def run_checks():
"""
# while True:
# trackslib.validate_tracks()
trackslib.validate_tracks()
# Populate()
# CreateAlbums()
Populate()
CreateAlbums()
# if helpers.Ping()():
# CheckArtistImages()()
if helpers.Ping()():
CheckArtistImages()()
ValidateThumbs()
+9 -3
View File
@@ -1,6 +1,7 @@
"""
This library contains all the functions related to albums.
"""
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
import os
import random
@@ -80,13 +81,18 @@ class ValidateThumbs:
albums = helpers.Get.get_all_albums()
thumbs = [(album.hash + ".webp") for album in albums]
for t in thumbs:
e = helpers.UseBisection(entries, "filename", [t])
def rip_image(t_hash: str):
e = helpers.UseBisection(entries, "filename", [t_hash])()[0]
if e is None:
hash = t.split(".")[0]
hash = t_hash.split(".")[0]
RipAlbumImage(hash)
return e
with ThreadPoolExecutor() as pool:
pool.map(rip_image, thumbs)
def __init__(self) -> None:
self.remove_obsolete()
self.find_lost_thumbnails()
+1 -5
View File
@@ -8,7 +8,6 @@ from app.models import Folder
from app.models import Track
from app import instances
from app.logger import Log
@dataclass
@@ -65,14 +64,11 @@ class getFnF:
tracks = instances.tracks_instance.find_songs_by_filenames(files)
tracks = [Track(track) for track in tracks]
s = time.time()
# folders = [create_folder(dir) for dir in dirs]
with ThreadPoolExecutor() as pool:
iter = pool.map(create_folder, dirs)
folders = [i for i in iter if i is not None]
d = time.time() - s
Log(f"Did that in {d} seconds")
folders = filter(lambda f: f.trackcount > 0, folders)
return tracks, folders
+3 -12
View File
@@ -4,12 +4,12 @@ from concurrent.futures import ThreadPoolExecutor
from typing import List
from app import settings
from app.logger import logg
from app.helpers import Get, UseBisection, create_album_hash
from app.helpers import run_fast_scandir
from app.instances import tracks_instance
from app.lib.albumslib import create_album
from app.lib.taglib import get_tags
from app.logger import Log
from app.models import Album, Track
from tqdm import tqdm
@@ -45,8 +45,6 @@ class Populate:
if track["filepath"] in self.files:
self.files.remove(track["filepath"])
Log(f"Found {len(self.files)} untagged tracks")
def get_tags(self, file: str):
tags = get_tags(file)
@@ -60,16 +58,14 @@ class Populate:
Loops through all the untagged files and tags them.
"""
s = time.time()
print(f"Started tagging files")
logg.info("Tagging untagged tracks...")
with ThreadPoolExecutor() as executor:
executor.map(self.get_tags, self.files)
if len(self.tagged_tracks) > 0:
tracks_instance.insert_many(self.tagged_tracks)
d = time.time() - s
Log(f"Tagged {len(self.tagged_tracks)} files in {d} seconds")
logg.info(f"Tagged {len(self.tagged_tracks)} tracks.")
@dataclass
@@ -86,9 +82,7 @@ class CreateAlbums:
prealbums = self.create_pre_albums(self.db_tracks)
prealbums = self.filter_processed(self.db_albums, prealbums)
print(f"📌 {len(prealbums)}")
s = time.time()
albums = []
for album in tqdm(prealbums, desc="Creating albums"):
@@ -103,9 +97,6 @@ class CreateAlbums:
# if i is not None:
# albums.append(i)
d = time.time() - s
Log(f"Created {len(albums)} albums in {d} seconds")
if len(albums) > 0:
instances.album_instance.insert_many(albums)
+7 -2
View File
@@ -4,7 +4,7 @@ This library contains the classes and functions related to the watchdog file wat
import os
import time
from app import api
from app.logger import logg
from app import instances
from app import models
from app.helpers import Get, create_album_hash
@@ -27,7 +27,12 @@ class OnMyWatch:
def run(self):
event_handler = Handler()
self.observer.schedule(event_handler, self.directory, recursive=True)
self.observer.start()
try:
self.observer.start()
except OSError:
logg.error("Could not start watchdog.")
return
try:
while True:
+49 -4
View File
@@ -1,8 +1,53 @@
import logging
from app.settings import logger
class Log:
class CustomFormatter(logging.Formatter):
def __init__(self, msg):
if logger.enable:
print("\n🦋 " + msg + "\n")
grey = "\x1b[38;20m"
yellow = "\x1b[33;20m"
red = "\x1b[31;20m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
# format = (
# "%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
# )
format = "%(levelname)s: @%(name)s: >>> %(message)s (%(filename)s:%(lineno)d)"
FORMATS = {
logging.DEBUG: grey + format + reset,
logging.INFO: grey + format + reset,
logging.WARNING: yellow + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset,
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
logg = logging.getLogger("ALICE_MUSIC_SERVER")
logg.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(CustomFormatter())
logg.addHandler(ch)
def get_logger():
if logger.enable:
return logg
return None
logg = get_logger()
# copied from: https://stackoverflow.com/a/56944256: