mirror of
https://github.com/C9Glax/tranga.git
synced 2025-04-12 19:36:08 +02:00
44 lines
1.6 KiB
C#
44 lines
1.6 KiB
C#
using System.Net;
|
|
using log4net;
|
|
|
|
namespace API.MangaDownloadClients;
|
|
|
|
internal abstract class DownloadClient
|
|
{
|
|
private readonly Dictionary<RequestType, DateTime> _lastExecutedRateLimit;
|
|
protected ILog Log { get; init; }
|
|
|
|
protected DownloadClient()
|
|
{
|
|
this.Log = LogManager.GetLogger(GetType());
|
|
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);
|
|
_lastExecutedRateLimit.TryAdd(requestType, DateTime.UtcNow.Subtract(timeBetweenRequests));
|
|
|
|
TimeSpan rateLimitTimeout = timeBetweenRequests.Subtract(DateTime.UtcNow.Subtract(_lastExecutedRateLimit[requestType]));
|
|
|
|
if (rateLimitTimeout > TimeSpan.Zero)
|
|
{
|
|
Thread.Sleep(rateLimitTimeout);
|
|
}
|
|
|
|
RequestResult result = MakeRequestInternal(url, referrer, clickButton);
|
|
_lastExecutedRateLimit[requestType] = DateTime.UtcNow;
|
|
return result;
|
|
}
|
|
|
|
internal abstract RequestResult MakeRequestInternal(string url, string? referrer = null, string? clickButton = null);
|
|
} |