From 93bb8ef6ee8a914a4703f9eb0225c2179f4bbac2 Mon Sep 17 00:00:00 2001 From: glax <--local> Date: Thu, 18 May 2023 17:18:41 +0200 Subject: [PATCH] Added delay functionality for rate-limits --- Tranga/Connector.cs | 14 +++++++++++++- Tranga/Connectors/MangaDex.cs | 8 ++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Tranga/Connector.cs b/Tranga/Connector.cs index 93ee2b0..fcb82cb 100644 --- a/Tranga/Connector.cs +++ b/Tranga/Connector.cs @@ -24,9 +24,21 @@ public abstract class Connector internal class DownloadClient { + private TimeSpan requestSpeed; + private DateTime lastRequest; static readonly HttpClient client = new HttpClient(); - public RequestResult GetPage(string url) + + public DownloadClient(uint delay) { + this.requestSpeed = TimeSpan.FromMilliseconds(delay); + this.lastRequest = DateTime.Now.Subtract(requestSpeed); + } + + public RequestResult MakeRequest(string url) + { + while((DateTime.Now - lastRequest) < requestSpeed) + Thread.Sleep(10); + HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, url); HttpResponseMessage response = client.Send(requestMessage); Stream resultString = response.IsSuccessStatusCode ? response.Content.ReadAsStream() : Stream.Null; diff --git a/Tranga/Connectors/MangaDex.cs b/Tranga/Connectors/MangaDex.cs index 6fadb71..1dd6a7b 100644 --- a/Tranga/Connectors/MangaDex.cs +++ b/Tranga/Connectors/MangaDex.cs @@ -8,7 +8,7 @@ public class MangaDex : Connector { internal override string downloadLocation { get; } public override string name { get; } - private DownloadClient _downloadClient = new (); + private DownloadClient _downloadClient = new (1500); public MangaDex(string downloadLocation) { @@ -26,7 +26,7 @@ public class MangaDex : Connector while (offset < total) { offset += limit; - DownloadClient.RequestResult requestResult = _downloadClient.GetPage(string.Concat(publicationsUrl, "0")); + DownloadClient.RequestResult requestResult = _downloadClient.MakeRequest(string.Concat(publicationsUrl, "0")); JsonObject? result = JsonSerializer.Deserialize(requestResult.result); if (result is null) break; @@ -116,7 +116,7 @@ public class MangaDex : Connector { offset += limit; DownloadClient.RequestResult requestResult = - _downloadClient.GetPage($"https://api.mangadex.org/manga/{id}/feed?limit={limit}&offset={offset}"); + _downloadClient.MakeRequest($"https://api.mangadex.org/manga/{id}/feed?limit={limit}&offset={offset}"); JsonObject? result = JsonSerializer.Deserialize(requestResult.result); if (result is null) break; @@ -149,7 +149,7 @@ public class MangaDex : Connector public override void DownloadChapter(Publication publication, Chapter chapter) { DownloadClient.RequestResult requestResult = - _downloadClient.GetPage($"https://api.mangadex.org/at-home/server/{chapter.url}?forcePort443=false'"); + _downloadClient.MakeRequest($"https://api.mangadex.org/at-home/server/{chapter.url}?forcePort443=false'"); JsonObject? result = JsonSerializer.Deserialize(requestResult.result); if (result is null) return;