diff --git a/app/api/folder.py b/app/api/folder.py index deafdcf5..42c7da0c 100644 --- a/app/api/folder.py +++ b/app/api/folder.py @@ -26,9 +26,11 @@ def get_folder_tree(): data = request.get_json() req_dir = "$home" + tracks_only = False if data is not None: try: req_dir: str = data["folder"] + tracks_only: bool = data["tracks_only"] except KeyError: req_dir = "$home" @@ -56,7 +58,7 @@ def get_folder_tree(): else: req_dir = "/" + req_dir + "/" if not req_dir.startswith("/") else req_dir + "/" - tracks, folders = GetFilesAndDirs(req_dir)() + tracks, folders = GetFilesAndDirs(req_dir, tracks_only=tracks_only)() return { "tracks": tracks, diff --git a/app/lib/folderslib.py b/app/lib/folderslib.py index 393534ca..e43206df 100644 --- a/app/lib/folderslib.py +++ b/app/lib/folderslib.py @@ -44,8 +44,9 @@ class GetFilesAndDirs: Get files and folders from a directory. """ - def __init__(self, path: str) -> None: + def __init__(self, path: str, tracks_only=False) -> None: self.path = path + self.tracks_only = tracks_only def __call__(self) -> tuple[list[Track], list[Folder]]: try: @@ -80,6 +81,9 @@ class GetFilesAndDirs: files = [f["path"] for f in files_] tracks = TrackStore.get_tracks_by_filepaths(files) - folders = get_folders(dirs) + + folders = [] + if not self.tracks_only: + folders = get_folders(dirs) return tracks, folders diff --git a/app/lib/home/recents.py b/app/lib/home/recents.py index 3f4507d8..19c3786d 100644 --- a/app/lib/home/recents.py +++ b/app/lib/home/recents.py @@ -13,6 +13,11 @@ from app.serializers.artist import serialize_for_card from itertools import groupby from datetime import datetime, timedelta +older_albums = set() +older_artists = set() + +group_type = tuple[str, list[Track]] + def timestamp_from_days_ago(days_ago): current_datetime = datetime.now() @@ -24,9 +29,6 @@ def timestamp_from_days_ago(days_ago): return past_timestamp -group_type = tuple[str, list[Track]] - - def calc_based_on_percent(items: list[str], total: int): """ Checks if items is more than 85% of total items. Returns a boolean and the most common item. @@ -34,7 +36,7 @@ def calc_based_on_percent(items: list[str], total: int): most_common = max(items, key=items.count) most_common_count = items.count(most_common) - return most_common_count / total >= 0.85, most_common + return most_common_count / total >= 0.85, most_common, most_common_count def check_is_album_folder(group: group_type): @@ -49,6 +51,30 @@ def check_is_artist_folder(group: group_type): return calc_based_on_percent(artisthashes, len(group_)) +def check_is_new_artist(artisthash: str): + if artisthash in older_artists: + return False + + return True + + +def check_is_new_album(albumhash: str): + if albumhash in older_albums: + return False + + return True + + +def create_track(t: Track): + track = serialize_track(t, to_remove={"created_date"}) + track["help_text"] = "NEW TRACK" + + return { + "type": "track", + "item": track, + } + + def check_is_track_folder(group: group_type): key, group_ = group @@ -56,13 +82,7 @@ def check_is_track_folder(group: group_type): if len(group_) >= 3: return False - return [ - { - "type": "track", - "item": serialize_track(t, to_remove={"created_date"}), - } - for t in group_ - ] + return [create_track(t) for t in group_] def check_folder_type(group_: group_type) -> str: @@ -71,35 +91,48 @@ def check_folder_type(group_: group_type) -> str: key, tracks = group_ if len(tracks) == 1: - return { - "type": "track", - "item": serialize_track(tracks[0], to_remove={"created_date"}), - } + return create_track(tracks[0]) - is_album, albumhash = check_is_album_folder(group_) + is_album, albumhash, _ = check_is_album_folder(group_) if is_album: album = AlbumStore.get_album_by_hash(albumhash) + + if album is None: + return None + + album = album_serializer( + album, + to_remove={ + "genres", + "og_title", + "date", + "duration", + "count", + "albumartists_hashes", + "base_title", + }, + ) + album["help_text"] = ( + "NEW ALBUM" if check_is_new_album(albumhash) else "NEW TRACKS" + ) + return { "type": "album", - "item": album_serializer( - album, - to_remove={ - "genres", - "og_title", - "date", - "duration", - "count", - "albumartists_hashes", - "base_title", - }, - ), + "item": album, } - is_artist, artisthash = check_is_artist_folder(group_) + is_artist, artisthash, trackcount = check_is_artist_folder(group_) if is_artist: artist = ArtistStore.get_artist_by_hash(artisthash) + + if artist is None: + return None + artist = serialize_for_card(artist) - artist["trackcount"] = len(tracks) + artist["trackcount"] = trackcount + artist["help_text"] = ( + "NEW ARTIST" if check_is_new_artist(artisthash) else "NEW MUSIC" + ) return { "type": "artist", @@ -115,6 +148,7 @@ def check_folder_type(group_: group_type) -> str: "item": { "path": key, "count": len(tracks), + "help_text": "NEW MUSIC", }, } ) @@ -131,8 +165,16 @@ def group_track_by_folders(tracks: Track) -> (str, list[Track]): def get_recent_items(cutoff_days: int): timestamp = timestamp_from_days_ago(cutoff_days) + tracks: list[Track] = [] + + for t in TrackStore.tracks: + if t.created_date > timestamp: + tracks.append(t) + continue + + older_albums.add(t.albumhash) + older_artists.add(t.artist_hashes) - tracks = (t for t in TrackStore.tracks if t.created_date > timestamp) tracks = sorted(tracks, key=lambda t: t.created_date) groups = group_track_by_folders(tracks) @@ -143,6 +185,9 @@ def get_recent_items(cutoff_days: int): item = check_folder_type(group) if item not in recent_items: + if not item: + continue + recent_items.append(item) if type(item) == dict else recent_items.extend( item )