mirror of
https://github.com/C9Glax/tranga.git
synced 2025-07-06 10:54:17 +02:00
DownloadClient is now abstract for HttpDownloadClient and ChromiumDownloadClient The chromium client will exit the headless browser (on clean exit of the program). The field "name" of MangaConnector is no longer abstract, instead set through constructor.
66 lines
2.4 KiB
C#
66 lines
2.4 KiB
C#
using System.Net;
|
|
using HtmlAgilityPack;
|
|
|
|
namespace Tranga.MangaConnectors;
|
|
|
|
internal abstract class DownloadClient : GlobalBase
|
|
{
|
|
private readonly Dictionary<byte, DateTime> _lastExecutedRateLimit;
|
|
private readonly Dictionary<byte, TimeSpan> _rateLimit;
|
|
|
|
protected DownloadClient(GlobalBase clone, Dictionary<byte, int> rateLimitRequestsPerMinute) : base(clone)
|
|
{
|
|
this._lastExecutedRateLimit = new();
|
|
_rateLimit = new();
|
|
foreach (KeyValuePair<byte, int> limit in rateLimitRequestsPerMinute)
|
|
_rateLimit.Add(limit.Key, TimeSpan.FromMinutes(1).Divide(limit.Value));
|
|
}
|
|
|
|
public RequestResult MakeRequest(string url, byte requestType, string? referrer = null)
|
|
{
|
|
if (_rateLimit.TryGetValue(requestType, out TimeSpan value))
|
|
_lastExecutedRateLimit.TryAdd(requestType, DateTime.Now.Subtract(value));
|
|
else
|
|
{
|
|
Log("RequestType not configured for rate-limit.");
|
|
return new RequestResult(HttpStatusCode.NotAcceptable, null, Stream.Null);
|
|
}
|
|
|
|
TimeSpan rateLimitTimeout = _rateLimit[requestType]
|
|
.Subtract(DateTime.Now.Subtract(_lastExecutedRateLimit[requestType]));
|
|
|
|
if (rateLimitTimeout > TimeSpan.Zero)
|
|
Thread.Sleep(rateLimitTimeout);
|
|
|
|
RequestResult result = MakeRequestInternal(url, referrer);
|
|
_lastExecutedRateLimit[requestType] = DateTime.Now;
|
|
return result;
|
|
}
|
|
|
|
protected abstract RequestResult MakeRequestInternal(string url, string? referrer = null);
|
|
public abstract void Close();
|
|
|
|
public struct RequestResult
|
|
{
|
|
public HttpStatusCode statusCode { get; }
|
|
public Stream result { get; }
|
|
public bool hasBeenRedirected { get; }
|
|
public string? redirectedToUrl { get; }
|
|
public HtmlDocument? htmlDocument { get; }
|
|
|
|
public RequestResult(HttpStatusCode statusCode, HtmlDocument? htmlDocument, Stream result)
|
|
{
|
|
this.statusCode = statusCode;
|
|
this.htmlDocument = htmlDocument;
|
|
this.result = result;
|
|
}
|
|
|
|
public RequestResult(HttpStatusCode statusCode, HtmlDocument? htmlDocument, Stream result, bool hasBeenRedirected, string redirectedTo)
|
|
: this(statusCode, htmlDocument, result)
|
|
{
|
|
this.hasBeenRedirected = hasBeenRedirected;
|
|
redirectedToUrl = redirectedTo;
|
|
}
|
|
}
|
|
|
|
} |