mirror of
https://github.com/C9Glax/tranga.git
synced 2025-10-11 05:09:49 +02:00
Use DelegatingHandler for RateLimits
Some checks failed
Docker Image CI / build (push) Has been cancelled
Some checks failed
Docker Image CI / build (push) Has been cancelled
This commit is contained in:
@@ -5,9 +5,7 @@ using API.MangaDownloadClients;
|
||||
using API.Schema.MangaContext;
|
||||
using log4net;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Formats.Jpeg;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
|
||||
namespace API.MangaConnectors;
|
||||
@@ -15,7 +13,7 @@ namespace API.MangaConnectors;
|
||||
[PrimaryKey("Name")]
|
||||
public abstract class MangaConnector(string name, string[] supportedLanguages, string[] baseUris, string iconUrl)
|
||||
{
|
||||
[NotMapped] internal DownloadClient downloadClient { get; init; } = null!;
|
||||
[NotMapped] internal IDownloadClient downloadClient { get; init; } = null!;
|
||||
[NotMapped] protected ILog Log { get; init; } = LogManager.GetLogger(name);
|
||||
[StringLength(32)] public string Name { get; init; } = name;
|
||||
[StringLength(8)] public string[] SupportedLanguages { get; init; } = supportedLanguages;
|
||||
@@ -50,14 +48,14 @@ public abstract class MangaConnector(string name, string[] supportedLanguages, s
|
||||
if (File.Exists(saveImagePath))
|
||||
return filename;
|
||||
|
||||
RequestResult coverResult = downloadClient.MakeRequest(mangaId.Obj.CoverUrl, RequestType.MangaCover, $"https://{match.Groups[1].Value}");
|
||||
if ((int)coverResult.statusCode < 200 || (int)coverResult.statusCode >= 300)
|
||||
HttpResponseMessage coverResult = downloadClient.MakeRequest(mangaId.Obj.CoverUrl, RequestType.MangaCover, $"https://{match.Groups[1].Value}").Result;
|
||||
if ((int)coverResult.StatusCode < 200 || (int)coverResult.StatusCode >= 300)
|
||||
return SaveCoverImageToCache(mangaId, --retries);
|
||||
|
||||
try
|
||||
{
|
||||
using MemoryStream ms = new();
|
||||
coverResult.result.CopyTo(ms);
|
||||
coverResult.Content.ReadAsStream().CopyTo(ms);
|
||||
byte[] imageBytes = ms.ToArray();
|
||||
Directory.CreateDirectory(TrangaSettings.CoverImageCacheOriginal);
|
||||
File.WriteAllBytes(saveImagePath, imageBytes);
|
||||
|
@@ -35,14 +35,14 @@ public class MangaDex : MangaConnector
|
||||
$"&includes%5B%5D=manga&includes%5B%5D=cover_art&includes%5B%5D=author&includes%5B%5D=artist&includes%5B%5D=tag'";
|
||||
offset += Limit;
|
||||
|
||||
RequestResult result = downloadClient.MakeRequest(requestUrl, RequestType.MangaDexFeed);
|
||||
if ((int)result.statusCode < 200 || (int)result.statusCode >= 300)
|
||||
HttpResponseMessage result = downloadClient.MakeRequest(requestUrl, RequestType.MangaDexFeed).Result;
|
||||
if ((int)result.StatusCode < 200 || (int)result.StatusCode >= 300)
|
||||
{
|
||||
Log.Error("Request failed");
|
||||
return [];
|
||||
}
|
||||
|
||||
using StreamReader sr = new (result.result);
|
||||
using StreamReader sr = new (result.Content.ReadAsStream());
|
||||
JObject jObject = JObject.Parse(sr.ReadToEnd());
|
||||
|
||||
if (jObject.Value<string>("result") != "ok")
|
||||
@@ -96,14 +96,14 @@ public class MangaDex : MangaConnector
|
||||
$"https://api.mangadex.org/manga/{mangaIdOnSite}" +
|
||||
$"?includes%5B%5D=manga&includes%5B%5D=cover_art&includes%5B%5D=author&includes%5B%5D=artist&includes%5B%5D=tag'";
|
||||
|
||||
RequestResult result = downloadClient.MakeRequest(requestUrl, RequestType.MangaDexFeed);
|
||||
if ((int)result.statusCode < 200 || (int)result.statusCode >= 300)
|
||||
HttpResponseMessage result = downloadClient.MakeRequest(requestUrl, RequestType.MangaDexFeed).Result;
|
||||
if ((int)result.StatusCode < 200 || (int)result.StatusCode >= 300)
|
||||
{
|
||||
Log.Error("Request failed");
|
||||
return null;
|
||||
}
|
||||
|
||||
using StreamReader sr = new (result.result);
|
||||
using StreamReader sr = new (result.Content.ReadAsStream());
|
||||
JObject jObject = JObject.Parse(sr.ReadToEnd());
|
||||
|
||||
if (jObject.Value<string>("result") != "ok")
|
||||
@@ -138,14 +138,14 @@ public class MangaDex : MangaConnector
|
||||
$"contentRating%5B%5D=safe&contentRating%5B%5D=suggestive&contentRating%5B%5D=erotica&includeFutureUpdates=0&includes%5B%5D=";
|
||||
offset += Limit;
|
||||
|
||||
RequestResult result = downloadClient.MakeRequest(requestUrl, RequestType.MangaDexFeed);
|
||||
if ((int)result.statusCode < 200 || (int)result.statusCode >= 300)
|
||||
HttpResponseMessage result = downloadClient.MakeRequest(requestUrl, RequestType.MangaDexFeed).Result;
|
||||
if ((int)result.StatusCode < 200 || (int)result.StatusCode >= 300)
|
||||
{
|
||||
Log.Error("Request failed");
|
||||
return [];
|
||||
}
|
||||
|
||||
using StreamReader sr = new (result.result);
|
||||
using StreamReader sr = new (result.Content.ReadAsStream());
|
||||
JObject jObject = JObject.Parse(sr.ReadToEnd());
|
||||
|
||||
if (jObject.Value<string>("result") != "ok")
|
||||
@@ -191,14 +191,14 @@ public class MangaDex : MangaConnector
|
||||
string id = match.Groups[1].Value;
|
||||
string requestUrl = $"https://api.mangadex.org/at-home/server/{id}";
|
||||
|
||||
RequestResult result = downloadClient.MakeRequest(requestUrl, RequestType.Default);
|
||||
if ((int)result.statusCode < 200 || (int)result.statusCode >= 300)
|
||||
HttpResponseMessage result = downloadClient.MakeRequest(requestUrl, RequestType.Default).Result;
|
||||
if ((int)result.StatusCode < 200 || (int)result.StatusCode >= 300)
|
||||
{
|
||||
Log.Error("Request failed");
|
||||
return [];
|
||||
}
|
||||
|
||||
using StreamReader sr = new (result.result);
|
||||
using StreamReader sr = new (result.Content.ReadAsStream());
|
||||
JObject jObject = JObject.Parse(sr.ReadToEnd());
|
||||
|
||||
if (jObject.Value<string>("result") != "ok")
|
||||
|
@@ -36,7 +36,7 @@ public class MangaPark : MangaConnector
|
||||
for (int page = 1;; page++) // break; in loop
|
||||
{
|
||||
Uri searchUri = new(baseUri, $"search?word={HttpUtility.UrlEncode(mangaSearchName)}&lang={Tranga.Settings.DownloadLanguage}&page={page}");
|
||||
if (downloadClient.MakeRequest(searchUri.ToString(), RequestType.Default) is { statusCode: >= HttpStatusCode.OK and < HttpStatusCode.Ambiguous } result)
|
||||
if (downloadClient.MakeRequest(searchUri.ToString(), RequestType.Default).Result is { StatusCode: >= HttpStatusCode.OK and < HttpStatusCode.Ambiguous } result)
|
||||
{
|
||||
HtmlDocument document = result.CreateDocument();
|
||||
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract HAP sucks with nullable types
|
||||
@@ -73,8 +73,8 @@ public class MangaPark : MangaConnector
|
||||
|
||||
public override (Manga, MangaConnectorId<Manga>)? GetMangaFromUrl(string url)
|
||||
{
|
||||
if (downloadClient.MakeRequest(url, RequestType.Default) is
|
||||
{ statusCode: >= HttpStatusCode.OK and < HttpStatusCode.Ambiguous } result)
|
||||
if (downloadClient.MakeRequest(url, RequestType.Default).Result is
|
||||
{ StatusCode: >= HttpStatusCode.OK and < HttpStatusCode.Ambiguous } result)
|
||||
{
|
||||
HtmlDocument document= result.CreateDocument();
|
||||
|
||||
@@ -145,8 +145,8 @@ public class MangaPark : MangaConnector
|
||||
|
||||
List<(Chapter, MangaConnectorId<Chapter>)> ret = [];
|
||||
|
||||
if (downloadClient.MakeRequest(requestUri.ToString(), RequestType.Default) is
|
||||
{ statusCode: >= HttpStatusCode.OK and < HttpStatusCode.Ambiguous } result)
|
||||
if (downloadClient.MakeRequest(requestUri.ToString(), RequestType.Default).Result is
|
||||
{ StatusCode: >= HttpStatusCode.OK and < HttpStatusCode.Ambiguous } result)
|
||||
{
|
||||
HtmlDocument document= result.CreateDocument();
|
||||
|
||||
@@ -220,8 +220,8 @@ public class MangaPark : MangaConnector
|
||||
Log.Debug($"Using domain {domain}");
|
||||
Uri baseUri = new ($"https://{domain}/");
|
||||
Uri requestUri = new (baseUri, $"title/{chapterId.IdOnConnectorSite}");
|
||||
if (downloadClient.MakeRequest(requestUri.ToString(), RequestType.Default) is
|
||||
{ statusCode: >= HttpStatusCode.OK and < HttpStatusCode.Ambiguous } result)
|
||||
if (downloadClient.MakeRequest(requestUri.ToString(), RequestType.Default).Result is
|
||||
{ StatusCode: >= HttpStatusCode.OK and < HttpStatusCode.Ambiguous } result)
|
||||
{
|
||||
HtmlDocument document = result.CreateDocument();
|
||||
|
||||
@@ -240,10 +240,10 @@ public class MangaPark : MangaConnector
|
||||
|
||||
internal static class MangaParkHelper
|
||||
{
|
||||
internal static HtmlDocument CreateDocument(this RequestResult result)
|
||||
internal static HtmlDocument CreateDocument(this HttpResponseMessage result)
|
||||
{
|
||||
HtmlDocument document = new();
|
||||
StreamReader sr = new (result.result);
|
||||
StreamReader sr = new (result.Content.ReadAsStream());
|
||||
string htmlStr = sr.ReadToEnd().Replace("q:key", "qkey");
|
||||
document.LoadHtml(htmlStr);
|
||||
|
||||
|
@@ -30,11 +30,11 @@ public sealed class Mangaworld : MangaConnector
|
||||
Uri baseUri = new ("https://www.mangaworld.cx/");
|
||||
Uri searchUrl = new (baseUri, "archive?keyword=" + HttpUtility.UrlEncode(mangaSearchName));
|
||||
|
||||
RequestResult res = downloadClient.MakeRequest(searchUrl.ToString(), RequestType.Default);
|
||||
if ((int)res.statusCode < 200 || (int)res.statusCode >= 300)
|
||||
HttpResponseMessage res = downloadClient.MakeRequest(searchUrl.ToString(), RequestType.Default).Result;
|
||||
if ((int)res.StatusCode < 200 || (int)res.StatusCode >= 300)
|
||||
return [];
|
||||
|
||||
using StreamReader sr = new (res.result);
|
||||
using StreamReader sr = new (res.Content.ReadAsStream());
|
||||
string html = sr.ReadToEnd();
|
||||
|
||||
HtmlDocument doc = new ();
|
||||
@@ -85,11 +85,11 @@ public sealed class Mangaworld : MangaConnector
|
||||
string slug = parts[1];
|
||||
|
||||
string url = $"https://www.mangaworld.cx/manga/{id}/{slug}/";
|
||||
RequestResult res = downloadClient.MakeRequest(url, RequestType.MangaInfo);
|
||||
if ((int)res.statusCode < 200 || (int)res.statusCode >= 300)
|
||||
HttpResponseMessage res = downloadClient.MakeRequest(url, RequestType.MangaInfo).Result;
|
||||
if ((int)res.StatusCode < 200 || (int)res.StatusCode >= 300)
|
||||
return null;
|
||||
|
||||
using StreamReader sr = new (res.result);
|
||||
using StreamReader sr = new (res.Content.ReadAsStream());
|
||||
string html = sr.ReadToEnd();
|
||||
|
||||
HtmlDocument doc = new ();
|
||||
@@ -175,11 +175,11 @@ public sealed class Mangaworld : MangaConnector
|
||||
{
|
||||
string url = EnsureListStyle(chapterId.WebsiteUrl ?? $"https://www.mangaworld.cx/manga/{chapterId.IdOnConnectorSite}");
|
||||
|
||||
RequestResult res = downloadClient.MakeRequest(url, RequestType.MangaInfo);
|
||||
if ((int)res.statusCode < 200 || (int)res.statusCode >= 300)
|
||||
HttpResponseMessage res = downloadClient.MakeRequest(url, RequestType.MangaInfo).Result;
|
||||
if ((int)res.StatusCode < 200 || (int)res.StatusCode >= 300)
|
||||
return [];
|
||||
|
||||
using StreamReader sr = new (res.result);
|
||||
using StreamReader sr = new (res.Content.ReadAsStream());
|
||||
string html = sr.ReadToEnd();
|
||||
|
||||
Uri baseUri = new (url);
|
||||
@@ -354,20 +354,20 @@ public sealed class Mangaworld : MangaConnector
|
||||
baseUri = new (seriesUrl);
|
||||
|
||||
// 1) tenta client "Default"
|
||||
RequestResult res = downloadClient.MakeRequest(seriesUrl, RequestType.Default);
|
||||
if ((int)res.statusCode >= 200 && (int)res.statusCode < 300)
|
||||
HttpResponseMessage res = downloadClient.MakeRequest(seriesUrl, RequestType.Default).Result;
|
||||
if ((int)res.StatusCode >= 200 && (int)res.StatusCode < 300)
|
||||
{
|
||||
using StreamReader sr = new (res.result);
|
||||
using StreamReader sr = new (res.Content.ReadAsStream());
|
||||
string html = sr.ReadToEnd();
|
||||
if (!LooksLikeChallenge(html))
|
||||
return html;
|
||||
}
|
||||
|
||||
// 2) fallback: client “MangaInfo” (proxy/Flare se configurato)
|
||||
RequestResult res2 = downloadClient.MakeRequest(seriesUrl, RequestType.MangaInfo);
|
||||
if ((int)res2.statusCode >= 200 && (int)res2.statusCode < 300)
|
||||
HttpResponseMessage res2 = downloadClient.MakeRequest(seriesUrl, RequestType.MangaInfo).Result;
|
||||
if ((int)res2.StatusCode >= 200 && (int)res2.StatusCode < 300)
|
||||
{
|
||||
using StreamReader sr2 = new StreamReader(res2.result);
|
||||
using StreamReader sr2 = new StreamReader(res2.Content.ReadAsStream());
|
||||
return sr2.ReadToEnd();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user