2020-05-16 15:08:40 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Wabbajack.BuildServer;
|
2020-08-12 04:25:12 +00:00
|
|
|
|
using Wabbajack.Common;
|
2020-05-16 15:08:40 +00:00
|
|
|
|
using Wabbajack.Lib.NexusApi;
|
|
|
|
|
using Wabbajack.Server.DataLayer;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Server.Services
|
|
|
|
|
{
|
|
|
|
|
public class NexusKeyMaintainance : AbstractService<NexusKeyMaintainance, int>
|
|
|
|
|
{
|
|
|
|
|
private SqlService _sql;
|
2020-08-12 04:25:12 +00:00
|
|
|
|
private string _selfKey;
|
2020-05-16 15:08:40 +00:00
|
|
|
|
|
2020-07-09 04:14:35 +00:00
|
|
|
|
public NexusKeyMaintainance(ILogger<NexusKeyMaintainance> logger, AppSettings settings, SqlService sql, QuickSync quickSync) : base(logger, settings, quickSync, TimeSpan.FromHours(4))
|
2020-05-16 15:08:40 +00:00
|
|
|
|
{
|
|
|
|
|
_sql = sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<NexusApiClient> GetClient()
|
|
|
|
|
{
|
|
|
|
|
var keys = await _sql.GetNexusApiKeysWithCounts(1500);
|
2020-08-12 04:25:12 +00:00
|
|
|
|
foreach (var key in keys.Where(k => k.Key != _selfKey))
|
2020-05-16 15:08:40 +00:00
|
|
|
|
{
|
2021-01-09 21:46:46 +00:00
|
|
|
|
try
|
2020-11-11 13:25:57 +00:00
|
|
|
|
{
|
2021-01-09 21:46:46 +00:00
|
|
|
|
var client = new TrackingClient(_sql, key);
|
|
|
|
|
if (!await client.IsPremium())
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning($"Purging non premium key");
|
|
|
|
|
await _sql.DeleteNexusAPIKey(key.Key);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Utils.Log($"Error getting tracking client: {ex}");
|
2020-11-11 13:25:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-16 15:08:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-09 21:46:46 +00:00
|
|
|
|
var bclient = await NexusApiClient.Get();
|
|
|
|
|
await bclient.GetUserStatus();
|
|
|
|
|
return bclient;
|
2020-05-16 15:08:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task<int> Execute()
|
|
|
|
|
{
|
2020-08-12 04:25:12 +00:00
|
|
|
|
_selfKey ??= await Utils.FromEncryptedJson<string>("nexusapikey");
|
2020-05-16 15:08:40 +00:00
|
|
|
|
var keys = await _sql.GetNexusApiKeysWithCounts(0);
|
|
|
|
|
_logger.Log(LogLevel.Information, $"Verifying {keys.Count} API Keys");
|
|
|
|
|
foreach (var key in keys)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var client = new TrackingClient(_sql, key);
|
|
|
|
|
|
|
|
|
|
var status = await client.GetUserStatus();
|
|
|
|
|
if (!status.is_premium)
|
|
|
|
|
{
|
|
|
|
|
await _sql.DeleteNexusAPIKey(key.Key);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (daily, hourly) = await client.GetRemainingApiCalls();
|
|
|
|
|
await _sql.SetNexusAPIKey(key.Key, daily, hourly);
|
|
|
|
|
}
|
2020-10-01 03:50:09 +00:00
|
|
|
|
catch (Exception)
|
2020-05-16 15:08:40 +00:00
|
|
|
|
{
|
|
|
|
|
_logger.Log(LogLevel.Warning, "Update error, purging API key");
|
|
|
|
|
await _sql.DeleteNexusAPIKey(key.Key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return keys.Count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class TrackingClient : NexusApiClient
|
|
|
|
|
{
|
|
|
|
|
private SqlService _sql;
|
|
|
|
|
public TrackingClient(SqlService sql, (string Key, int Daily, int Hourly) key) : base(key.Key)
|
|
|
|
|
{
|
|
|
|
|
_sql = sql;
|
|
|
|
|
DailyRemaining = key.Daily;
|
|
|
|
|
HourlyRemaining = key.Hourly;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 04:25:12 +00:00
|
|
|
|
protected override async Task UpdateRemaining(HttpResponseMessage response)
|
2020-05-16 15:08:40 +00:00
|
|
|
|
{
|
|
|
|
|
await base.UpdateRemaining(response);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var dailyRemaining = int.Parse(response.Headers.GetValues("x-rl-daily-remaining").First());
|
|
|
|
|
var hourlyRemaining = int.Parse(response.Headers.GetValues("x-rl-hourly-remaining").First());
|
|
|
|
|
await _sql.SetNexusAPIKey(ApiKey, dailyRemaining, hourlyRemaining);
|
|
|
|
|
}
|
2020-10-01 03:50:09 +00:00
|
|
|
|
catch (Exception)
|
2020-05-16 15:08:40 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|