Batch watchlist status checks to eliminate rate limit errors

pull/253/head
Broque Thomas 2 months ago
parent cdcaa245d1
commit 41e895d254

@ -23635,6 +23635,27 @@ def check_watchlist_status():
print(f"Error checking watchlist status: {e}")
return jsonify({"success": False, "error": str(e)}), 500
@app.route('/api/watchlist/check-batch', methods=['POST'])
def check_watchlist_status_batch():
"""Check watchlist status for multiple artists in one request"""
try:
data = request.get_json()
artist_ids = data.get('artist_ids', [])
if not artist_ids:
return jsonify({"success": False, "error": "Missing artist_ids"}), 400
database = get_database()
pid = get_current_profile_id()
results = {}
for aid in artist_ids:
results[aid] = database.is_artist_in_watchlist(aid, profile_id=pid)
return jsonify({"success": True, "results": results})
except Exception as e:
print(f"Error batch checking watchlist status: {e}")
return jsonify({"success": False, "error": str(e)}), 500
@app.route('/api/watchlist/scan', methods=['POST'])
def start_watchlist_scan():
"""Start a watchlist scan for new releases"""

@ -28047,37 +28047,44 @@ async function updateWatchlistButtonCount() {
*/
async function updateArtistCardWatchlistStatus() {
const artistCards = document.querySelectorAll('.artist-card');
const artistIds = [];
for (const card of artistCards) {
const artistId = card.dataset.artistId;
if (!artistId) continue;
if (artistId) artistIds.push(artistId);
}
if (!artistIds.length) return;
try {
const response = await fetch('/api/watchlist/check', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ artist_id: artistId })
});
try {
const response = await fetch('/api/watchlist/check-batch', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ artist_ids: artistIds })
});
const data = await response.json();
if (data.success && data.results) {
for (const card of artistCards) {
const artistId = card.dataset.artistId;
if (!artistId) continue;
const data = await response.json();
if (data.success) {
const button = card.querySelector('.watchlist-toggle-btn');
if (!button) continue;
const icon = button.querySelector('.watchlist-icon');
const text = button.querySelector('.watchlist-text');
if (data.is_watching) {
icon.textContent = '👁️';
text.textContent = 'Watching...';
if (data.results[artistId]) {
if (icon) icon.textContent = '👁️';
if (text) text.textContent = 'Watching...';
button.classList.add('watching');
} else {
icon.textContent = '👁️';
text.textContent = 'Add to Watchlist';
if (icon) icon.textContent = '👁️';
if (text) text.textContent = 'Add to Watchlist';
button.classList.remove('watching');
}
}
} catch (error) {
console.error(`Error checking watchlist status for artist ${artistId}:`, error);
}
} catch (error) {
console.error('Error batch checking watchlist status:', error);
}
}
@ -36478,24 +36485,29 @@ async function toggleRecommendedWatchlist(btn) {
}
async function checkRecommendedWatchlistStatuses(artists) {
for (const artist of artists) {
try {
const resp = await fetch('/api/watchlist/check', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ artist_id: artist.artist_id })
});
const data = await resp.json();
if (data.success && data.is_watching) {
const btn = document.querySelector(`.recommended-card-watchlist-btn[data-artist-id="${artist.artist_id}"]`);
if (btn) {
btn.classList.add('watching');
btn.textContent = 'Watching';
try {
const artistIds = artists.map(a => a.artist_id).filter(Boolean);
if (!artistIds.length) return;
const resp = await fetch('/api/watchlist/check-batch', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ artist_ids: artistIds })
});
const data = await resp.json();
if (data.success && data.results) {
for (const [aid, isWatching] of Object.entries(data.results)) {
if (isWatching) {
const btn = document.querySelector(`.recommended-card-watchlist-btn[data-artist-id="${aid}"]`);
if (btn) {
btn.classList.add('watching');
btn.textContent = 'Watching';
}
}
}
} catch (e) {
// Non-critical
}
} catch (e) {
// Non-critical
}
}

Loading…
Cancel
Save