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:
@@ -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,6 +32,7 @@ def get_recently_played(limit=7):
|
|||||||
{"name": "recentlyplayed", "handler": get_recently_played_playlist},
|
{"name": "recentlyplayed", "handler": get_recently_played_playlist},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def create_items(entries: list[TrackLog]):
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
if len(items) >= limit:
|
if len(items) >= limit:
|
||||||
break
|
break
|
||||||
@@ -199,6 +200,28 @@ def get_recently_played(limit=7):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
BATCH_SIZE = 200
|
||||||
|
current_index = 0
|
||||||
|
entries = ScrobbleTable.get_all(0, BATCH_SIZE)
|
||||||
|
max_iterations = 20 # Safeguard against unexpected infinite loops
|
||||||
|
iterations = 0
|
||||||
|
|
||||||
|
while len(items) < limit and iterations < max_iterations:
|
||||||
|
create_items(entries[current_index : current_index + BATCH_SIZE])
|
||||||
|
current_index += BATCH_SIZE
|
||||||
|
|
||||||
|
if len(items) < limit:
|
||||||
|
entries = ScrobbleTable.get_all(current_index, BATCH_SIZE)
|
||||||
|
if not entries:
|
||||||
|
break
|
||||||
|
|
||||||
|
iterations += 1
|
||||||
|
|
||||||
|
if iterations == max_iterations:
|
||||||
|
print(
|
||||||
|
f"Warning: Reached maximum iterations ({max_iterations}) while fetching recently played items"
|
||||||
|
)
|
||||||
|
|
||||||
return items
|
return items
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user