mirror of
https://github.com/C9Glax/tranga.git
synced 2025-06-19 09:37:53 +02:00
Add API
This commit is contained in:
42
API/MangaDownloadClients/DownloadClient.cs
Normal file
42
API/MangaDownloadClients/DownloadClient.cs
Normal file
@ -0,0 +1,42 @@
|
||||
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);
|
||||
_lastExecutedRateLimit.TryAdd(requestType, DateTime.Now.Subtract(timeBetweenRequests));
|
||||
|
||||
TimeSpan rateLimitTimeout = timeBetweenRequests.Subtract(DateTime.Now.Subtract(_lastExecutedRateLimit[requestType]));
|
||||
|
||||
if (rateLimitTimeout > TimeSpan.Zero)
|
||||
{
|
||||
Thread.Sleep(rateLimitTimeout);
|
||||
}
|
||||
|
||||
RequestResult result = MakeRequestInternal(url, referrer, clickButton);
|
||||
_lastExecutedRateLimit[requestType] = DateTime.Now;
|
||||
return result;
|
||||
}
|
||||
|
||||
internal abstract RequestResult MakeRequestInternal(string url, string? referrer = null, string? clickButton = null);
|
||||
}
|
Reference in New Issue
Block a user