Added delay functionality for rate-limits

This commit is contained in:
glax 2023-05-18 17:18:41 +02:00
parent ae29b8f341
commit 93bb8ef6ee
2 changed files with 17 additions and 5 deletions

View File

@ -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;

View File

@ -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<JsonObject>(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<JsonObject>(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<JsonObject>(requestResult.result);
if (result is null)
return;