show recently added items based on by sorting all tracks

.ie. will still show recent items even if they are 3 months old (is this good or bad?)
This commit is contained in:
mungai-njoroge
2024-02-14 23:09:05 +03:00
parent f62fe0ac24
commit ec5889515b
5 changed files with 21 additions and 29 deletions
+1 -3
View File
@@ -10,12 +10,10 @@ parser.add_argument("limit", type=int, required=False, default=7, location="args
class RecentlyAdded(Resource): class RecentlyAdded(Resource):
def get(self): def get(self):
cutoff = 14
args = parser.parse_args() args = parser.parse_args()
limit = args["limit"] limit = args["limit"]
return {"items": get_recent_items(cutoff, limit), "cutoff": cutoff} return {"items": get_recent_items(limit)}
class RecentlyPlayed(Resource): class RecentlyPlayed(Resource):
+16 -25
View File
@@ -34,6 +34,7 @@ def check_is_album_folder(tracks: list[Track]):
def check_is_artist_folder(tracks: list[Track]): def check_is_artist_folder(tracks: list[Track]):
# INFO: flatten artist hashes using "-" as a separator
artisthashes = "-".join(t.artist_hashes for t in tracks).split("-") artisthashes = "-".join(t.artist_hashes for t in tracks).split("-")
return calc_based_on_percent(artisthashes, len(tracks)) return calc_based_on_percent(artisthashes, len(tracks))
@@ -46,24 +47,27 @@ def check_is_track_folder(tracks: list[Track]):
return [create_track(t) for t in tracks] return [create_track(t) for t in tracks]
def check_is_new_artist(artisthash: str): def check_is_new_artist(artisthash: str, timestamp: int):
""" """
Checks if an artist already exists in the library. Checks if an artist already exists in the library.
""" """
if artisthash in older_artists: tracks = filter(
return False lambda t: t.last_mod < timestamp and artisthash in t.artist_hashes,
TrackStore.tracks,
)
return True return next(tracks, None) is None
def check_is_new_album(albumhash: str): def check_is_new_album(albumhash: str, timestamp: int):
""" """
Checks if an album already exists in the library. Checks if an album already exists in the library.
""" """
if albumhash in older_albums: tracks = filter(
return False lambda t: t.last_mod < timestamp and t.albumhash == albumhash, TrackStore.tracks
)
return True return next(tracks, None) is None
def create_track(t: Track): def create_track(t: Track):
@@ -115,7 +119,7 @@ def check_folder_type(group_: group_type) -> str:
}, },
) )
album["help_text"] = ( album["help_text"] = (
"NEW ALBUM" if check_is_new_album(albumhash) else "NEW TRACKS" "NEW ALBUM" if check_is_new_album(albumhash, time) else "NEW TRACKS"
) )
album["time"] = timestamp_to_time_passed(time) album["time"] = timestamp_to_time_passed(time)
@@ -134,7 +138,7 @@ def check_folder_type(group_: group_type) -> str:
artist = serialize_for_card(artist) artist = serialize_for_card(artist)
artist["trackcount"] = trackcount artist["trackcount"] = trackcount
artist["help_text"] = ( artist["help_text"] = (
"NEW ARTIST" if check_is_new_artist(artisthash) else "NEW MUSIC" "NEW ARTIST" if check_is_new_artist(artisthash, time) else "NEW MUSIC"
) )
artist["time"] = timestamp_to_time_passed(time) artist["time"] = timestamp_to_time_passed(time)
@@ -167,25 +171,12 @@ def group_track_by_folders(tracks: Track):
{"folder": folder, "tracks": list(tracks), "time": os.path.getctime(folder)} {"folder": folder, "tracks": list(tracks), "time": os.path.getctime(folder)}
for folder, tracks in groups for folder, tracks in groups
) )
print(groups)
# sort groups by last modified date # sort groups by last modified date
return sorted(groups, key=lambda group: group["time"], reverse=True) return sorted(groups, key=lambda group: group["time"], reverse=True)
def get_recent_items(cutoff_days: int, limit: int = 7): def get_recent_items(limit: int = 7):
timestamp = timestamp_from_days_ago(cutoff_days) tracks = sorted(TrackStore.tracks, key=lambda t: t.created_date)
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 = sorted(tracks, key=lambda t: t.created_date)
groups = group_track_by_folders(tracks) groups = group_track_by_folders(tracks)
recent_items = [] recent_items = []
+1 -1
View File
@@ -153,7 +153,7 @@ class Populate:
tagged_count = 0 tagged_count = 0
fav_tracks = favdb.get_fav_tracks() fav_tracks = favdb.get_fav_tracks()
fav_tracks = "-".join([t[1] for t in fav_tracks]) fav_tracks = set(t[1] for t in fav_tracks)
for file in tqdm(untagged, desc="Reading files"): for file in tqdm(untagged, desc="Reading files"):
if POPULATE_KEY != key: if POPULATE_KEY != key:
+1
View File
@@ -1,6 +1,7 @@
""" """
This library contains the classes and functions related to the watchdog file watcher. This library contains the classes and functions related to the watchdog file watcher.
""" """
import json import json
import os import os
import sqlite3 import sqlite3
+2
View File
@@ -1,6 +1,7 @@
""" """
Prepares the server for use. Prepares the server for use.
""" """
from app.db.sqlite.settings import load_settings from app.db.sqlite.settings import load_settings
from app.setup.files import create_config_dir from app.setup.files import create_config_dir
from app.setup.sqlite import run_migrations, setup_sqlite from app.setup.sqlite import run_migrations, setup_sqlite
@@ -23,6 +24,7 @@ def run_setup():
instance_key = get_random_str() instance_key = get_random_str()
# INFO: Load all tracks, albums, and artists into memory
TrackStore.load_all_tracks(instance_key) TrackStore.load_all_tracks(instance_key)
AlbumStore.load_albums(instance_key) AlbumStore.load_albums(instance_key)
ArtistStore.load_artists(instance_key) ArtistStore.load_artists(instance_key)