Skip to content

Commit

Permalink
Merge branch 'cuttingedge' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
C9Glax committed Apr 15, 2024
2 parents 590547e + c25a4f6 commit e022bf3
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Tranga/Jobs/JobBoss.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private void LoadJobsList(HashSet<MangaConnector> connectors)
}

HashSet<string> coverFileNames = cachedPublications.Select(manga => manga.coverFileNameInCache!).ToHashSet();
foreach (string fileName in Directory.GetFiles(settings.coverImageCache))
foreach (string fileName in Directory.GetFiles(settings.coverImageCache)) //Cleanup Unused Covers
{
if(!coverFileNames.Any(existingManga => fileName.Contains(existingManga)))
File.Delete(fileName);
Expand Down
3 changes: 2 additions & 1 deletion Tranga/MangaConnectors/ChromiumDownloadClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text;
using HtmlAgilityPack;
using PuppeteerSharp;
using PuppeteerSharp.Input;

namespace Tranga.MangaConnectors;

Expand Down Expand Up @@ -81,7 +82,7 @@ protected override RequestResult MakeRequestInternal(string url, string? referre
{
if (content.Contains("text/html"))
{
if(clickButton is not null)
if (clickButton is not null && page.QuerySelectorAsync(clickButton).Result is not null)
page.ClickAsync(clickButton).Wait();
string htmlString = page.GetContentAsync().Result;
stream = new MemoryStream(Encoding.Default.GetBytes(htmlString));
Expand Down
6 changes: 4 additions & 2 deletions Tranga/MangaConnectors/MangaConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ protected HttpStatusCode DownloadChapterImages(string[] imageUrls, string saveAr
return HttpStatusCode.Created;

//Create a temporary folder to store images
string tempFolder = Directory.CreateTempSubdirectory().FullName;
string tempFolder = Directory.CreateTempSubdirectory("trangatemp").FullName;

int chapter = 0;
//Download all Images to temporary Folder
Expand All @@ -260,8 +260,10 @@ protected HttpStatusCode DownloadChapterImages(string[] imageUrls, string saveAr
progressToken?.Increment();
}

if(comicInfoPath is not null)
if(comicInfoPath is not null){
File.Copy(comicInfoPath, Path.Join(tempFolder, "ComicInfo.xml"));
File.Delete(comicInfoPath); //Delete tmp-file
}

Log($"Creating archive {saveArchiveFilePath}");
//ZIP-it and ship-it
Expand Down
11 changes: 10 additions & 1 deletion Tranga/MangaConnectors/MangaDex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public override Chapter[] GetChapters(Manga manga, string language="en")
{
JsonObject chapter = (JsonObject)jsonNode!;
JsonObject attributes = chapter["attributes"]!.AsObject();

string chapterId = chapter["id"]!.GetValue<string>();

string? title = attributes.ContainsKey("title") && attributes["title"] is not null
Expand All @@ -230,6 +231,14 @@ public override Chapter[] GetChapters(Manga manga, string language="en")
? attributes["chapter"]!.GetValue<string>()
: "null";


if (attributes.ContainsKey("pages") && attributes["pages"] is not null &&
attributes["pages"]!.GetValue<int>() < 1)
{
Log($"Skipping {chapterId} Vol.{volume} Ch.{chapterNum} {title} because it has no pages or is externally linked.");
continue;
}

if(chapterNum is not "null")
chapters.Add(new Chapter(manga, title, volume, chapterNum, chapterId));
}
Expand All @@ -252,7 +261,7 @@ public override HttpStatusCode DownloadChapter(Chapter chapter, ProgressToken? p
Log($"Retrieving chapter-info {chapter} {chapterParentManga}");
//Request URLs for Chapter-Images
RequestResult requestResult =
downloadClient.MakeRequest($"https://api.mangadex.org/at-home/server/{chapter.url}?forcePort443=false'", RequestType.MangaDexImage);
downloadClient.MakeRequest($"https://api.mangadex.org/at-home/server/{chapter.url}?forcePort443=false", RequestType.MangaDexImage);
if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300)
{
progressToken?.Cancel();
Expand Down
15 changes: 14 additions & 1 deletion Tranga/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ private void HandleGet(HttpListenerRequest request, HttpListenerResponse respons
case "Settings/customRequestLimit":
SendResponse(HttpStatusCode.OK, response, settings.requestLimits);
break;
case "Settings/AprilFoolsMode":
SendResponse(HttpStatusCode.OK, response, settings.aprilFoolsMode);
break;
case "NotificationConnectors":
SendResponse(HttpStatusCode.OK, response, notificationConnectors);
break;
Expand Down Expand Up @@ -397,14 +400,24 @@ private void HandlePost(HttpListenerRequest request, HttpListenerResponse respon
case "Settings/UpdateDownloadLocation":
if (!requestVariables.TryGetValue("downloadLocation", out string? downloadLocation) ||
!requestVariables.TryGetValue("moveFiles", out string? moveFilesStr) ||
!Boolean.TryParse(moveFilesStr, out bool moveFiles))
!bool.TryParse(moveFilesStr, out bool moveFiles))
{
SendResponse(HttpStatusCode.BadRequest, response);
break;
}
settings.UpdateDownloadLocation(downloadLocation, moveFiles);
SendResponse(HttpStatusCode.Accepted, response);
break;
case "Settings/AprilFoolsMode":
if (!requestVariables.TryGetValue("enabled", out string? aprilFoolsModeEnabledStr) ||
bool.TryParse(aprilFoolsModeEnabledStr, out bool aprilFoolsModeEnabled))
{
SendResponse(HttpStatusCode.BadRequest, response);
break;
}
settings.UpdateAprilFoolsMode(aprilFoolsModeEnabled);
SendResponse(HttpStatusCode.Accepted, response);
break;
/*case "Settings/UpdateWorkingDirectory":
if (!requestVariables.TryGetValue("workingDirectory", out string? workingDirectory))
{
Expand Down
17 changes: 16 additions & 1 deletion Tranga/Tranga.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public Tranga(Logger? logger, TrangaSettings settings) : base(logger, settings)
new Bato(this),
new MangaLife(this)
};
foreach(DirectoryInfo dir in new DirectoryInfo(Path.GetTempPath()).GetDirectories("trangatemp"))//Cleanup old temp folders
dir.Delete();
jobBoss = new(this, this._connectors);
StartJobBoss();
this._server = new Server(this);
Expand Down Expand Up @@ -71,10 +73,23 @@ private void StartJobBoss()
{
while (keepRunning)
{
jobBoss.CheckJobs();
if(!settings.aprilFoolsMode & !IsAprilFirst())
jobBoss.CheckJobs();
else
Log("April Fools Mode in Effect");
Thread.Sleep(100);
}
});
t.Start();
}

private bool IsAprilFirst()
{
//UTC 01 Apr +-12hrs
DateTime start = new DateTime(DateTime.Now.Year, 03, 31, 12, 0, 0, DateTimeKind.Utc);
DateTime end = new DateTime(DateTime.Now.Year, 04, 02, 12, 0, 0, DateTimeKind.Utc);
if (DateTime.UtcNow > start && DateTime.UtcNow < end)
return true;
return false;
}
}
14 changes: 10 additions & 4 deletions Tranga/TrangaSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using Tranga.LibraryConnectors;
using Tranga.MangaConnectors;
Expand All @@ -13,14 +12,15 @@ public class TrangaSettings
public string downloadLocation { get; private set; }
public string workingDirectory { get; private set; }
public int apiPortNumber { get; init; }
public string userAgent { get; set; } = DefaultUserAgent;
public string userAgent { get; private set; } = DefaultUserAgent;
[JsonIgnore] public string settingsFilePath => Path.Join(workingDirectory, "settings.json");
[JsonIgnore] public string libraryConnectorsFilePath => Path.Join(workingDirectory, "libraryConnectors.json");
[JsonIgnore] public string notificationConnectorsFilePath => Path.Join(workingDirectory, "notificationConnectors.json");
[JsonIgnore] public string jobsFolderPath => Path.Join(workingDirectory, "jobs");
[JsonIgnore] public string coverImageCache => Path.Join(workingDirectory, "imageCache");
[JsonIgnore] internal static readonly string DefaultUserAgent = $"Tranga ({Enum.GetName(Environment.OSVersion.Platform)}; {(Environment.Is64BitOperatingSystem ? "x64" : "")}) / 1.0";
public ushort? version { get; set; } = 1;
public ushort? version { get; } = 1;
public bool aprilFoolsMode { get; private set; } = true;
[JsonIgnore]internal static readonly Dictionary<RequestType, int> DefaultRequestLimits = new ()
{
{RequestType.MangaInfo, 250},
Expand Down Expand Up @@ -102,6 +102,12 @@ public HashSet<NotificationConnector> LoadNotificationConnectors(GlobalBase clon
})!;
}

public void UpdateAprilFoolsMode(bool enabled)
{
this.aprilFoolsMode = enabled;
ExportSettings();
}

public void UpdateDownloadLocation(string newPath, bool moveFiles = true)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
Expand Down

0 comments on commit e022bf3

Please sign in to comment.