From 1212d35290ec40382a42e3ac2918477d7923734d Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Thu, 14 May 2020 05:53:51 -0600 Subject: [PATCH] Rework the nexus api limit code a bit --- Wabbajack.Lib/NexusApi/NexusApi.cs | 8 ++++++++ Wabbajack.Server/Services/ArchiveDownloader.cs | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Wabbajack.Lib/NexusApi/NexusApi.cs b/Wabbajack.Lib/NexusApi/NexusApi.cs index 10b83edb..3dc808a2 100644 --- a/Wabbajack.Lib/NexusApi/NexusApi.cs +++ b/Wabbajack.Lib/NexusApi/NexusApi.cs @@ -153,6 +153,14 @@ namespace Wabbajack.Lib.NexusApi return await Get(url); } + public async Task<(int, int)> GetRemainingApiCalls() + { + var url = "https://api.nexusmods.com/v1/users/validate.json"; + using var response = await HttpClient.GetAsync(url); + return (int.Parse(response.Headers.GetValues("X-RL-Daily-Remaining").First()), + int.Parse(response.Headers.GetValues("X-RL-Hourly-Remaining").First())); + } + #endregion #region Rate Tracking diff --git a/Wabbajack.Server/Services/ArchiveDownloader.cs b/Wabbajack.Server/Services/ArchiveDownloader.cs index eea0b99d..68069b53 100644 --- a/Wabbajack.Server/Services/ArchiveDownloader.cs +++ b/Wabbajack.Server/Services/ArchiveDownloader.cs @@ -26,12 +26,16 @@ namespace Wabbajack.Server.Services public override async Task Execute() { _nexusClient ??= await NexusApiClient.Get(); - await _nexusClient.GetUserStatus(); int count = 0; while (true) { - bool ignoreNexus = _nexusClient.HourlyRemaining < 25; + var (daily, hourly) = await _nexusClient.GetRemainingApiCalls(); + bool ignoreNexus = hourly < 25; + if (ignoreNexus) + _logger.LogWarning($"Ignoring Nexus Downloads due to low hourly api limit (Daily: {_nexusClient.DailyRemaining}, Hourly:{_nexusClient.HourlyRemaining})"); + else + _logger.LogInformation($"Looking for any download (Daily: {_nexusClient.DailyRemaining}, Hourly:{_nexusClient.HourlyRemaining})"); var nextDownload = await _sql.GetNextPendingDownload(ignoreNexus);