more edits

- watchdog still broken
This commit is contained in:
geoffrey45
2022-06-17 10:22:53 +03:00
parent 1cc7c933b7
commit 7718a7c99f
4 changed files with 29 additions and 38 deletions
+14 -13
View File
@@ -1,6 +1,7 @@
""" """
This library contains all the functions related to albums. This library contains all the functions related to albums.
""" """
from pprint import pprint
import random import random
from typing import List from typing import List
@@ -33,7 +34,6 @@ def validate() -> None:
""" """
def find_album(albums: List[models.Album], hash: str) -> int | None: def find_album(albums: List[models.Album], hash: str) -> int | None:
""" """
Finds an album by album title and artist. Finds an album by album title and artist.
@@ -114,26 +114,28 @@ class GetAlbumTracks:
and album artist. 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.hash = albumhash
self.tracks = tracklist 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 = [] tracks = []
index = trackslib.find_track(self.tracks, self.hash) tracks = helpers.UseBisection(self.tracks, "albumhash", [self.hash])()
while index is not None: pprint(tracks)
track = self.tracks[index]
tracks.append(track) # while index is not None:
self.tracks.remove(track) # track = self.tracks[index]
index = trackslib.find_track(self.tracks, self.hash) # tracks.append(track)
# self.tracks.remove(track)
# index = helpers.UseBisection(self.tracks, "albumhash", [self.hash])()
return tracks return tracks
def get_album_tracks(album: str, artist: str) -> List: def get_album_tracks(tracklist: List[models.Track], hash: str) -> List:
return GetAlbumTracks(album, artist).find_tracks() return GetAlbumTracks(tracklist, hash)()
def create_album(track: dict, tracklist: list) -> dict: def create_album(track: dict, tracklist: list) -> dict:
@@ -144,7 +146,6 @@ def create_album(track: dict, tracklist: list) -> dict:
"title": track["album"], "title": track["album"],
"artist": track["albumartist"], "artist": track["albumartist"],
} }
albumhash = helpers.create_album_hash(album["title"], album["artist"]) albumhash = helpers.create_album_hash(album["title"], album["artist"])
album_tracks = get_album_tracks(tracklist, albumhash) album_tracks = get_album_tracks(tracklist, albumhash)
+10 -23
View File
@@ -7,9 +7,8 @@ import time
from app import api from app import api
from app import instances from app import instances
from app import models 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 create_album
from app.lib.albumslib import find_album
from app.lib.taglib import get_tags from app.lib.taglib import get_tags
from watchdog.events import PatternMatchingEventHandler from watchdog.events import PatternMatchingEventHandler
from watchdog.observers import Observer from watchdog.observers import Observer
@@ -51,24 +50,19 @@ def add_track(filepath: str) -> None:
if tags is not None: if tags is not None:
hash = create_album_hash(tags["album"], tags["albumartist"]) hash = create_album_hash(tags["album"], tags["albumartist"])
tags["albumhash"] = hash 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: if album is None:
album = api.ALBUMS[albumindex] album_data = create_album(tags, all_tracks)
else:
album_data = create_album(tags, api.DB_TRACKS)
album = models.Album(album_data) album = models.Album(album_data)
instances.album_instance.insert_album(album) instances.album_instance.insert_album(album)
api.ALBUMS.append(album)
tags["image"] = album.image tags["image"] = album.image
upsert_id = instances.tracks_instance.insert_song(tags) instances.tracks_instance.insert_song(tags)
tags["_id"] = {"$oid": str(upsert_id)}
api.TRACKS.append(models.Track(tags))
def remove_track(filepath: str) -> None: def remove_track(filepath: str) -> None:
@@ -76,16 +70,9 @@ def remove_track(filepath: str) -> None:
Removes a track from the music dict. Removes a track from the music dict.
""" """
try: filepath = filepath + "k"
trackid = instances.tracks_instance.get_song_by_path(filepath)["_id"]["$oid"]
except TypeError:
print(f"💙 Watchdog Error: Error removing track {filepath} TypeError")
return
track = UseBisection(api.TRACKS, "trackid", [trackid])() instances.tracks_instance.remove_song_by_filepath(filepath)
if track is not None:
api.TRACKS.remove(track[0])
instances.tracks_instance.remove_song_by_id(trackid)
class Handler(PatternMatchingEventHandler): class Handler(PatternMatchingEventHandler):
@@ -151,4 +138,4 @@ watch = OnMyWatch()
# TODO # TODO
# When removing a track, check if there are other tracks in the same album, # When removing a track, check if there are other tracks in the same album,
# if it was the last one, remove the album. # if it was the last one, remove the album.
+4 -1
View File
@@ -2,6 +2,7 @@
Contains all the models for objects generation and typing. Contains all the models for objects generation and typing.
""" """
from dataclasses import dataclass, field from dataclasses import dataclass, field
import random
from typing import List from typing import List
from app import helpers from app import helpers
@@ -33,8 +34,10 @@ class Track:
try: try:
self.trackid = tags["_id"]["$oid"] self.trackid = tags["_id"]["$oid"]
except KeyError: except KeyError:
self.trackid = "".join(
random.choice("abcdefghijklmnopqrstuvwxyz0123456789") for i in range(20)
)
print("No id") print("No id")
print(tags)
self.title = tags["title"] self.title = tags["title"]
self.artists = tags["artists"].split(", ") self.artists = tags["artists"].split(", ")
+1 -1
View File
@@ -12,7 +12,7 @@ HOME_DIR = os.path.expanduser("~")
APP_DIR = os.path.join(HOME_DIR, CONFIG_FOLDER) APP_DIR = os.path.join(HOME_DIR, CONFIG_FOLDER)
THUMBS_PATH = os.path.join(APP_DIR, "images", "thumbnails") THUMBS_PATH = os.path.join(APP_DIR, "images", "thumbnails")
TEST_DIR = "/home/cwilvx/Music/Link to Music/Chill/Wolftyla Radio" TEST_DIR = "/home/cwilvx/Music/Link to Music/Chill/Wolftyla Radio"
HOME_DIR = TEST_DIR # HOME_DIR = TEST_DIR
# URL # URL
IMG_BASE_URI = "http://127.0.0.1:8900/images/" IMG_BASE_URI = "http://127.0.0.1:8900/images/"
IMG_ARTIST_URI = IMG_BASE_URI + "artists/" IMG_ARTIST_URI = IMG_BASE_URI + "artists/"