From 51702a544b8da0ba982f72792f5115bc45e89377 Mon Sep 17 00:00:00 2001 From: Josh Patra Date: Mon, 2 Feb 2026 23:49:39 -0500 Subject: [PATCH] fix spotify search: start 24h ahead, search backwards for 72h --- .../Spotify/SpotifyMissingTracksFetcher.cs | 48 +++++-------------- 1 file changed, 13 insertions(+), 35 deletions(-) diff --git a/allstarr/Services/Spotify/SpotifyMissingTracksFetcher.cs b/allstarr/Services/Spotify/SpotifyMissingTracksFetcher.cs index 41fff44..abb023a 100644 --- a/allstarr/Services/Spotify/SpotifyMissingTracksFetcher.cs +++ b/allstarr/Services/Spotify/SpotifyMissingTracksFetcher.cs @@ -462,11 +462,15 @@ public class SpotifyMissingTracksFetcher : BackgroundService var httpClient = _httpClientFactory.CreateClient(); - // Search forward first (newest files), then backwards to handle timezone differences - // We want the file with the furthest forward timestamp (most recent) + // Search starting from 24 hours ahead, going backwards for 72 hours + // This handles timezone differences where the plugin may have run "in the future" from our perspective 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(" 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; DateTime? foundFileTime = null; @@ -511,14 +515,15 @@ public class SpotifyMissingTracksFetcher : BackgroundService _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) - _logger.LogInformation(" Phase 1: Searching forward 24 hours..."); + // Search from 24h ahead, going backwards minute by minute for 72 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; - var time = now.AddMinutes(minutesAhead); + var time = searchStart.AddMinutes(-minutesBehind); var result = await TryFetchMissingTracksFile(playlistName, time, jellyfinUrl, apiKey, httpClient, cancellationToken); if (result.found) @@ -529,39 +534,12 @@ public class SpotifyMissingTracksFetcher : BackgroundService } // Small delay every 60 requests to avoid rate limiting - if (minutesAhead % 60 == 0) + if (minutesBehind > 0 && minutesBehind % 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); - if (result.found) - { - found = true; - foundFileTime = result.fileTime; - return foundFileTime; - } - - // Small delay every 60 requests to avoid rate limiting - if (minutesBehind > 0 && minutesBehind % 60 == 0) - { - await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken); - } - } - } - if (!found) { _logger.LogWarning(" ✗ Could not find new missing tracks file (searched +24h forward, -48h backward)");