From fcfd0c793623c5e7fece4ca647f718968580ba57 Mon Sep 17 00:00:00 2001 From: geoffrey45 Date: Mon, 21 Mar 2022 00:59:51 +0300 Subject: [PATCH] create the lib module --- server/app/lib/__init__.py | 0 server/app/lib/albumslib.py | 101 ++++++++++++++++++++++++++++++++++ server/app/lib/playlistlib.py | 24 ++++++++ server/app/lib/searchlib.py | 26 +++++++++ server/app/lib/trackslib.py | 46 ++++++++++++++++ 5 files changed, 197 insertions(+) create mode 100644 server/app/lib/__init__.py create mode 100644 server/app/lib/albumslib.py create mode 100644 server/app/lib/playlistlib.py create mode 100644 server/app/lib/searchlib.py create mode 100644 server/app/lib/trackslib.py diff --git a/server/app/lib/__init__.py b/server/app/lib/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/server/app/lib/albumslib.py b/server/app/lib/albumslib.py new file mode 100644 index 00000000..e67b5190 --- /dev/null +++ b/server/app/lib/albumslib.py @@ -0,0 +1,101 @@ +import urllib +from typing import List +from app import models, functions, helpers +from app.lib import trackslib +from app import api + + +@helpers.background +def create_everything() -> List[models.Track]: + """ + Creates album objects for all albums and returns + a list of track objects + """ + albums: list[models.Album] = functions.get_all_albums() + + api.ALBUMS.clear() + api.ALBUMS.extend(albums) + trackslib.create_all_tracks() + + + +def get_album_duration(album: list) -> int: + """ + Gets the duration of an album. + """ + + album_duration = 0 + + for track in album: + album_duration += track["length"] + + return album_duration + + +def get_album_image(album: list) -> str: + """ + Gets the image of an album. + """ + + for track in album: + img = functions.extract_thumb(track["filepath"]) + + if img is not None: + return img + + return functions.use_defaults() + + +def get_album_tracks(album: str, artist: str) -> List: + return [ + track + for track in api.DB_TRACKS + if track["album"] == album and track["albumartist"] == artist + ] + + +def create_album(track) -> models.Album: + """ + Generates and returns an album object from a track object. + """ + album = { + "album": track["album"], + "artist": track["albumartist"], + } + + album_tracks = get_album_tracks(album["album"], album["artist"]) + + album["count"] = len(album_tracks) + album["duration"] = get_album_duration(album_tracks) + album["date"] = album_tracks[0]["date"] + album["artistimage"] = urllib.parse.quote_plus( + album_tracks[0]["albumartist"] + ".webp" + ) + + album["image"] = get_album_image(album_tracks) + + return models.Album(album) + + +def find_album(albumtitle, artist): + for album in api.ALBUMS: + if album.album == albumtitle and album.artist == artist: + return album + + +def search_albums_by_name(query: str) -> List[models.Album]: + """ + Searches albums by album name. + """ + title_albums: List[models.Album] = [] + artist_albums: List[models.Album] = [] + + for album in api.ALBUMS: + if query.lower() in album.album.lower(): + title_albums.append(album) + + for album in api.ALBUMS: + if query.lower() in album.artist.lower(): + artist_albums.append(album) + + return [*title_albums, *artist_albums] diff --git a/server/app/lib/playlistlib.py b/server/app/lib/playlistlib.py new file mode 100644 index 00000000..10fa97e0 --- /dev/null +++ b/server/app/lib/playlistlib.py @@ -0,0 +1,24 @@ +from app import api, instances, models +from app.lib import trackslib + + +def add_track(playlistid: str, trackid: str): + """ + Adds a track to a playlist in the api.PLAYLISTS dict and to the database. + """ + for playlist in api.PLAYLISTS: + if playlist.playlistid == playlistid: + track = trackslib.get_track_by_id(trackid) + playlist.tracks.append(track) + + instances.playlist_instance.add_track_to_playlist(playlistid, track) + + + + +def create_all_playlists(): + """ + Gets all playlists from the database. + """ + for playlist in instances.playlist_instance.get_all_playlists(): + api.PLAYLISTS.append(models.Playlist(playlist)) diff --git a/server/app/lib/searchlib.py b/server/app/lib/searchlib.py new file mode 100644 index 00000000..cc444ef7 --- /dev/null +++ b/server/app/lib/searchlib.py @@ -0,0 +1,26 @@ +from typing import List +from app import models, helpers +from app.lib import albumslib +from app import api + + +def get_tracks(query: str) -> List[models.Track]: + """ + Gets all songs with a given title. + """ + tracks = [track for track in api.TRACKS if query.lower() in track.title.lower()] + return helpers.remove_duplicates(tracks) + + +def get_search_albums(query: str) -> List[models.Album]: + """ + Gets all songs with a given album. + """ + return albumslib.search_albums_by_name(query) + + +def get_artists(artist: str) -> List[models.Track]: + """ + Gets all songs with a given artist. + """ + return [track for track in api.TRACKS if artist.lower() in str(track.artists).lower()] diff --git a/server/app/lib/trackslib.py b/server/app/lib/trackslib.py new file mode 100644 index 00000000..5c5419ed --- /dev/null +++ b/server/app/lib/trackslib.py @@ -0,0 +1,46 @@ +import os +from typing import List +from app import models, instances +from app.lib import albumslib +from app.helpers import remove_duplicates +from app import api + +def create_all_tracks() -> List[models.Track]: + """ + Gets all songs under the ~/ directory. + """ + print("Getting all songs...") + tracks: list[models.Track] = [] + + for track in api.DB_TRACKS: + try: + os.chmod(track["filepath"], 0o755) + except FileNotFoundError: + instances.songs_instance.remove_song_by_filepath(track["filepath"]) + + album = albumslib.find_album(track["album"], track["albumartist"]) + + track["image"] = album.image + + tracks.append(models.Track(track)) + + api.TRACKS.clear() + api.TRACKS.extend(tracks) + + +def get_album_tracks(albumname, artist): + """Returns api tracks matching an album""" + _tracks: List[models.Track] = [] + + for track in api.TRACKS: + if track.album == albumname and track.albumartist == artist: + _tracks.append(track) + + return remove_duplicates(_tracks) + + +def get_track_by_id(trackid: str) -> models.Track: + """Returns api track matching an id""" + for track in api.TRACKS: + if track.id == trackid: + return track \ No newline at end of file