Compare commits
No commits in common. "9a640aed2735e3a9c28dff4ab724550861f649cb" and "9b70994f71b06bf3fc2da5b371b33a5a703fc5b5" have entirely different histories.
9a640aed27
...
9b70994f71
@ -52,7 +52,7 @@ public abstract class LoggerBase : TextWriter
|
||||
public override string ToString()
|
||||
{
|
||||
string dateTimeString = $"{logTime.ToShortDateString()} {logTime.ToLongTimeString()}";
|
||||
return $"[{dateTimeString}] {caller.Split(new char[]{'.','+'}).Last(),15} | {value}";
|
||||
return $"[{dateTimeString}] {caller,30} | {value}";
|
||||
}
|
||||
}
|
||||
}
|
@ -209,12 +209,8 @@ public static class Tranga_Cli
|
||||
$"{"",-5}{"Task",-20} | {"Last Executed",-20} | {"Reoccurrence",-12} | {"State",-10} | {"Connector",-15} | Publication/Manga";
|
||||
Console.WriteLine(header);
|
||||
Console.WriteLine(new string('-', header.Length));
|
||||
foreach (TrangaTask trangaTask in tasks)
|
||||
{
|
||||
string[] taskSplit = trangaTask.ToString().Split(", ");
|
||||
Console.WriteLine($"{tIndex++:000}: {taskSplit[0],-20} | {taskSplit[1],-20} | {taskSplit[2],-12} | {taskSplit[3],-10} | {(taskSplit.Length > 4 ? taskSplit[4] : ""),-15} | {(taskSplit.Length > 5 ? taskSplit[5] : "")}");
|
||||
}
|
||||
|
||||
foreach(TrangaTask trangaTask in tasks)
|
||||
Console.WriteLine($"{tIndex++:000}: {trangaTask}");
|
||||
}
|
||||
|
||||
private static TrangaTask? SelectTask(TrangaTask[] tasks, Logger logger)
|
||||
|
@ -169,21 +169,19 @@ public abstract class Connector
|
||||
private static readonly HttpClient Client = new();
|
||||
|
||||
private readonly Dictionary<byte, DateTime> _lastExecutedRateLimit;
|
||||
private readonly Dictionary<byte, TimeSpan> _rateLimit;
|
||||
private Logger? logger;
|
||||
private readonly Dictionary<byte, TimeSpan> _RateLimit;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a httpClient
|
||||
/// </summary>
|
||||
/// <param name="delay">minimum delay between requests (to avoid spam)</param>
|
||||
/// <param name="rateLimitRequestsPerMinute">Rate limits for requests. byte is RequestType, int maximum requests per minute for RequestType</param>
|
||||
public DownloadClient(Dictionary<byte, int> rateLimitRequestsPerMinute, Logger? logger)
|
||||
public DownloadClient(Dictionary<byte, int> rateLimitRequestsPerMinute)
|
||||
{
|
||||
this.logger = logger;
|
||||
_lastExecutedRateLimit = new();
|
||||
_rateLimit = new();
|
||||
_RateLimit = new();
|
||||
foreach(KeyValuePair<byte, int> limit in rateLimitRequestsPerMinute)
|
||||
_rateLimit.Add(limit.Key, TimeSpan.FromMinutes(1).Divide(limit.Value));
|
||||
_RateLimit.Add(limit.Key, TimeSpan.FromMinutes(1).Divide(limit.Value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -194,36 +192,19 @@ public abstract class Connector
|
||||
/// <returns>RequestResult with StatusCode and Stream of received data</returns>
|
||||
public RequestResult MakeRequest(string url, byte requestType)
|
||||
{
|
||||
if (_rateLimit.TryGetValue(requestType, out TimeSpan value))
|
||||
if (_RateLimit.TryGetValue(requestType, out TimeSpan value))
|
||||
_lastExecutedRateLimit.TryAdd(requestType, DateTime.Now.Subtract(value));
|
||||
else
|
||||
{
|
||||
logger?.WriteLine(this.GetType().ToString(), "RequestType not configured for rate-limit.");
|
||||
return new RequestResult(HttpStatusCode.NotAcceptable, Stream.Null);
|
||||
}
|
||||
|
||||
TimeSpan rateLimitTimeout = _rateLimit[requestType]
|
||||
.Subtract(DateTime.Now.Subtract(_lastExecutedRateLimit[requestType]));
|
||||
|
||||
Thread.Sleep(rateLimitTimeout);
|
||||
while(DateTime.Now.Subtract(_lastExecutedRateLimit[requestType]) < _RateLimit[requestType])
|
||||
Thread.Sleep(10);
|
||||
_lastExecutedRateLimit[requestType] = DateTime.Now;
|
||||
|
||||
HttpResponseMessage? response = null;
|
||||
while (response is null)
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpRequestMessage requestMessage = new(HttpMethod.Get, url);
|
||||
_lastExecutedRateLimit[requestType] = DateTime.Now;
|
||||
response = Client.Send(requestMessage);
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
Thread.Sleep(_rateLimit[requestType] * 2);
|
||||
}
|
||||
}
|
||||
HttpRequestMessage requestMessage = new(HttpMethod.Get, url);
|
||||
HttpResponseMessage response = Client.Send(requestMessage);
|
||||
Stream resultString = response.IsSuccessStatusCode ? response.Content.ReadAsStream() : Stream.Null;
|
||||
if (!response.IsSuccessStatusCode)
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Request-Error {response.StatusCode}: {response.ReasonPhrase}");
|
||||
return new RequestResult(response.StatusCode, resultString);
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,7 @@ public class MangaDex : Connector
|
||||
{(byte)RequestType.AtHomeServer, 40},
|
||||
{(byte)RequestType.Cover, 250},
|
||||
{(byte)RequestType.Author, 250}
|
||||
}, logger);
|
||||
});
|
||||
}
|
||||
|
||||
public override Publication[] GetPublications(string publicationTitle = "")
|
||||
@ -270,26 +270,29 @@ public class MangaDex : Connector
|
||||
if(!Directory.Exists(publicationFolder))
|
||||
Directory.CreateDirectory(publicationFolder);
|
||||
DirectoryInfo dirInfo = new (publicationFolder);
|
||||
if (dirInfo.EnumerateFiles().Any(info => info.Name.Contains("cover.")))
|
||||
foreach(FileInfo fileInfo in dirInfo.EnumerateFiles())
|
||||
if (fileInfo.Name.Contains("cover."))
|
||||
{
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Cover exists {publication.sortName}");
|
||||
return;
|
||||
}
|
||||
|
||||
if (publication.posterUrl is null)
|
||||
{
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Cover exists {publication.sortName}");
|
||||
logger?.WriteLine(this.GetType().ToString(), $"No posterurl in publication");
|
||||
return;
|
||||
}
|
||||
|
||||
if (publication.posterUrl is null || !(bool)publication.posterUrl?.Contains("http"))
|
||||
{
|
||||
logger?.WriteLine(this.GetType().ToString(), $"No Poster-URL in publication");
|
||||
return;
|
||||
}
|
||||
string coverUrl = publication.posterUrl;
|
||||
|
||||
//Get file-extension (jpg, png)
|
||||
string[] split = publication.posterUrl.Split('.');
|
||||
string[] split = coverUrl.Split('.');
|
||||
string extension = split[^1];
|
||||
|
||||
string outFolderPath = Path.Join(downloadLocation, publication.folderName);
|
||||
Directory.CreateDirectory(outFolderPath);
|
||||
|
||||
//Download cover-Image
|
||||
DownloadImage(publication.posterUrl, Path.Join(downloadLocation, publication.folderName, $"cover.{extension}"), this.downloadClient, (byte)RequestType.AtHomeServer);
|
||||
DownloadImage(coverUrl, Path.Join(downloadLocation, publication.folderName, $"cover.{extension}"), this.downloadClient, (byte)RequestType.AtHomeServer);
|
||||
}
|
||||
}
|
@ -92,13 +92,13 @@ public static class TaskExecutor
|
||||
/// <param name="chapterCollection"></param>
|
||||
private static void DownloadNewChapters(Connector connector, Publication publication, string language, ref Dictionary<Publication, List<Chapter>> chapterCollection)
|
||||
{
|
||||
//Check if Publication already has a Folder
|
||||
List<Chapter> newChapters = UpdateChapters(connector, publication, language, ref chapterCollection);
|
||||
connector.DownloadCover(publication);
|
||||
|
||||
//Check if Publication already has a Folder and a series.json
|
||||
string publicationFolder = Path.Join(connector.downloadLocation, publication.folderName);
|
||||
if(!Directory.Exists(publicationFolder))
|
||||
Directory.CreateDirectory(publicationFolder);
|
||||
List<Chapter> newChapters = UpdateChapters(connector, publication, language, ref chapterCollection);
|
||||
|
||||
connector.DownloadCover(publication);
|
||||
|
||||
string seriesInfoPath = Path.Join(publicationFolder, "series.json");
|
||||
if(!File.Exists(seriesInfoPath))
|
||||
|
@ -56,6 +56,6 @@ public class TrangaTask
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{task}, {lastExecuted}, {reoccurrence}, {state} {(connectorName is not null ? $", {connectorName}" : "" )} {(publication is not null ? $", {publication?.sortName}": "")}";
|
||||
return $"{task,-20} | {lastExecuted,-20} | {reoccurrence,-12} | {state,-10} | {connectorName,-15} | {publication?.sortName}";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user