sort files by creation date

This commit is contained in:
geoffrey45
2022-03-02 15:12:13 +03:00
parent 59646ce7f1
commit 6931cd926c
4 changed files with 77 additions and 15 deletions
+17 -5
View File
@@ -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")
return instances.songs_instance.get_song_by_album("Legends Never Die", "Juice WRLD")
+10 -4
View File
@@ -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
return album_duration
+2
View File
@@ -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()
+48 -6
View File
@@ -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"]