oh how i hate time

This commit is contained in:
2026-02-02 13:31:10 -05:00
parent ff48891a5a
commit 97975f1e08
2 changed files with 40 additions and 65 deletions

View File

@@ -3011,7 +3011,7 @@ public class JellyfinController : ControllerBase
/// </summary> /// </summary>
[HttpGet("spotify/sync", Order = 1)] [HttpGet("spotify/sync", Order = 1)]
[ServiceFilter(typeof(ApiKeyAuthFilter))] [ServiceFilter(typeof(ApiKeyAuthFilter))]
public async Task<IActionResult> TriggerSpotifySync() public async Task<IActionResult> TriggerSpotifySync([FromServices] IEnumerable<IHostedService> hostedServices)
{ {
if (!_spotifySettings.Enabled) if (!_spotifySettings.Enabled)
{ {
@@ -3020,77 +3020,43 @@ public class JellyfinController : ControllerBase
_logger.LogInformation("Manual Spotify sync triggered"); _logger.LogInformation("Manual Spotify sync triggered");
var results = new Dictionary<string, object>(); // Find the SpotifyMissingTracksFetcher service
var fetcherService = hostedServices
.OfType<allstarr.Services.Spotify.SpotifyMissingTracksFetcher>()
.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<string, object>();
for (int i = 0; i < _spotifySettings.PlaylistIds.Count; i++) for (int i = 0; i < _spotifySettings.PlaylistIds.Count; i++)
{ {
var playlistId = _spotifySettings.PlaylistIds[i];
try
{
// Use configured name if available, otherwise use ID
var playlistName = i < _spotifySettings.PlaylistNames.Count var playlistName = i < _spotifySettings.PlaylistNames.Count
? _spotifySettings.PlaylistNames[i] ? _spotifySettings.PlaylistNames[i]
: playlistId; : _spotifySettings.PlaylistIds[i];
_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}"; var cacheKey = $"spotify:missing:{playlistName}";
await _cache.SetAsync(cacheKey, tracks, TimeSpan.FromHours(24)); var tracks = await _cache.GetAsync<List<allstarr.Models.Spotify.MissingTrack>>(cacheKey);
if (tracks != null && tracks.Count > 0)
{
results[playlistName] = new { results[playlistName] = new {
status = "success", status = "success",
tracks = tracks.Count, tracks = tracks.Count
filename = filename
}; };
_logger.LogInformation("✓ Cached {Count} missing tracks for {Playlist} from {Filename}",
tracks.Count, playlistName, filename);
found = true;
break;
} }
} else
}
catch (Exception ex)
{ {
_logger.LogDebug(ex, "Failed to fetch {Filename}", filename); results[playlistName] = new {
} status = "not_found",
} message = "No missing tracks found"
};
if (!found)
{
results[playlistName] = new { status = "not_found", message = "No missing tracks file found" };
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error syncing playlist {PlaylistId}", playlistId);
results[playlistId] = new { status = "error", message = ex.Message };
} }
} }

View File

@@ -35,6 +35,15 @@ public class SpotifyMissingTracksFetcher : BackgroundService
_logger = logger; _logger = logger;
} }
/// <summary>
/// Public method to trigger fetching manually (called from controller).
/// </summary>
public async Task TriggerFetchAsync()
{
_logger.LogInformation("Manual fetch triggered");
await FetchMissingTracksAsync(CancellationToken.None, bypassSyncWindowCheck: true);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
_logger.LogInformation("========================================"); _logger.LogInformation("========================================");