diff --git a/server/app/lib/albumslib.py b/server/app/lib/albumslib.py index edaec8c6..6405ace7 100644 --- a/server/app/lib/albumslib.py +++ b/server/app/lib/albumslib.py @@ -1,6 +1,7 @@ """ This library contains all the functions related to albums. """ +from pprint import pprint import random from typing import List @@ -33,7 +34,6 @@ def validate() -> None: """ - def find_album(albums: List[models.Album], hash: str) -> int | None: """ Finds an album by album title and artist. @@ -114,26 +114,28 @@ class GetAlbumTracks: and album artist. """ - def __init__(self, tracklist: list, albumhash: str) -> None: + def __init__(self, tracklist: List[models.Track], albumhash: str) -> None: self.hash = albumhash self.tracks = tracklist - self.tracks.sort(key=lambda x: x["albumhash"]) + self.tracks.sort(key=lambda x: x.albumhash) - def find_tracks(self): + def __call__(self): tracks = [] - index = trackslib.find_track(self.tracks, self.hash) + tracks = helpers.UseBisection(self.tracks, "albumhash", [self.hash])() - while index is not None: - track = self.tracks[index] - tracks.append(track) - self.tracks.remove(track) - index = trackslib.find_track(self.tracks, self.hash) + pprint(tracks) + + # while index is not None: + # track = self.tracks[index] + # tracks.append(track) + # self.tracks.remove(track) + # index = helpers.UseBisection(self.tracks, "albumhash", [self.hash])() return tracks -def get_album_tracks(album: str, artist: str) -> List: - return GetAlbumTracks(album, artist).find_tracks() +def get_album_tracks(tracklist: List[models.Track], hash: str) -> List: + return GetAlbumTracks(tracklist, hash)() def create_album(track: dict, tracklist: list) -> dict: @@ -144,7 +146,6 @@ def create_album(track: dict, tracklist: list) -> dict: "title": track["album"], "artist": track["albumartist"], } - albumhash = helpers.create_album_hash(album["title"], album["artist"]) album_tracks = get_album_tracks(tracklist, albumhash) diff --git a/server/app/lib/watchdoge.py b/server/app/lib/watchdoge.py index d7496e2b..f3360bc7 100644 --- a/server/app/lib/watchdoge.py +++ b/server/app/lib/watchdoge.py @@ -7,9 +7,8 @@ import time from app import api from app import instances from app import models -from app.helpers import UseBisection, create_album_hash +from app.helpers import Get, create_album_hash from app.lib.albumslib import create_album -from app.lib.albumslib import find_album from app.lib.taglib import get_tags from watchdog.events import PatternMatchingEventHandler from watchdog.observers import Observer @@ -51,24 +50,19 @@ def add_track(filepath: str) -> None: if tags is not None: hash = create_album_hash(tags["album"], tags["albumartist"]) tags["albumhash"] = hash - api.DB_TRACKS.append(tags) - albumindex = find_album(api.ALBUMS, hash) + album = instances.album_instance.find_album_by_hash(hash) + all_tracks = Get.get_all_tracks() + all_tracks.append(models.Track(tags)) - if albumindex is not None: - album = api.ALBUMS[albumindex] - else: - album_data = create_album(tags, api.DB_TRACKS) + if album is None: + album_data = create_album(tags, all_tracks) album = models.Album(album_data) instances.album_instance.insert_album(album) - api.ALBUMS.append(album) tags["image"] = album.image - upsert_id = instances.tracks_instance.insert_song(tags) - tags["_id"] = {"$oid": str(upsert_id)} - - api.TRACKS.append(models.Track(tags)) + instances.tracks_instance.insert_song(tags) def remove_track(filepath: str) -> None: @@ -76,16 +70,9 @@ def remove_track(filepath: str) -> None: Removes a track from the music dict. """ - try: - trackid = instances.tracks_instance.get_song_by_path(filepath)["_id"]["$oid"] - except TypeError: - print(f"💙 Watchdog Error: Error removing track {filepath} TypeError") - return + filepath = filepath + "k" - track = UseBisection(api.TRACKS, "trackid", [trackid])() - if track is not None: - api.TRACKS.remove(track[0]) - instances.tracks_instance.remove_song_by_id(trackid) + instances.tracks_instance.remove_song_by_filepath(filepath) class Handler(PatternMatchingEventHandler): @@ -151,4 +138,4 @@ watch = OnMyWatch() # TODO # When removing a track, check if there are other tracks in the same album, -# if it was the last one, remove the album. \ No newline at end of file +# if it was the last one, remove the album. diff --git a/server/app/models.py b/server/app/models.py index c65c6494..2bc11fe6 100644 --- a/server/app/models.py +++ b/server/app/models.py @@ -2,6 +2,7 @@ Contains all the models for objects generation and typing. """ from dataclasses import dataclass, field +import random from typing import List from app import helpers @@ -33,8 +34,10 @@ class Track: try: self.trackid = tags["_id"]["$oid"] except KeyError: + self.trackid = "".join( + random.choice("abcdefghijklmnopqrstuvwxyz0123456789") for i in range(20) + ) print("No id") - print(tags) self.title = tags["title"] self.artists = tags["artists"].split(", ") diff --git a/server/app/settings.py b/server/app/settings.py index fb840a26..9cce056e 100644 --- a/server/app/settings.py +++ b/server/app/settings.py @@ -12,7 +12,7 @@ HOME_DIR = os.path.expanduser("~") APP_DIR = os.path.join(HOME_DIR, CONFIG_FOLDER) THUMBS_PATH = os.path.join(APP_DIR, "images", "thumbnails") TEST_DIR = "/home/cwilvx/Music/Link to Music/Chill/Wolftyla Radio" -HOME_DIR = TEST_DIR +# HOME_DIR = TEST_DIR # URL IMG_BASE_URI = "http://127.0.0.1:8900/images/" IMG_ARTIST_URI = IMG_BASE_URI + "artists/"