mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-05 04:53:01 +00:00
fix: limited items on recently played
This commit is contained in:
+161
-138
@@ -2,6 +2,7 @@ from datetime import datetime
|
|||||||
import os
|
import os
|
||||||
from app.db.userdata import FavoritesTable, PlaylistTable, ScrobbleTable
|
from app.db.userdata import FavoritesTable, PlaylistTable, ScrobbleTable
|
||||||
|
|
||||||
|
from app.models.logger import TrackLog
|
||||||
from app.models.playlist import Playlist
|
from app.models.playlist import Playlist
|
||||||
from app.serializers.track import serialize_track
|
from app.serializers.track import serialize_track
|
||||||
from app.serializers.album import album_serializer
|
from app.serializers.album import album_serializer
|
||||||
@@ -23,7 +24,6 @@ from app.store.artists import ArtistStore
|
|||||||
|
|
||||||
def get_recently_played(limit=7):
|
def get_recently_played(limit=7):
|
||||||
# TODO: Paginate this
|
# TODO: Paginate this
|
||||||
entries = ScrobbleTable.get_all(0, 200)
|
|
||||||
items = []
|
items = []
|
||||||
added = set()
|
added = set()
|
||||||
|
|
||||||
@@ -32,171 +32,194 @@ def get_recently_played(limit=7):
|
|||||||
{"name": "recentlyplayed", "handler": get_recently_played_playlist},
|
{"name": "recentlyplayed", "handler": get_recently_played_playlist},
|
||||||
]
|
]
|
||||||
|
|
||||||
for entry in entries:
|
def create_items(entries: list[TrackLog]):
|
||||||
if len(items) >= limit:
|
for entry in entries:
|
||||||
break
|
if len(items) >= limit:
|
||||||
|
break
|
||||||
|
|
||||||
if entry.source in added:
|
if entry.source in added:
|
||||||
continue
|
|
||||||
|
|
||||||
added.add(entry.source)
|
|
||||||
|
|
||||||
if entry.type == "album":
|
|
||||||
album = AlbumStore.get_album_by_hash(entry.type_src)
|
|
||||||
|
|
||||||
if album is None:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
album = album_serializer(
|
added.add(entry.source)
|
||||||
album,
|
|
||||||
to_remove={
|
|
||||||
"genres",
|
|
||||||
"date",
|
|
||||||
"count",
|
|
||||||
"duration",
|
|
||||||
"albumartists_hashes",
|
|
||||||
"og_title",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
album["help_text"] = "album"
|
|
||||||
album["time"] = timestamp_to_time_passed(entry.timestamp)
|
|
||||||
|
|
||||||
items.append(
|
if entry.type == "album":
|
||||||
{
|
album = AlbumStore.get_album_by_hash(entry.type_src)
|
||||||
"type": "album",
|
|
||||||
"item": album,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
|
|
||||||
if entry.type == "artist":
|
if album is None:
|
||||||
artist = ArtistStore.get_artist_by_hash(entry.type_src)
|
continue
|
||||||
|
|
||||||
if artist is None:
|
album = album_serializer(
|
||||||
continue
|
album,
|
||||||
|
to_remove={
|
||||||
artist = serialize_for_card(artist)
|
"genres",
|
||||||
artist["help_text"] = "artist"
|
"date",
|
||||||
artist["time"] = timestamp_to_time_passed(entry.timestamp)
|
"count",
|
||||||
|
"duration",
|
||||||
items.append(
|
"albumartists_hashes",
|
||||||
{
|
"og_title",
|
||||||
"type": "artist",
|
|
||||||
"item": artist,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
continue
|
|
||||||
|
|
||||||
if entry.type == "folder":
|
|
||||||
folder = entry.type_src
|
|
||||||
|
|
||||||
if not folder:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if not folder.endswith("/"):
|
|
||||||
folder += "/"
|
|
||||||
|
|
||||||
is_home_dir = entry.type_src == "$home"
|
|
||||||
|
|
||||||
if is_home_dir:
|
|
||||||
folder = os.path.expanduser("~")
|
|
||||||
|
|
||||||
# print(folder)
|
|
||||||
# folder = os.path.join("/", folder, "")
|
|
||||||
# print(folder)
|
|
||||||
# count = len([t for t in TrackStore.tracks if t.folder == folder])
|
|
||||||
count = FolderStore.count_tracks_containing_paths([folder])
|
|
||||||
items.append(
|
|
||||||
{
|
|
||||||
"type": "folder",
|
|
||||||
"item": {
|
|
||||||
"path": folder,
|
|
||||||
"count": count[0]["trackcount"],
|
|
||||||
"help_text": "folder",
|
|
||||||
"time": timestamp_to_time_passed(entry.timestamp),
|
|
||||||
},
|
},
|
||||||
}
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
|
|
||||||
if entry.type == "playlist":
|
|
||||||
is_custom = entry.type_src in [i["name"] for i in custom_playlists]
|
|
||||||
# is_recently_added = entry.type_src == "recentlyadded"
|
|
||||||
|
|
||||||
if is_custom:
|
|
||||||
playlist, _ = next(
|
|
||||||
i["handler"]()
|
|
||||||
for i in custom_playlists
|
|
||||||
if i["name"] == entry.type_src
|
|
||||||
)
|
)
|
||||||
playlist.images = [i["image"] for i in playlist.images]
|
album["help_text"] = "album"
|
||||||
|
album["time"] = timestamp_to_time_passed(entry.timestamp)
|
||||||
playlist = serialize_playlist(
|
|
||||||
playlist, to_remove={"settings", "duration"}
|
|
||||||
)
|
|
||||||
|
|
||||||
playlist["help_text"] = "playlist"
|
|
||||||
playlist["time"] = timestamp_to_time_passed(entry.timestamp)
|
|
||||||
|
|
||||||
items.append(
|
items.append(
|
||||||
{
|
{
|
||||||
"type": "playlist",
|
"type": "album",
|
||||||
"item": playlist,
|
"item": album,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
playlist = PlaylistTable.get_by_id(entry.type_src)
|
if entry.type == "artist":
|
||||||
if playlist is None:
|
artist = ArtistStore.get_artist_by_hash(entry.type_src)
|
||||||
|
|
||||||
|
if artist is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
artist = serialize_for_card(artist)
|
||||||
|
artist["help_text"] = "artist"
|
||||||
|
artist["time"] = timestamp_to_time_passed(entry.timestamp)
|
||||||
|
|
||||||
|
items.append(
|
||||||
|
{
|
||||||
|
"type": "artist",
|
||||||
|
"item": artist,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
tracks = TrackStore.get_tracks_by_trackhashes(playlist.trackhashes)
|
if entry.type == "folder":
|
||||||
playlist.clear_lists()
|
folder = entry.type_src
|
||||||
|
|
||||||
if not playlist.has_image:
|
if not folder:
|
||||||
images = get_first_4_images(tracks)
|
continue
|
||||||
images = [i["image"] for i in images]
|
|
||||||
playlist.images = images
|
if not folder.endswith("/"):
|
||||||
|
folder += "/"
|
||||||
|
|
||||||
|
is_home_dir = entry.type_src == "$home"
|
||||||
|
|
||||||
|
if is_home_dir:
|
||||||
|
folder = os.path.expanduser("~")
|
||||||
|
|
||||||
|
# print(folder)
|
||||||
|
# folder = os.path.join("/", folder, "")
|
||||||
|
# print(folder)
|
||||||
|
# count = len([t for t in TrackStore.tracks if t.folder == folder])
|
||||||
|
count = FolderStore.count_tracks_containing_paths([folder])
|
||||||
|
items.append(
|
||||||
|
{
|
||||||
|
"type": "folder",
|
||||||
|
"item": {
|
||||||
|
"path": folder,
|
||||||
|
"count": count[0]["trackcount"],
|
||||||
|
"help_text": "folder",
|
||||||
|
"time": timestamp_to_time_passed(entry.timestamp),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if entry.type == "playlist":
|
||||||
|
is_custom = entry.type_src in [i["name"] for i in custom_playlists]
|
||||||
|
# is_recently_added = entry.type_src == "recentlyadded"
|
||||||
|
|
||||||
|
if is_custom:
|
||||||
|
playlist, _ = next(
|
||||||
|
i["handler"]()
|
||||||
|
for i in custom_playlists
|
||||||
|
if i["name"] == entry.type_src
|
||||||
|
)
|
||||||
|
playlist.images = [i["image"] for i in playlist.images]
|
||||||
|
|
||||||
|
playlist = serialize_playlist(
|
||||||
|
playlist, to_remove={"settings", "duration"}
|
||||||
|
)
|
||||||
|
|
||||||
|
playlist["help_text"] = "playlist"
|
||||||
|
playlist["time"] = timestamp_to_time_passed(entry.timestamp)
|
||||||
|
|
||||||
|
items.append(
|
||||||
|
{
|
||||||
|
"type": "playlist",
|
||||||
|
"item": playlist,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
|
playlist = PlaylistTable.get_by_id(entry.type_src)
|
||||||
|
if playlist is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
tracks = TrackStore.get_tracks_by_trackhashes(playlist.trackhashes)
|
||||||
|
playlist.clear_lists()
|
||||||
|
|
||||||
|
if not playlist.has_image:
|
||||||
|
images = get_first_4_images(tracks)
|
||||||
|
images = [i["image"] for i in images]
|
||||||
|
playlist.images = images
|
||||||
|
|
||||||
|
items.append(
|
||||||
|
{
|
||||||
|
"type": "playlist",
|
||||||
|
"item": {
|
||||||
|
"help_text": "playlist",
|
||||||
|
"time": timestamp_to_time_passed(entry.timestamp),
|
||||||
|
**serialize_playlist(playlist),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if entry.type == "favorite":
|
||||||
|
items.append(
|
||||||
|
{
|
||||||
|
"type": "favorite_tracks",
|
||||||
|
"item": {
|
||||||
|
"help_text": "playlist",
|
||||||
|
"count": FavoritesTable.count(),
|
||||||
|
"time": timestamp_to_time_passed(entry.timestamp),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
|
t = TrackStore.trackhashmap.get(entry.trackhash)
|
||||||
|
|
||||||
|
if t is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
track = serialize_track(t.get_best())
|
||||||
|
track["help_text"] = "track"
|
||||||
|
track["time"] = timestamp_to_time_passed(entry.timestamp)
|
||||||
|
|
||||||
items.append(
|
items.append(
|
||||||
{
|
{
|
||||||
"type": "playlist",
|
"type": "track",
|
||||||
"item": {
|
"item": track,
|
||||||
"help_text": "playlist",
|
|
||||||
"time": timestamp_to_time_passed(entry.timestamp),
|
|
||||||
**serialize_playlist(playlist),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
if entry.type == "favorite":
|
BATCH_SIZE = 200
|
||||||
items.append(
|
current_index = 0
|
||||||
{
|
entries = ScrobbleTable.get_all(0, BATCH_SIZE)
|
||||||
"type": "favorite_tracks",
|
max_iterations = 20 # Safeguard against unexpected infinite loops
|
||||||
"item": {
|
iterations = 0
|
||||||
"help_text": "playlist",
|
|
||||||
"count": FavoritesTable.count(),
|
|
||||||
"time": timestamp_to_time_passed(entry.timestamp),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
|
|
||||||
t = TrackStore.trackhashmap.get(entry.trackhash)
|
while len(items) < limit and iterations < max_iterations:
|
||||||
|
create_items(entries[current_index : current_index + BATCH_SIZE])
|
||||||
|
current_index += BATCH_SIZE
|
||||||
|
|
||||||
if t is None:
|
if len(items) < limit:
|
||||||
continue
|
entries = ScrobbleTable.get_all(current_index, BATCH_SIZE)
|
||||||
|
if not entries:
|
||||||
|
break
|
||||||
|
|
||||||
track = serialize_track(t.get_best())
|
iterations += 1
|
||||||
track["help_text"] = "track"
|
|
||||||
track["time"] = timestamp_to_time_passed(entry.timestamp)
|
|
||||||
|
|
||||||
items.append(
|
if iterations == max_iterations:
|
||||||
{
|
print(
|
||||||
"type": "track",
|
f"Warning: Reached maximum iterations ({max_iterations}) while fetching recently played items"
|
||||||
"item": track,
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return items
|
return items
|
||||||
|
|||||||
Reference in New Issue
Block a user