diff --git a/server/app/api/__init__.py b/server/app/api/__init__.py index 90b2b1ee..d2327ff7 100644 --- a/server/app/api/__init__.py +++ b/server/app/api/__init__.py @@ -3,16 +3,18 @@ This module contains all the Flask Blueprints and API routes. It also contains a that are used through-out the app. It handles the initialization of the watchdog, checking and creating config dirs and starting the re-indexing process using a background thread. """ +from typing import List +from typing import Set -from typing import List, Set - -from app import models, instances -from app import functions, helpers, prep +from app import functions +from app import helpers +from app import instances +from app import models +from app import prep from app.lib import albumslib from app.lib import folderslib from app.lib import playlistlib - DB_TRACKS = instances.tracks_instance.get_all_tracks() VALID_FOLDERS: Set[str] = set() diff --git a/server/app/api/artist.py b/server/app/api/artist.py index fc7c60b6..0958197a 100644 --- a/server/app/api/artist.py +++ b/server/app/api/artist.py @@ -1,16 +1,14 @@ """ Contains all the artist(s) routes. """ - - -from flask import Blueprint import urllib -from app import instances +from app import cache from app import helpers +from app import instances +from flask import Blueprint artist_bp = Blueprint("artist", __name__, url_prefix="/") -from app import cache @artist_bp.route("/artist/") @@ -53,4 +51,8 @@ def get_artist_data(artist: str): return albums_with_count - return {"artist": artist_obj, "songs": songs, "albums": get_artist_albums()} + return { + "artist": artist_obj, + "songs": songs, + "albums": get_artist_albums() + } diff --git a/server/app/api/track.py b/server/app/api/track.py index a3d35391..2ddc9e02 100644 --- a/server/app/api/track.py +++ b/server/app/api/track.py @@ -32,4 +32,4 @@ def get_sample_track(): """ return instances.tracks_instance.get_song_by_album("Legends Never Die", - "Juice WRLD") + "Juice WRLD") diff --git a/server/app/db/albums.py b/server/app/db/albums.py index fd87a708..7a85fae7 100644 --- a/server/app/db/albums.py +++ b/server/app/db/albums.py @@ -3,9 +3,8 @@ This file contains the Album class for interacting with album documents in MongoDB. """ from app import db -from bson import ObjectId - from app.models import Album +from bson import ObjectId convert_many = db.convert_many convert_one = db.convert_one diff --git a/server/app/functions.py b/server/app/functions.py index 1242cccd..880ca46a 100644 --- a/server/app/functions.py +++ b/server/app/functions.py @@ -1,11 +1,11 @@ """ This module contains functions for the server """ -from dataclasses import asdict import datetime import os import random import time +from dataclasses import asdict from io import BytesIO from typing import List @@ -18,12 +18,12 @@ from app import settings from app.lib import albumslib from app.lib import folderslib from app.lib import watchdoge +from app.lib.taglib import get_tags +from app.lib.taglib import return_album_art +from app.logger import Log from PIL import Image from progress.bar import Bar -from app.logger import Log -from app.lib.taglib import get_tags, return_album_art - @helpers.background def reindex_tracks(): @@ -60,7 +60,8 @@ def populate(): albums = [] folders = set() - files = helpers.run_fast_scandir(settings.HOME_DIR, [".flac", ".mp3"], full=True)[1] + files = helpers.run_fast_scandir(settings.HOME_DIR, [".flac", ".mp3"], + full=True)[1] _bar = Bar("Checking files", max=len(files)) for track in db_tracks: @@ -128,11 +129,8 @@ def populate(): end = time.time() print( - str(datetime.timedelta(seconds=round(end - start))) - + " elapsed for " - + str(len(files)) - + " files" - ) + str(datetime.timedelta(seconds=round(end - start))) + " elapsed for " + + str(len(files)) + " files") def fetch_image_path(artist: str) -> str or None: @@ -167,9 +165,8 @@ def fetch_artist_images(): _bar = Bar("Processing images", max=len(artists)) for artist in artists: - file_path = ( - helpers.app_dir + "/images/artists/" + artist.replace("/", "::") + ".webp" - ) + file_path = (helpers.app_dir + "/images/artists/" + + artist.replace("/", "::") + ".webp") if not os.path.exists(file_path): img_path = fetch_image_path(artist) @@ -191,8 +188,7 @@ def fetch_album_bio(title: str, albumartist: str): Returns the album bio for a given album. """ last_fm_url = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key={}&artist={}&album={}&format=json".format( - settings.LAST_FM_API_KEY, albumartist, title - ) + settings.LAST_FM_API_KEY, albumartist, title) try: response = requests.get(last_fm_url) @@ -201,7 +197,8 @@ def fetch_album_bio(title: str, albumartist: str): return None try: - bio = data["album"]["wiki"]["summary"].split(' Dict[List[str], List[str]]: +def run_fast_scandir(__dir: str, + ext: list, + full=False) -> Dict[List[str], List[str]]: """ Scans a directory for files with a specific extension. Returns a list of files and folders in the directory. """ @@ -59,12 +62,10 @@ def remove_duplicates(tracklist: List[models.Track]) -> List[models.Track]: while song_num < len(tracklist) - 1: for index, song in enumerate(tracklist): - if ( - tracklist[song_num].title == song.title - and tracklist[song_num].album == song.album - and tracklist[song_num].artists == song.artists - and index != song_num - ): + if (tracklist[song_num].title == song.title + and tracklist[song_num].album == song.album + and tracklist[song_num].artists == song.artists + and index != song_num): tracklist.remove(song) song_num += 1 @@ -107,10 +108,11 @@ def check_artist_image(image: str) -> str: """ img_name = image.replace("/", "::") + ".webp" - if not os.path.exists(os.path.join(app_dir, "images", "artists", img_name)): + if not os.path.exists(os.path.join(app_dir, "images", "artists", + img_name)): return use_memoji() else: - return (settings.IMG_ARTIST_URI + img_name,) + return (settings.IMG_ARTIST_URI + img_name, ) class Timer: diff --git a/server/app/instances.py b/server/app/instances.py index c5878018..08fb85a3 100644 --- a/server/app/instances.py +++ b/server/app/instances.py @@ -1,11 +1,14 @@ """ All the MongoDB instances are created here. """ - -from app.db import artists, albums, trackcolors, tracks, playlists +from app.db import albums +from app.db import artists +from app.db import playlists +from app.db import trackcolors +from app.db import tracks tracks_instance = tracks.AllSongs() artist_instance = artists.Artists() track_color_instance = trackcolors.TrackColors() album_instance = albums.Albums() -playlist_instance = playlists.Playlists() \ No newline at end of file +playlist_instance = playlists.Playlists() diff --git a/server/app/lib/albumslib.py b/server/app/lib/albumslib.py index 429b27c8..e93f7264 100644 --- a/server/app/lib/albumslib.py +++ b/server/app/lib/albumslib.py @@ -5,13 +5,13 @@ import random import urllib from pprint import pprint from typing import List -from progress.bar import Bar from app import api from app import functions +from app import instances from app import models from app.lib import trackslib -from app import instances +from progress.bar import Bar def get_all_albums() -> List[models.Album]: @@ -63,7 +63,8 @@ def find_album(albumtitle: str, artist: str) -> models.Album: iter += 1 mid = (left + right) // 2 - if api.ALBUMS[mid].title == albumtitle and api.ALBUMS[mid].artist == artist: + if api.ALBUMS[mid].title == albumtitle and api.ALBUMS[ + mid].artist == artist: return mid if api.ALBUMS[mid].title < albumtitle: @@ -101,7 +102,8 @@ def get_album_image(album: list) -> str: """ for track in album: - img_p = (track["album"] + track["albumartist"] + ".webp").replace("/", "::") + img_p = (track["album"] + track["albumartist"] + ".webp").replace( + "/", "::") img = functions.extract_thumb(track["filepath"], webp_path=img_p) if img is not None: @@ -140,8 +142,7 @@ def create_album(track) -> models.Album: album["date"] = album_tracks[0]["date"] album["artistimage"] = urllib.parse.quote_plus( - album_tracks[0]["albumartist"] + ".webp" - ) + album_tracks[0]["albumartist"] + ".webp") album["image"] = get_album_image(album_tracks) diff --git a/server/app/lib/colorlib.py b/server/app/lib/colorlib.py index 2bc6f581..5882461f 100644 --- a/server/app/lib/colorlib.py +++ b/server/app/lib/colorlib.py @@ -1,10 +1,11 @@ -import colorgram -from progress.bar import Bar -from PIL import Image from io import BytesIO -from app import api, instances +import colorgram +from app import api +from app import instances from app.lib.taglib import return_album_art +from PIL import Image +from progress.bar import Bar def get_image_colors(image) -> list: diff --git a/server/app/lib/folderslib.py b/server/app/lib/folderslib.py index a7ba2504..ed7ea883 100644 --- a/server/app/lib/folderslib.py +++ b/server/app/lib/folderslib.py @@ -1,5 +1,6 @@ import time -from typing import List, Set +from typing import List +from typing import Set from app import api from app import helpers diff --git a/server/app/lib/taglib.py b/server/app/lib/taglib.py index 92efbfd7..dd5cca68 100644 --- a/server/app/lib/taglib.py +++ b/server/app/lib/taglib.py @@ -1,13 +1,13 @@ import os -from mutagen.flac import FLAC -from mutagen.id3 import ID3 -from mutagen.flac import MutagenError -import mutagen import urllib -from PIL import Image from io import BytesIO +import mutagen from app import settings +from mutagen.flac import FLAC +from mutagen.flac import MutagenError +from mutagen.id3 import ID3 +from PIL import Image def return_album_art(filepath: str): diff --git a/server/app/lib/watchdoge.py b/server/app/lib/watchdoge.py index 8d7cbb20..50191363 100644 --- a/server/app/lib/watchdoge.py +++ b/server/app/lib/watchdoge.py @@ -1,19 +1,17 @@ """ This library contains the classes and functions related to the watchdog file watcher. """ - -import time import os +import time -from watchdog.observers import Observer -from watchdog.events import PatternMatchingEventHandler - +from app import api from app import instances from app import models -from app import api from app.lib import folderslib -from app.lib.taglib import get_tags from app.lib.albumslib import create_album +from app.lib.taglib import get_tags +from watchdog.events import PatternMatchingEventHandler +from watchdog.observers import Observer class OnMyWatch: @@ -76,7 +74,8 @@ def remove_track(filepath: str) -> None: fpath = filepath.replace(fname, "") try: - trackid = instances.tracks_instance.get_song_by_path(filepath)["_id"]["$oid"] + trackid = instances.tracks_instance.get_song_by_path( + filepath)["_id"]["$oid"] except TypeError: print(f"šŸ’™ Watchdog Error: Error removing track {filepath} TypeError") return diff --git a/server/app/logger.py b/server/app/logger.py index c8cc1b18..ee844ccd 100644 --- a/server/app/logger.py +++ b/server/app/logger.py @@ -2,6 +2,7 @@ from app.settings import logger class Log: + def __init__(self, msg): if logger.enable: print("\nšŸ¦‹ " + msg + "\n") diff --git a/server/app/models.py b/server/app/models.py index ba8ea21d..862c6f7b 100644 --- a/server/app/models.py +++ b/server/app/models.py @@ -73,11 +73,9 @@ class Album: def get_p_track(ptrack): for track in api.TRACKS: - if ( - track.title == ptrack["title"] - and track.artists == ptrack["artists"] - and ptrack["album"] == track.album - ): + if (track.title == ptrack["title"] + and track.artists == ptrack["artists"] + and ptrack["album"] == track.album): return track diff --git a/server/app/settings.py b/server/app/settings.py index 8b0f6c29..446d65f9 100644 --- a/server/app/settings.py +++ b/server/app/settings.py @@ -1,8 +1,8 @@ """ Contains default configs """ -from dataclasses import dataclass import os +from dataclasses import dataclass # paths CONFIG_FOLDER = ".alice" @@ -36,4 +36,4 @@ P_COLORS = [ class logger: - enable = True \ No newline at end of file + enable = True