From 97975f1e088d4ee459ce56853605423058a4e546 Mon Sep 17 00:00:00 2001 From: Josh Patra Date: Mon, 2 Feb 2026 13:31:10 -0500 Subject: [PATCH] oh how i hate time --- allstarr/Controllers/JellyfinController.cs | 96 ++++++------------- .../Spotify/SpotifyMissingTracksFetcher.cs | 9 ++ 2 files changed, 40 insertions(+), 65 deletions(-) diff --git a/allstarr/Controllers/JellyfinController.cs b/allstarr/Controllers/JellyfinController.cs index c6cf237..6fa5f47 100644 --- a/allstarr/Controllers/JellyfinController.cs +++ b/allstarr/Controllers/JellyfinController.cs @@ -3011,7 +3011,7 @@ public class JellyfinController : ControllerBase /// [HttpGet("spotify/sync", Order = 1)] [ServiceFilter(typeof(ApiKeyAuthFilter))] - public async Task TriggerSpotifySync() + public async Task TriggerSpotifySync([FromServices] IEnumerable hostedServices) { if (!_spotifySettings.Enabled) { @@ -3020,77 +3020,43 @@ public class JellyfinController : ControllerBase _logger.LogInformation("Manual Spotify sync triggered"); - var results = new Dictionary(); + // Find the SpotifyMissingTracksFetcher service + var fetcherService = hostedServices + .OfType() + .FirstOrDefault(); + if (fetcherService == null) + { + return StatusCode(500, new { error = "SpotifyMissingTracksFetcher not found" }); + } + + // Trigger fetch manually + await fetcherService.TriggerFetchAsync(); + + // Check what was cached + var results = new Dictionary(); for (int i = 0; i < _spotifySettings.PlaylistIds.Count; i++) { - var playlistId = _spotifySettings.PlaylistIds[i]; + var playlistName = i < _spotifySettings.PlaylistNames.Count + ? _spotifySettings.PlaylistNames[i] + : _spotifySettings.PlaylistIds[i]; - try + var cacheKey = $"spotify:missing:{playlistName}"; + var tracks = await _cache.GetAsync>(cacheKey); + + if (tracks != null && tracks.Count > 0) { - // Use configured name if available, otherwise use ID - var playlistName = i < _spotifySettings.PlaylistNames.Count - ? _spotifySettings.PlaylistNames[i] - : playlistId; - - _logger.LogInformation("Fetching missing tracks for {Playlist} (ID: {Id})", playlistName, playlistId); - - // Try to fetch the missing tracks file - search last 24 hours - var now = DateTime.UtcNow; - var searchStart = now.AddHours(-24); - - var httpClient = new HttpClient(); - var found = false; - - // Search every minute for the last 24 hours (1440 attempts max) - for (var time = searchStart; time <= now; time = time.AddMinutes(1)) - { - var filename = $"{playlistName}_missing_{time:yyyy-MM-dd_HH-mm}.json"; - var url = $"{_settings.Url}/Viperinius.Plugin.SpotifyImport/MissingTracksFile" + - $"?name={Uri.EscapeDataString(filename)}&api_key={_settings.ApiKey}"; - - try - { - _logger.LogDebug("Trying {Filename}", filename); - var response = await httpClient.GetAsync(url); - if (response.IsSuccessStatusCode) - { - var json = await response.Content.ReadAsStringAsync(); - var tracks = ParseMissingTracksJson(json); - - if (tracks.Count > 0) - { - var cacheKey = $"spotify:missing:{playlistName}"; - await _cache.SetAsync(cacheKey, tracks, TimeSpan.FromHours(24)); - - results[playlistName] = new { - status = "success", - tracks = tracks.Count, - filename = filename - }; - - _logger.LogInformation("✓ Cached {Count} missing tracks for {Playlist} from {Filename}", - tracks.Count, playlistName, filename); - found = true; - break; - } - } - } - catch (Exception ex) - { - _logger.LogDebug(ex, "Failed to fetch {Filename}", filename); - } - } - - if (!found) - { - results[playlistName] = new { status = "not_found", message = "No missing tracks file found" }; - } + results[playlistName] = new { + status = "success", + tracks = tracks.Count + }; } - catch (Exception ex) + else { - _logger.LogError(ex, "Error syncing playlist {PlaylistId}", playlistId); - results[playlistId] = new { status = "error", message = ex.Message }; + results[playlistName] = new { + status = "not_found", + message = "No missing tracks found" + }; } } diff --git a/allstarr/Services/Spotify/SpotifyMissingTracksFetcher.cs b/allstarr/Services/Spotify/SpotifyMissingTracksFetcher.cs index 5570599..6ddc5e5 100644 --- a/allstarr/Services/Spotify/SpotifyMissingTracksFetcher.cs +++ b/allstarr/Services/Spotify/SpotifyMissingTracksFetcher.cs @@ -35,6 +35,15 @@ public class SpotifyMissingTracksFetcher : BackgroundService _logger = logger; } + /// + /// Public method to trigger fetching manually (called from controller). + /// + public async Task TriggerFetchAsync() + { + _logger.LogInformation("Manual fetch triggered"); + await FetchMissingTracksAsync(CancellationToken.None, bypassSyncWindowCheck: true); + } + protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation("========================================");