From 9a2635d7c329da774c3f015d4414672a6af1714c Mon Sep 17 00:00:00 2001 From: geoffrey45 Date: Sun, 12 Jun 2022 11:19:25 +0300 Subject: [PATCH] rewrite some for loops with UseBisection --- server/app/api/__init__.py | 2 +- server/app/api/playlist.py | 20 ++++++++++---------- server/app/helpers.py | 28 ++++++++++++++++------------ server/app/lib/watchdoge.py | 32 +++++++------------------------- 4 files changed, 34 insertions(+), 48 deletions(-) diff --git a/server/app/api/__init__.py b/server/app/api/__init__.py index b9b916d2..18d93dc4 100644 --- a/server/app/api/__init__.py +++ b/server/app/api/__init__.py @@ -34,7 +34,7 @@ def initialize() -> None: albumslib.create_everything() folderslib.run_scandir() playlistlib.create_all_playlists() - functions.reindex_tracks() + # functions.reindex_tracks() initialize() diff --git a/server/app/api/playlist.py b/server/app/api/playlist.py index d8462414..d8730a02 100644 --- a/server/app/api/playlist.py +++ b/server/app/api/playlist.py @@ -12,7 +12,7 @@ from app.lib import playlistlib from flask import Blueprint from flask import request -from app.helpers import create_new_date +from app.helpers import UseBisection, create_new_date playlist_bp = Blueprint("playlist", __name__, url_prefix="/") @@ -78,13 +78,12 @@ def add_track_to_playlist(playlist_id: str): @playlist_bp.route("/playlist/") def get_single_p_info(playlistid: str): - for p in api.PLAYLISTS: - if p.playlistid == playlistid: - tracks = p.get_tracks() - return { - "info": serializer.Playlist(p), - "tracks": tracks, - } + p = UseBisection(api.PLAYLISTS, "playlistid", [playlistid])() + playlist: models.Playlist = p[0] + + if playlist is not None: + tracks = playlist.get_tracks() + return {"info": serializer.Playlist(playlist), "tracks": tracks} return {"info": {}, "tracks": []} @@ -106,9 +105,10 @@ def update_playlist(playlistid: str): "thumb": None, } - for p in api.PLAYLISTS: - if p.playlistid == playlistid: + p = UseBisection(api.PLAYLISTS, "playlistid", [playlistid])() + p: models.Playlist = p[0] + if playlist is not None: if image: image_, thumb_ = playlistlib.save_p_image(image, playlistid) playlist["image"] = image_ diff --git a/server/app/helpers.py b/server/app/helpers.py index 75a92647..e57b2404 100644 --- a/server/app/helpers.py +++ b/server/app/helpers.py @@ -4,9 +4,7 @@ This module contains mini functions for the server. import os import random import threading -import time from datetime import datetime -from typing import Any from typing import Dict from typing import List @@ -62,10 +60,12 @@ 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 @@ -108,8 +108,7 @@ 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 img_name @@ -136,10 +135,15 @@ def create_safe_name(name: str) -> str: class UseBisection: + """ + Uses bisection to find a list of items in another list. - def __init__(self, list: List, search_from: str, - queries: List[str]) -> None: - self.list = list + returns a list of found items with `None` items being not found + items. + """ + + def __init__(self, source: List, search_from: str, queries: List[str]) -> None: + self.list = source self.queries = queries self.search_from = search_from self.list.sort(key=lambda x: getattr(x, search_from)) @@ -160,5 +164,5 @@ class UseBisection: return None - def __call__(self) -> Any: + def __call__(self) -> List: return [self.find(query) for query in self.queries] diff --git a/server/app/lib/watchdoge.py b/server/app/lib/watchdoge.py index 30c5b202..1118a452 100644 --- a/server/app/lib/watchdoge.py +++ b/server/app/lib/watchdoge.py @@ -7,8 +7,7 @@ import time from app import api from app import instances from app import models -from app.helpers import create_album_hash -from app.lib import folderslib +from app.helpers import UseBisection, create_album_hash from app.lib.albumslib import create_album from app.lib.albumslib import find_album from app.lib.taglib import get_tags @@ -71,38 +70,22 @@ def add_track(filepath: str) -> None: api.TRACKS.append(models.Track(tags)) - # folder = tags["folder"] - - # if folder not in api.VALID_FOLDERS: - # api.VALID_FOLDERS.add(folder) - # f = folderslib.create_folder(folder) - # api.FOLDERS.append(f) - def remove_track(filepath: str) -> None: """ Removes a track from the music dict. """ - fname = filepath.split("/")[-1] - 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 - instances.tracks_instance.remove_song_by_id(trackid) - - for track in api.TRACKS: - if track.trackid == trackid: - api.TRACKS.remove(track) - - for folder in api.FOLDERS: - if folder.path + "/" == fpath and folder.trackcount - 1 == 0: - api.FOLDERS.remove(folder) - api.VALID_FOLDERS.remove(folder.path) + track = UseBisection(api.TRACKS, "trackid", [trackid])() + if track is not None: + api.TRACKS.remove(track[0]) + instances.tracks_instance.remove_song_by_id(trackid) class Handler(PatternMatchingEventHandler): @@ -156,13 +139,12 @@ class Handler(PatternMatchingEventHandler): print("⚫ closed ~~~") try: self.files_to_process.remove(event.src_path) + add_track(event.src_path) except ValueError: """ The file was already removed from the list, or it was not in the list to begin with. """ pass - add_track(event.src_path) - watch = OnMyWatch()