diff --git a/package.json b/package.json index d6e85445..5f276402 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dependencies": { "axios": "^0.26.0", "mitt": "^3.0.0", + "pinia": "^2.0.11", "register-service-worker": "^1.7.1", "sass": "^1.49.0", "sass-loader": "^10", diff --git a/server/app/api.py b/server/app/api.py index d20458c8..1f72c315 100644 --- a/server/app/api.py +++ b/server/app/api.py @@ -11,7 +11,8 @@ bp = Blueprint("api", __name__, url_prefix="") home_dir = helpers.home_dir -all_the_f_music = helpers.get_all_songs() +all_the_f_albums = helpers.create_all_albums() +all_the_f_music = helpers.create_all_tracks() def initialize() -> None: diff --git a/server/app/functions.py b/server/app/functions.py index 0df31d97..00fdf044 100644 --- a/server/app/functions.py +++ b/server/app/functions.py @@ -20,6 +20,7 @@ from app import helpers from app import instances from app import api from app import models +import urllib def populate(): @@ -41,7 +42,7 @@ def populate(): if tags is not None: instances.songs_instance.insert_song(tags) - api.all_the_f_music = helpers.get_all_songs() + api.all_the_f_music = helpers.create_all_tracks() print("\n check done") end = time.time() @@ -165,7 +166,7 @@ def extract_thumb(audio_file_path: str) -> str: """ Extracts the thumbnail from an audio file. Returns the path to the thumbnail. """ - webp_path = audio_file_path.split("/")[-1] + ".webp" + webp_path = urllib.parse.quote_plus(audio_file_path.split("/")[-1] + ".webp") img_path = os.path.join(helpers.app_dir, "images", "thumbnails", webp_path) if os.path.exists(img_path): @@ -366,17 +367,11 @@ def get_all_albums(): album["count"] = len(album_tracks) album["duration"] = helpers.get_album_duration(album_tracks) album["date"] = album_tracks[0]["date"] - album["artistimage"] = ( - "http://127.0.0.1:8900/images/artists/" - + album_tracks[0]["albumartist"].replace("/", "::") - + ".webp" + album["artistimage"] = urllib.parse.quote_plus( + album_tracks[0]["albumartist"] + ".webp" ) - if len(album_tracks) == 1: - album["image"] = extract_thumb(album_tracks[0]["filepath"]) - - if len(album_tracks) > 1: - album["image"] = helpers.get_album_image(album_tracks) + album["image"] = helpers.get_album_image(album_tracks) instances.album_instance.insert_album(album) diff --git a/server/app/helpers.py b/server/app/helpers.py index 2df97a0a..7ea82e9b 100644 --- a/server/app/helpers.py +++ b/server/app/helpers.py @@ -146,7 +146,7 @@ def create_config_dir() -> None: os.chmod(path, 0o755) -def get_all_songs() -> List[models.Track]: +def create_all_tracks() -> List[models.Track]: """ Gets all songs under the ~/ directory. """ @@ -160,11 +160,26 @@ def get_all_songs() -> List[models.Track]: except FileNotFoundError: instances.songs_instance.remove_song_by_filepath(track["filepath"]) + album = instances.album_instance.get_album_by_name(track['album', track['albumartist']]) + + if album is None: + track['albumid'] = album['albumid'] + track['image'] = album['image'] tracks.append(models.Track(track)) return tracks +def create_all_albums() -> List[models.Album]: + """Creates album objects for all albums""" + albums: list[models.Album] = [] + + for album in instances.album_instance.get_all_albums(): + albums.append(models.Album(album)) + + return albums + + def extract_colors(image) -> list: """Extracts 2 of the most dominant colors from an image.""" try: @@ -208,4 +223,4 @@ def get_album_image(album: list) -> str: if img is not None: return img - return functions.use_defaults() \ No newline at end of file + return functions.use_defaults() diff --git a/server/app/models.py b/server/app/models.py index 5d8bb68d..e5f31ba7 100644 --- a/server/app/models.py +++ b/server/app/models.py @@ -237,6 +237,7 @@ class Track: """ trackid: str + albumid: str title: str artists: str albumartist: str @@ -244,7 +245,6 @@ class Track: folder: str filepath: str length: int - date: int genre: str bitrate: int image: str @@ -260,7 +260,6 @@ class Track: self.folder = tags["folder"] self.filepath = tags["filepath"] self.length = tags["length"] - self.date = tags["date"] self.genre = tags["genre"] self.bitrate = tags["bitrate"] self.image = "http://127.0.0.1:8900/images/thumbnails/" + tags["image"] @@ -287,6 +286,13 @@ class Albums(Mongo): upsert=True, ).upserted_id + def get_all_albums(self) -> list: + """ + Returns all the albums in the database. + """ + albums = self.collection.find() + return convert_many(albums) + def get_album_by_id(self, id: str) -> dict: """ Returns a single album matching the id in the query params. @@ -294,11 +300,11 @@ class Albums(Mongo): album = self.collection.find_one({"_id": ObjectId(id)}) return convert_one(album) - def get_album_by_name(self, name: str) -> dict: + def get_album_by_name(self, name: str, artist: str) -> dict: """ Returns a single album matching the name in the query params. """ - album = self.collection.find_one({"album": name}) + album = self.collection.find_one({"album": name, "artist": artist}) return convert_one(album) def get_album_by_artist(self, name: str) -> dict: @@ -318,15 +324,18 @@ class Album: albumid: str album: str artist: str - albumartist: str - year: int + count: int + duration: int + date: int + artistimage: str image: str - tracks: list def __init__(self, tags): self.albumid = tags["_id"]["$oid"] self.album = tags["album"] self.artist = tags["artist"] - self.albumartist = tags["albumartist"] - self.year = tags["year"] - self.image = "" \ No newline at end of file + self.count = tags["count"] + self.duration = tags["duration"] + self.date = tags["date"] + self.artistimage = tags["artistimage"] + self.image = tags["image"] diff --git a/src/App.vue b/src/App.vue index 6b4ee05a..a677adb1 100644 --- a/src/App.vue +++ b/src/App.vue @@ -3,7 +3,6 @@