mirror of
https://github.com/SoPat712/allstarr.git
synced 2026-02-09 23:55:10 -05:00
Fix Spotify missing tracks search order: forward 12h then backward 12h
This commit is contained in:
@@ -146,12 +146,13 @@ public class SpotifyMissingTracksFetcher : BackgroundService
|
||||
.AddMinutes(settings.SyncStartMinute);
|
||||
var syncEnd = syncStart.AddHours(settings.SyncWindowHours);
|
||||
|
||||
if (now < syncStart || now > syncEnd)
|
||||
// Only run after the sync window has passed
|
||||
if (now < syncEnd)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Within sync window, fetching missing tracks...");
|
||||
_logger.LogInformation("Sync window passed, searching last 24 hours for missing tracks...");
|
||||
|
||||
foreach (var kvp in _playlistIdToName)
|
||||
{
|
||||
@@ -175,18 +176,76 @@ public class SpotifyMissingTracksFetcher : BackgroundService
|
||||
var jellyfinUrl = _jellyfinSettings.Value.Url;
|
||||
var apiKey = _jellyfinSettings.Value.ApiKey;
|
||||
var httpClient = _httpClientFactory.CreateClient();
|
||||
var today = DateTime.UtcNow.Date;
|
||||
var syncStart = today
|
||||
|
||||
// Start from the configured sync time (most likely time)
|
||||
var now = DateTime.UtcNow;
|
||||
var todaySync = now.Date
|
||||
.AddHours(settings.SyncStartHour)
|
||||
.AddMinutes(settings.SyncStartMinute);
|
||||
var syncEnd = syncStart.AddHours(settings.SyncWindowHours);
|
||||
|
||||
_logger.LogInformation("Searching for missing tracks file for {Playlist}", playlistName);
|
||||
// If we haven't reached today's sync time yet, start from yesterday's sync time
|
||||
var syncTime = now >= todaySync ? todaySync : todaySync.AddDays(-1);
|
||||
|
||||
for (var time = syncStart; time <= syncEnd; time = time.AddMinutes(5))
|
||||
_logger.LogInformation("Searching ±12 hours around {SyncTime} for {Playlist}",
|
||||
syncTime, playlistName);
|
||||
|
||||
var found = false;
|
||||
|
||||
// Search forward 12 hours from sync time
|
||||
for (var minutesAhead = 0; minutesAhead <= 720; minutesAhead++) // 720 minutes = 12 hours
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested) break;
|
||||
|
||||
var time = syncTime.AddMinutes(minutesAhead);
|
||||
if (await TryFetchMissingTracksFile(playlistName, time, jellyfinUrl, apiKey, httpClient, cancellationToken))
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// Small delay every 60 requests
|
||||
if (minutesAhead > 0 && minutesAhead % 60 == 0)
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
// Then search backwards 12 hours from sync time
|
||||
if (!found)
|
||||
{
|
||||
for (var minutesBehind = 1; minutesBehind <= 720; minutesBehind++)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested) break;
|
||||
|
||||
var time = syncTime.AddMinutes(-minutesBehind);
|
||||
if (await TryFetchMissingTracksFile(playlistName, time, jellyfinUrl, apiKey, httpClient, cancellationToken))
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// Small delay every 60 requests
|
||||
if (minutesBehind % 60 == 0)
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
_logger.LogWarning("Could not find missing tracks file for {Playlist} in ±12 hour window", playlistName);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<bool> TryFetchMissingTracksFile(
|
||||
string playlistName,
|
||||
DateTime time,
|
||||
string jellyfinUrl,
|
||||
string apiKey,
|
||||
HttpClient httpClient,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var filename = $"{playlistName}_missing_{time:yyyy-MM-dd_HH-mm}.json";
|
||||
var url = $"{jellyfinUrl}/Viperinius.Plugin.SpotifyImport/MissingTracksFile" +
|
||||
$"?name={Uri.EscapeDataString(filename)}&api_key={apiKey}";
|
||||
@@ -202,11 +261,12 @@ public class SpotifyMissingTracksFetcher : BackgroundService
|
||||
|
||||
if (tracks.Count > 0)
|
||||
{
|
||||
var cacheKey = $"spotify:missing:{playlistName}";
|
||||
await _cache.SetAsync(cacheKey, tracks, TimeSpan.FromHours(24));
|
||||
_logger.LogInformation(
|
||||
"✓ Cached {Count} missing tracks for {Playlist} from {Filename}",
|
||||
tracks.Count, playlistName, filename);
|
||||
break;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -214,7 +274,8 @@ public class SpotifyMissingTracksFetcher : BackgroundService
|
||||
{
|
||||
_logger.LogDebug(ex, "Failed to fetch {Filename}", filename);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private List<MissingTrack> ParseMissingTracks(string json)
|
||||
|
||||
Reference in New Issue
Block a user