mirror of
https://github.com/tcsenpai/swingmusic.git
synced 2025-07-20 16:40:07 +00:00
fix playlist last update
+ create a recently added playlist on get api
This commit is contained in:
parent
9dff629e1f
commit
5a420214f2
@ -13,6 +13,5 @@ class RecentlyAdded(Resource):
|
|||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
limit = args["limit"]
|
limit = args["limit"]
|
||||||
print(limit)
|
|
||||||
|
|
||||||
return {"items": get_recent_items(cutoff)[:limit], "cutoff": cutoff}
|
return {"items": get_recent_items(cutoff, limit), "cutoff": cutoff}
|
||||||
|
@ -12,6 +12,7 @@ from app import models
|
|||||||
from app.db.sqlite.playlists import SQLitePlaylistMethods
|
from app.db.sqlite.playlists import SQLitePlaylistMethods
|
||||||
from app.lib import playlistlib
|
from app.lib import playlistlib
|
||||||
from app.lib.albumslib import sort_by_track_no
|
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.models.track import Track
|
||||||
from app.store.albums import AlbumStore
|
from app.store.albums import AlbumStore
|
||||||
from app.store.tracks import TrackStore
|
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 = request.args.get("no_tracks", False)
|
||||||
no_tracks = no_tracks == "true"
|
no_tracks = no_tracks == "true"
|
||||||
|
|
||||||
|
is_recently_added = playlistid == "recentlyadded"
|
||||||
|
|
||||||
|
if not is_recently_added:
|
||||||
playlist = PL.get_playlist_by_id(int(playlistid))
|
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:
|
if playlist is None:
|
||||||
return {"msg": "Playlist not found"}, 404
|
return {"msg": "Playlist not found"}, 404
|
||||||
|
|
||||||
|
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 = TrackStore.get_tracks_by_trackhashes(list(playlist.trackhashes))
|
||||||
tracks = remove_duplicates(tracks)
|
|
||||||
|
|
||||||
|
tracks = remove_duplicates(tracks)
|
||||||
duration = sum(t.duration for t in tracks)
|
duration = sum(t.duration for t in tracks)
|
||||||
playlist.last_updated = date_string_to_time_passed(playlist.last_updated)
|
playlist.last_updated = date_string_to_time_passed(playlist.last_updated)
|
||||||
|
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
import json
|
import json
|
||||||
from collections import OrderedDict
|
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.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.dates import create_new_date
|
||||||
from app.utils.threading import background
|
|
||||||
|
|
||||||
|
|
||||||
class SQLitePlaylistMethods:
|
class SQLitePlaylistMethods:
|
||||||
|
@ -36,7 +36,7 @@ def calc_based_on_percent(items: list[str], total: int):
|
|||||||
most_common = max(items, key=items.count)
|
most_common = max(items, key=items.count)
|
||||||
most_common_count = items.count(most_common)
|
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):
|
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)
|
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)
|
timestamp = timestamp_from_days_ago(cutoff_days)
|
||||||
tracks: list[Track] = []
|
tracks: list[Track] = []
|
||||||
|
|
||||||
@ -181,7 +181,7 @@ def get_recent_items(cutoff_days: int):
|
|||||||
|
|
||||||
recent_items = []
|
recent_items = []
|
||||||
|
|
||||||
for group in groups:
|
for group in groups[:limit]:
|
||||||
item = check_folder_type(group)
|
item = check_folder_type(group)
|
||||||
|
|
||||||
if item not in recent_items:
|
if item not in recent_items:
|
||||||
@ -192,4 +192,11 @@ def get_recent_items(cutoff_days: int):
|
|||||||
item
|
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]
|
||||||
|
@ -4,25 +4,24 @@ from datetime import datetime
|
|||||||
_format = "%Y-%m-%d %H:%M:%S"
|
_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"
|
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.
|
:return: A string of the current date and time.
|
||||||
"""
|
"""
|
||||||
now = datetime.now()
|
if not date:
|
||||||
return now.strftime(_format)
|
date = datetime.now()
|
||||||
|
|
||||||
|
return date.strftime(_format)
|
||||||
|
|
||||||
|
|
||||||
def date_string_to_time_passed(prev_date: str) -> str:
|
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.
|
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()
|
now = datetime.now().timestamp()
|
||||||
then = datetime.strptime(prev_date, _format)
|
then = datetime.strptime(prev_date, _format).timestamp()
|
||||||
|
|
||||||
diff = now - then
|
diff = now - then
|
||||||
seconds = diff.seconds
|
|
||||||
|
|
||||||
now = pendulum.now()
|
now = pendulum.now()
|
||||||
return now.subtract(seconds=seconds).diff_for_humans()
|
return now.subtract(seconds=diff).diff_for_humans()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user