From 6931cd926c34e33b7c298612fe5405fb3871d071 Mon Sep 17 00:00:00 2001 From: geoffrey45 Date: Wed, 2 Mar 2022 15:12:13 +0300 Subject: [PATCH] sort files by creation date --- server/app/api.py | 22 +++++++++++++---- server/app/helpers.py | 14 ++++++++--- server/app/instances.py | 2 ++ server/app/models.py | 54 ++++++++++++++++++++++++++++++++++++----- 4 files changed, 77 insertions(+), 15 deletions(-) diff --git a/server/app/api.py b/server/app/api.py index bc28ec84..1095dd23 100644 --- a/server/app/api.py +++ b/server/app/api.py @@ -255,6 +255,7 @@ def get_folder_tree(folder: str): dir_content = os.scandir(os.path.join(home_dir, req_dir)) folders = [] + files = [] for entry in dir_content: if entry.is_dir() and not entry.name.startswith("."): @@ -269,11 +270,22 @@ def get_folder_tree(folder: str): folders.append(_dir) + if entry.is_file(): + if entry.name.endswith(".flac") or entry.name.endswith(".mp3"): + files.append(entry) + + files.sort(key=lambda x: os.path.getmtime(x.path)) + songs = [] - for track in all_the_f_music: - if track.folder == req_dir: - songs.append(track) + for entry in files: + for track in all_the_f_music: + if track.filepath == entry.path: + songs.append(track) + + # for track in all_the_f_music: + # if track.filepath == req_dir: + # songs.append(track) return { "files": helpers.remove_duplicates(songs), @@ -348,10 +360,10 @@ def send_track_file(trackid): return "File not found", 404 -@bp.route('/sample') +@bp.route("/sample") def get_sample_track(): """ Returns a sample track object. """ - return instances.songs_instance.get_song_by_album("Legends Never Die", "Juice WRLD") \ No newline at end of file + return instances.songs_instance.get_song_by_album("Legends Never Die", "Juice WRLD") diff --git a/server/app/helpers.py b/server/app/helpers.py index b443f683..ab509626 100644 --- a/server/app/helpers.py +++ b/server/app/helpers.py @@ -45,6 +45,8 @@ def reindex_tracks(): while flag is False: functions.populate() functions.populate_images() + # functions.save_t_colors() + time.sleep(300) @@ -153,17 +155,21 @@ def get_all_songs() -> List[models.Track]: for track in instances.songs_instance.get_all_songs(): try: - os.chmod(os.path.join(track["filepath"]), 0o755) + os.chmod(track["filepath"], 0o755) except FileNotFoundError: instances.songs_instance.remove_song_by_filepath(track["filepath"]) - tracks.append(functions.create_track_class(track)) + tracks.append(models.Track(track)) return tracks def extract_colors(image) -> list: - colors = sorted(colorgram.extract(image, 2), key=lambda c: c.hsl.h) + """Extracts 2 of the most dominant colors from an image.""" + try: + colors = sorted(colorgram.extract(image, 2), key=lambda c: c.hsl.h) + except OSError: + return [] formatted_colors = [] @@ -184,4 +190,4 @@ def get_album_duration(album: List[models.Track]) -> int: for track in album: album_duration += track.length - return album_duration \ No newline at end of file + return album_duration diff --git a/server/app/instances.py b/server/app/instances.py index 071fa057..954bc7fc 100644 --- a/server/app/instances.py +++ b/server/app/instances.py @@ -1,5 +1,7 @@ from app.models import AllSongs from app.models import Artists +from app.models import TrackColors songs_instance = AllSongs() artist_instance = Artists() +track_color_instance = TrackColors() \ No newline at end of file diff --git a/server/app/models.py b/server/app/models.py index 047c1ac4..bf5b59f5 100644 --- a/server/app/models.py +++ b/server/app/models.py @@ -53,8 +53,9 @@ class Artists(Mongo): """ Inserts an artist into the database. """ - self.collection.update_one(artist_obj, {"$set": artist_obj}, upsert=True).upserted_id - + self.collection.update_one( + artist_obj, {"$set": artist_obj}, upsert=True + ).upserted_id def get_all_artists(self) -> list: """ @@ -191,7 +192,7 @@ class AllSongs(Mongo): except: return False - def remove_song_by_id(self, id:str): + def remove_song_by_id(self, id: str): """ Removes a single track from the database. Returns a boolean indicating success or failure of the operation. """ @@ -201,6 +202,34 @@ class AllSongs(Mongo): except: return False + +class TrackColors(Mongo): + """ + The class for all track-related database operations. + """ + + def __init__(self): + super(TrackColors, self).__init__("TRACK_COLORS") + self.collection = self.db["TRACK_COLORS"] + + def insert_track_color(self, track_color: dict) -> None: + """ + Inserts a new track object into the database. + """ + return self.collection.update_one( + {"filepath": track_color["filepath"]}, + {"$set": track_color}, + upsert=True, + ).upserted_id + + def get_track_color_by_track(self, filepath: str) -> dict: + """ + Returns a track color object by its filepath. + """ + track_color = self.collection.find_one({"filepath": filepath}) + return convert_one_to_json(track_color) + + @dataclass class Track: """ @@ -213,6 +242,7 @@ class Track: albumartist: str album: str folder: str + filepath: str length: int date: int genre: str @@ -221,6 +251,18 @@ class Track: tracknumber: int discnumber: int - def __post_init__(self): - self.artists = self.artists.split(", ") - self.image = "http://127.0.0.1:8900/images/thumbnails/" + self.image + def __init__(self, tags): + self.trackid = tags["_id"]["$oid"] + self.title = tags["title"] + self.artists = tags["artists"].split(", ") + self.albumartist = tags["albumartist"] + self.album = tags["album"] + 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"] + self.tracknumber = tags["tracknumber"] + self.discnumber = tags["discnumber"]