mirror of
https://github.com/SoPat712/allstarr.git
synced 2026-02-09 23:55:10 -05:00
oh how i hate time
This commit is contained in:
@@ -3011,7 +3011,7 @@ public class JellyfinController : ControllerBase
|
||||
/// </summary>
|
||||
[HttpGet("spotify/sync", Order = 1)]
|
||||
[ServiceFilter(typeof(ApiKeyAuthFilter))]
|
||||
public async Task<IActionResult> TriggerSpotifySync()
|
||||
public async Task<IActionResult> TriggerSpotifySync([FromServices] IEnumerable<IHostedService> hostedServices)
|
||||
{
|
||||
if (!_spotifySettings.Enabled)
|
||||
{
|
||||
@@ -3020,77 +3020,43 @@ public class JellyfinController : ControllerBase
|
||||
|
||||
_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++)
|
||||
{
|
||||
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<List<allstarr.Models.Spotify.MissingTrack>>(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"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,15 @@ public class SpotifyMissingTracksFetcher : BackgroundService
|
||||
_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)
|
||||
{
|
||||
_logger.LogInformation("========================================");
|
||||
|
||||
Reference in New Issue
Block a user