fix playlist last update

+ create a recently added playlist on get api
This commit is contained in:
mungai-njoroge 2023-12-03 12:56:56 +03:00
parent 9dff629e1f
commit 5a420214f2
5 changed files with 41 additions and 21 deletions

View File

@ -13,6 +13,5 @@ class RecentlyAdded(Resource):
args = parser.parse_args()
limit = args["limit"]
print(limit)
return {"items": get_recent_items(cutoff)[:limit], "cutoff": cutoff}
return {"items": get_recent_items(cutoff, limit), "cutoff": cutoff}

View File

@ -12,6 +12,7 @@ from app import models
from app.db.sqlite.playlists import SQLitePlaylistMethods
from app.lib import playlistlib
from app.lib.albumslib import sort_by_track_no
from app.lib.home.recents import get_recent_tracks
from app.models.track import Track
from app.store.albums import AlbumStore
from app.store.tracks import TrackStore
@ -206,14 +207,31 @@ def get_playlist(playlistid: str):
no_tracks = request.args.get("no_tracks", False)
no_tracks = no_tracks == "true"
playlist = PL.get_playlist_by_id(int(playlistid))
is_recently_added = playlistid == "recentlyadded"
if not is_recently_added:
playlist = PL.get_playlist_by_id(int(playlistid))
else:
playlist = models.Playlist(
id="recentlyadded",
name="Recently Added",
image=None,
last_updated="Now",
settings={},
trackhashes=[],
)
if playlist is None:
return {"msg": "Playlist not found"}, 404
tracks = TrackStore.get_tracks_by_trackhashes(list(playlist.trackhashes))
tracks = remove_duplicates(tracks)
if is_recently_added:
tracks = get_recent_tracks(cutoff_days=14)
date = datetime.fromtimestamp(tracks[0].created_date)
playlist.last_updated = create_new_date(date)
else:
tracks = TrackStore.get_tracks_by_trackhashes(list(playlist.trackhashes))
tracks = remove_duplicates(tracks)
duration = sum(t.duration for t in tracks)
playlist.last_updated = date_string_to_time_passed(playlist.last_updated)

View File

@ -1,11 +1,8 @@
import json
from collections import OrderedDict
from app.db.sqlite.tracks import SQLiteTrackMethods
from app.db.sqlite.utils import SQLiteManager, tuple_to_playlist, tuples_to_playlists
from app.models import Artist
from app.utils.dates import create_new_date
from app.utils.threading import background
class SQLitePlaylistMethods:

View File

@ -36,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, most_common_count
return most_common_count / total >= 0.7, most_common, most_common_count
def check_is_album_folder(group: group_type):
@ -163,7 +163,7 @@ def group_track_by_folders(tracks: Track) -> (str, list[Track]):
return sorted(groups, key=lambda g: os.path.getctime(g[0]), reverse=True)
def get_recent_items(cutoff_days: int):
def get_recent_items(cutoff_days: int, limit: int = 7):
timestamp = timestamp_from_days_ago(cutoff_days)
tracks: list[Track] = []
@ -181,7 +181,7 @@ def get_recent_items(cutoff_days: int):
recent_items = []
for group in groups:
for group in groups[:limit]:
item = check_folder_type(group)
if item not in recent_items:
@ -192,4 +192,11 @@ def get_recent_items(cutoff_days: int):
item
)
return recent_items
return recent_items[:limit]
def get_recent_tracks(cutoff_days: int):
tracks = sorted(TrackStore.tracks, key=lambda t: t.created_date, reverse=True)
timestamp = timestamp_from_days_ago(cutoff_days)
return [t for t in tracks if t.created_date > timestamp]

View File

@ -4,25 +4,24 @@ from datetime import datetime
_format = "%Y-%m-%d %H:%M:%S"
def create_new_date():
def create_new_date(date: datetime = None) -> str:
"""
Creates a new date and time string in the format of "YYYY-MM-DD HH:MM:SS"
:return: A string of the current date and time.
"""
now = datetime.now()
return now.strftime(_format)
if not date:
date = datetime.now()
return date.strftime(_format)
def date_string_to_time_passed(prev_date: str) -> str:
"""
Converts a date string to time passed. e.g. 2 minutes ago, 1 hour ago, yesterday, 2 days ago, 2 weeks ago, etc.
"""
now = datetime.now()
then = datetime.strptime(prev_date, _format)
now = datetime.now().timestamp()
then = datetime.strptime(prev_date, _format).timestamp()
diff = now - then
seconds = diff.seconds
now = pendulum.now()
return now.subtract(seconds=seconds).diff_for_humans()
return now.subtract(seconds=diff).diff_for_humans()