fix spotify search: start 24h ahead, search backwards for 72h

This commit is contained in:
2026-02-02 23:49:39 -05:00
parent d9375405a5
commit 51702a544b

View File

@@ -462,11 +462,15 @@ public class SpotifyMissingTracksFetcher : BackgroundService
var httpClient = _httpClientFactory.CreateClient(); var httpClient = _httpClientFactory.CreateClient();
// Search forward first (newest files), then backwards to handle timezone differences // Search starting from 24 hours ahead, going backwards for 72 hours
// We want the file with the furthest forward timestamp (most recent) // This handles timezone differences where the plugin may have run "in the future" from our perspective
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
var searchStart = now.AddHours(24); // Start 24 hours from now
var totalMinutesToSearch = 72 * 60; // 72 hours = 4320 minutes
_logger.LogInformation(" Current UTC time: {Now:yyyy-MM-dd HH:mm}", now); _logger.LogInformation(" Current UTC time: {Now:yyyy-MM-dd HH:mm}", now);
_logger.LogInformation(" Searching +24h forward, then -48h backward"); _logger.LogInformation(" Search start: {Start:yyyy-MM-dd HH:mm} (24h ahead)", searchStart);
_logger.LogInformation(" Searching backwards for 72 hours ({Minutes} minutes)", totalMinutesToSearch);
var found = false; var found = false;
DateTime? foundFileTime = null; DateTime? foundFileTime = null;
@@ -511,40 +515,15 @@ public class SpotifyMissingTracksFetcher : BackgroundService
_logger.LogInformation(" Not found within ±1h of hint, doing full search..."); _logger.LogInformation(" Not found within ±1h of hint, doing full search...");
} }
// First search forward 24 hours (most likely to find newest files with timezone ahead) // Search from 24h ahead, going backwards minute by minute for 72 hours
_logger.LogInformation(" Phase 1: Searching forward 24 hours..."); _logger.LogInformation(" Searching from {Start:yyyy-MM-dd HH:mm} backwards to {End:yyyy-MM-dd HH:mm}...",
searchStart, searchStart.AddMinutes(-totalMinutesToSearch));
for (var minutesAhead = 1; minutesAhead <= 1440; minutesAhead++) for (var minutesBehind = 0; minutesBehind <= totalMinutesToSearch; minutesBehind++)
{ {
if (cancellationToken.IsCancellationRequested) break; if (cancellationToken.IsCancellationRequested) break;
var time = now.AddMinutes(minutesAhead); var time = searchStart.AddMinutes(-minutesBehind);
var result = await TryFetchMissingTracksFile(playlistName, time, jellyfinUrl, apiKey, httpClient, cancellationToken);
if (result.found)
{
found = true;
foundFileTime = result.fileTime;
return foundFileTime;
}
// Small delay every 60 requests to avoid rate limiting
if (minutesAhead % 60 == 0)
{
await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken);
}
}
// If not found forward, search backwards 48 hours
if (!found)
{
_logger.LogInformation(" Phase 2: Searching backward 48 hours...");
for (var minutesBehind = 0; minutesBehind <= 2880; minutesBehind++)
{
if (cancellationToken.IsCancellationRequested) break;
var time = now.AddMinutes(-minutesBehind);
var result = await TryFetchMissingTracksFile(playlistName, time, jellyfinUrl, apiKey, httpClient, cancellationToken); var result = await TryFetchMissingTracksFile(playlistName, time, jellyfinUrl, apiKey, httpClient, cancellationToken);
if (result.found) if (result.found)
@@ -560,7 +539,6 @@ public class SpotifyMissingTracksFetcher : BackgroundService
await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken); await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken);
} }
} }
}
if (!found) if (!found)
{ {