2024-12-14 21:53:29 +01:00
|
|
|
|
using System.Net;
|
|
|
|
|
using API.Schema;
|
|
|
|
|
|
|
|
|
|
namespace API.MangaDownloadClients;
|
|
|
|
|
|
|
|
|
|
internal abstract class DownloadClient
|
|
|
|
|
{
|
|
|
|
|
private readonly Dictionary<RequestType, DateTime> _lastExecutedRateLimit;
|
|
|
|
|
|
|
|
|
|
protected DownloadClient()
|
|
|
|
|
{
|
|
|
|
|
this._lastExecutedRateLimit = new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RequestResult MakeRequest(string url, RequestType requestType, string? referrer = null, string? clickButton = null)
|
|
|
|
|
{
|
|
|
|
|
if (!TrangaSettings.requestLimits.ContainsKey(requestType))
|
|
|
|
|
{
|
|
|
|
|
return new RequestResult(HttpStatusCode.NotAcceptable, null, Stream.Null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int rateLimit = TrangaSettings.userAgent == TrangaSettings.DefaultUserAgent
|
|
|
|
|
? TrangaSettings.DefaultRequestLimits[requestType]
|
|
|
|
|
: TrangaSettings.requestLimits[requestType];
|
|
|
|
|
|
|
|
|
|
TimeSpan timeBetweenRequests = TimeSpan.FromMinutes(1).Divide(rateLimit);
|
2025-02-01 21:58:50 +01:00
|
|
|
|
_lastExecutedRateLimit.TryAdd(requestType, DateTime.UtcNow.Subtract(timeBetweenRequests));
|
2024-12-14 21:53:29 +01:00
|
|
|
|
|
2025-02-01 21:58:50 +01:00
|
|
|
|
TimeSpan rateLimitTimeout = timeBetweenRequests.Subtract(DateTime.UtcNow.Subtract(_lastExecutedRateLimit[requestType]));
|
2024-12-14 21:53:29 +01:00
|
|
|
|
|
|
|
|
|
if (rateLimitTimeout > TimeSpan.Zero)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(rateLimitTimeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RequestResult result = MakeRequestInternal(url, referrer, clickButton);
|
2025-02-01 21:58:50 +01:00
|
|
|
|
_lastExecutedRateLimit[requestType] = DateTime.UtcNow;
|
2024-12-14 21:53:29 +01:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal abstract RequestResult MakeRequestInternal(string url, string? referrer = null, string? clickButton = null);
|
|
|
|
|
}
|