2
0

Compare commits

..

No commits in common. "a92eba2d14ff7b0ad67bb9cdde6645e7bd397a95" and "7b6724ad384ed9857c8308bfa7626cf81122cfe6" have entirely different histories.

13 changed files with 44 additions and 155 deletions

View File

@ -84,26 +84,24 @@ public readonly struct Chapter : IComparable
/// <returns>true if chapter is present</returns> /// <returns>true if chapter is present</returns>
internal bool CheckChapterIsDownloaded() internal bool CheckChapterIsDownloaded()
{ {
string mangaDirectory = Path.Join(TrangaSettings.downloadLocation, parentManga.folderName); if (!Directory.Exists(Path.Join(TrangaSettings.downloadLocation, parentManga.folderName)))
if (!Directory.Exists(mangaDirectory))
return false; return false;
FileInfo[] archives = new DirectoryInfo(mangaDirectory).GetFiles("*.cbz"); FileInfo[] archives = new DirectoryInfo(Path.Join(TrangaSettings.downloadLocation, parentManga.folderName)).GetFiles().Where(file => file.Name.Split('.')[^1] == "cbz").ToArray();
Regex volChRex = new(@"(?:Vol(?:ume)?\.([0-9]+)\D*)?Ch(?:apter)?\.([0-9]+(?:\.[0-9]+)*)"); Regex volChRex = new(@"(?:Vol(?:ume)?\.([0-9]+)\D*)?Ch(?:apter)?\.([0-9]+(?:\.[0-9]+)*)");
Chapter t = this; Chapter t = this;
string correctPath = GetArchiveFilePath(); string thisPath = GetArchiveFilePath();
FileInfo? archive = archives.FirstOrDefault(archive => FileInfo? archive = archives.FirstOrDefault(archive =>
{ {
Match m = volChRex.Match(archive.Name); Match m = volChRex.Match(archive.Name);
/*Uncommenting this section will only allow *Version without Volume number* -> *Version with Volume number* but not the other way string archiveVolNum = m.Groups[1].Success ? m.Groups[1].Value : "0";
if (m.Groups[1].Success) string archiveChNum = m.Groups[2].Value;
return m.Groups[1].Value == t.volumeNumber && m.Groups[2].Value == t.chapterNumber; return archiveVolNum == t.volumeNumber && archiveChNum == t.chapterNumber ||
else*/ archiveVolNum == "0" && archiveChNum == t.chapterNumber;
return m.Groups[2].Value == t.chapterNumber;
}); });
if(archive is not null && archive.FullName != correctPath) if(archive is not null && thisPath != archive.FullName)
archive.MoveTo(correctPath, true); archive.MoveTo(thisPath, true);
return (archive is not null); return archive is not null;
} }
/// <summary> /// <summary>
/// Creates full file path of chapter-archive /// Creates full file path of chapter-archive

View File

@ -125,10 +125,10 @@ public abstract class GlobalBase
Log(string.Format(fStr, replace)); Log(string.Format(fStr, replace));
} }
protected void SendNotifications(string title, string text, bool buffer = false) protected void SendNotifications(string title, string text)
{ {
foreach (NotificationConnector nc in notificationConnectors) foreach (NotificationConnector nc in notificationConnectors)
nc.SendNotification(title, text, buffer); nc.SendNotification(title, text);
} }
protected void AddNotificationConnector(NotificationConnector notificationConnector) protected void AddNotificationConnector(NotificationConnector notificationConnector)

View File

@ -37,7 +37,7 @@ public class DownloadChapter : Job
if (success == HttpStatusCode.OK) if (success == HttpStatusCode.OK)
{ {
UpdateLibraries(); UpdateLibraries();
SendNotifications("Chapter downloaded", $"{chapter.parentManga.sortName} - {chapter.chapterNumber}", true); SendNotifications("Chapter downloaded", $"{chapter.parentManga.sortName} - {chapter.chapterNumber}");
} }
}); });
downloadTask.Start(); downloadTask.Start();

View File

@ -204,7 +204,7 @@ public class JobBoss : GlobalBase
internal void UpdateJobFile(Job job, string? oldFile = null) internal void UpdateJobFile(Job job, string? oldFile = null)
{ {
string newJobFilePath = Path.Join(TrangaSettings.jobsFolderPath, $"{job.id}.json"); string newJobFilePath = Path.Join(TrangaSettings.jobsFolderPath, $"{job.id}.json");
string oldFilePath = Path.Join(TrangaSettings.jobsFolderPath, oldFile??$"{job.id}.json"); string oldFilePath = Path.Join(TrangaSettings.jobsFolderPath, oldFile);
//Delete old file //Delete old file
if (File.Exists(oldFilePath)) if (File.Exists(oldFilePath))

View File

@ -61,7 +61,7 @@ public class Kavita : LibraryConnector
return ""; return "";
} }
protected override void UpdateLibraryInternal() public override void UpdateLibrary()
{ {
Log("Updating libraries."); Log("Updating libraries.");
foreach (KavitaLibrary lib in GetLibraries()) foreach (KavitaLibrary lib in GetLibraries())

View File

@ -25,7 +25,7 @@ public class Komga : LibraryConnector
return $"Komga {baseUrl}"; return $"Komga {baseUrl}";
} }
protected override void UpdateLibraryInternal() public override void UpdateLibrary()
{ {
Log("Updating libraries."); Log("Updating libraries.");
foreach (KomgaLibrary lib in GetLibraries()) foreach (KomgaLibrary lib in GetLibraries())

View File

@ -17,9 +17,6 @@ public abstract class LibraryConnector : GlobalBase
public string baseUrl { get; } public string baseUrl { get; }
// ReSharper disable once MemberCanBeProtected.Global // ReSharper disable once MemberCanBeProtected.Global
public string auth { get; } //Base64 encoded, if you use your password everywhere, you have problems public string auth { get; } //Base64 encoded, if you use your password everywhere, you have problems
private DateTime? _updateLibraryRequested = null;
private readonly Thread? _libraryBufferThread = null;
private const int NoChangeTimeout = 2, BiggestInterval = 20;
protected LibraryConnector(GlobalBase clone, string baseUrl, string auth, LibraryType libraryType) : base(clone) protected LibraryConnector(GlobalBase clone, string baseUrl, string auth, LibraryType libraryType) : base(clone)
{ {
@ -31,47 +28,8 @@ public abstract class LibraryConnector : GlobalBase
this.baseUrl = baseUrlRex.Match(baseUrl).Value; this.baseUrl = baseUrlRex.Match(baseUrl).Value;
this.auth = auth; this.auth = auth;
this.libraryType = libraryType; this.libraryType = libraryType;
if (TrangaSettings.bufferLibraryUpdates)
{
_libraryBufferThread = new(CheckLibraryBuffer);
_libraryBufferThread.Start();
}
} }
public abstract void UpdateLibrary();
private void CheckLibraryBuffer()
{
while (true)
{
if (_updateLibraryRequested is not null && DateTime.Now.Subtract((DateTime)_updateLibraryRequested) > TimeSpan.FromMinutes(NoChangeTimeout)) //If no updates have been requested for NoChangeTimeout minutes, update library
{
UpdateLibraryInternal();
_updateLibraryRequested = null;
}
Thread.Sleep(100);
}
}
public void UpdateLibrary()
{
_updateLibraryRequested ??= DateTime.Now;
if (!TrangaSettings.bufferLibraryUpdates)
{
UpdateLibraryInternal();
return;
}else if (_updateLibraryRequested is not null &&
DateTime.Now.Subtract((DateTime)_updateLibraryRequested) > TimeSpan.FromMinutes(BiggestInterval)) //If the last update has been more than BiggestInterval minutes ago, update library
{
UpdateLibraryInternal();
_updateLibraryRequested = null;
}
else if(_updateLibraryRequested is not null)
{
Log($"Buffering Library Updates (Updates in latest {((DateTime)_updateLibraryRequested).Add(TimeSpan.FromMinutes(BiggestInterval)).Subtract(DateTime.Now)} or {((DateTime)_updateLibraryRequested).Add(TimeSpan.FromMinutes(NoChangeTimeout)).Subtract(DateTime.Now)})");
}
}
protected abstract void UpdateLibraryInternal();
internal abstract bool Test(); internal abstract bool Test();
protected static class NetClient protected static class NetClient

View File

@ -24,7 +24,7 @@ public class Gotify : NotificationConnector
return $"Gotify {endpoint}"; return $"Gotify {endpoint}";
} }
protected override void SendNotificationInternal(string title, string notificationText) public override void SendNotification(string title, string notificationText)
{ {
Log($"Sending notification: {title} - {notificationText}"); Log($"Sending notification: {title} - {notificationText}");
MessageData message = new(title, notificationText); MessageData message = new(title, notificationText);

View File

@ -20,7 +20,7 @@ public class LunaSea : NotificationConnector
return $"LunaSea {id}"; return $"LunaSea {id}";
} }
protected override void SendNotificationInternal(string title, string notificationText) public override void SendNotification(string title, string notificationText)
{ {
Log($"Sending notification: {title} - {notificationText}"); Log($"Sending notification: {title} - {notificationText}");
MessageData message = new(title, notificationText); MessageData message = new(title, notificationText);

View File

@ -3,70 +3,14 @@
public abstract class NotificationConnector : GlobalBase public abstract class NotificationConnector : GlobalBase
{ {
public readonly NotificationConnectorType notificationConnectorType; public readonly NotificationConnectorType notificationConnectorType;
private DateTime? _notificationRequested = null;
private readonly Thread? _notificationBufferThread = null;
private const int NoChangeTimeout = 3, BiggestInterval = 30;
private List<KeyValuePair<string, string>> _notifications = new();
protected NotificationConnector(GlobalBase clone, NotificationConnectorType notificationConnectorType) : base(clone) protected NotificationConnector(GlobalBase clone, NotificationConnectorType notificationConnectorType) : base(clone)
{ {
Log($"Creating notificationConnector {Enum.GetName(notificationConnectorType)}"); Log($"Creating notificationConnector {Enum.GetName(notificationConnectorType)}");
this.notificationConnectorType = notificationConnectorType; this.notificationConnectorType = notificationConnectorType;
if (TrangaSettings.bufferLibraryUpdates)
{
_notificationBufferThread = new(CheckNotificationBuffer);
_notificationBufferThread.Start();
}
}
private void CheckNotificationBuffer()
{
while (true)
{
if (_notificationRequested is not null && DateTime.Now.Subtract((DateTime)_notificationRequested) > TimeSpan.FromMinutes(NoChangeTimeout)) //If no updates have been requested for NoChangeTimeout minutes, update library
{
string[] uniqueTitles = _notifications.DistinctBy(n => n.Key).Select(n => n.Key).ToArray();
Log($"Notification Buffer sending! Notifications: {string.Join(", ", uniqueTitles)}");
foreach (string ut in uniqueTitles)
{
string[] texts = _notifications.Where(n => n.Key == ut).Select(n => n.Value).ToArray();
SendNotificationInternal($"{ut} ({texts.Length})", string.Join('\n', texts));
}
_notificationRequested = null;
}
Thread.Sleep(100);
}
} }
public enum NotificationConnectorType : byte { Gotify = 0, LunaSea = 1, Ntfy = 2 } public enum NotificationConnectorType : byte { Gotify = 0, LunaSea = 1, Ntfy = 2 }
public void SendNotification(string title, string notificationText, bool buffer = false) public abstract void SendNotification(string title, string notificationText);
{
_notificationRequested ??= DateTime.Now;
if (!TrangaSettings.bufferNotifications || !buffer)
{
SendNotificationInternal(title, notificationText);
return;
}
_notifications.Add(new(title, notificationText));
if (_notificationRequested is not null &&
DateTime.Now.Subtract((DateTime)_notificationRequested) > TimeSpan.FromMinutes(BiggestInterval)) //If the last update has been more than BiggestInterval minutes ago, update library
{
string[] uniqueTitles = _notifications.DistinctBy(n => n.Key).Select(n => n.Key).ToArray();
foreach (string ut in uniqueTitles)
{
string[] texts = _notifications.Where(n => n.Key == ut).Select(n => n.Value).ToArray();
SendNotificationInternal(ut, string.Join('\n', texts));
}
_notificationRequested = null;
}
else if(_notificationRequested is not null)
{
Log($"Buffering Notifications (Updates in latest {((DateTime)_notificationRequested).Add(TimeSpan.FromMinutes(BiggestInterval)).Subtract(DateTime.Now)} or {((DateTime)_notificationRequested).Add(TimeSpan.FromMinutes(NoChangeTimeout)).Subtract(DateTime.Now)})");
}
}
protected abstract void SendNotificationInternal(string title, string notificationText);
} }

View File

@ -54,7 +54,7 @@ public class Ntfy : NotificationConnector
return $"Ntfy {endpoint} {topic}"; return $"Ntfy {endpoint} {topic}";
} }
protected override void SendNotificationInternal(string title, string notificationText) public override void SendNotification(string title, string notificationText)
{ {
Log($"Sending notification: {title} - {notificationText}"); Log($"Sending notification: {title} - {notificationText}");
MessageData message = new(title, topic, notificationText); MessageData message = new(title, topic, notificationText);

View File

@ -33,7 +33,6 @@ public partial class Tranga : GlobalBase
this._server = new Server.Server(this); this._server = new Server.Server(this);
string[] emojis = { "(•‿•)", "(づ \u25d5‿\u25d5 )づ", "( \u02d8\u25bd\u02d8)っ\u2668", "=\uff3e\u25cf \u22cf \u25cf\uff3e=", "(ΦωΦ)", "(\u272a\u3268\u272a)", "( ノ・o・ )ノ", "(〜^\u2207^ )〜", "~(\u2267ω\u2266)~","૮ \u00b4• ﻌ \u00b4• ა", "(\u02c3ᆺ\u02c2)", "(=\ud83d\udf66 \u0f1d \ud83d\udf66=)"}; string[] emojis = { "(•‿•)", "(づ \u25d5‿\u25d5 )づ", "( \u02d8\u25bd\u02d8)っ\u2668", "=\uff3e\u25cf \u22cf \u25cf\uff3e=", "(ΦωΦ)", "(\u272a\u3268\u272a)", "( ノ・o・ )ノ", "(〜^\u2207^ )〜", "~(\u2267ω\u2266)~","૮ \u00b4• ﻌ \u00b4• ა", "(\u02c3ᆺ\u02c2)", "(=\ud83d\udf66 \u0f1d \ud83d\udf66=)"};
SendNotifications("Tranga Started", emojis[Random.Shared.Next(0,emojis.Length-1)]); SendNotifications("Tranga Started", emojis[Random.Shared.Next(0,emojis.Length-1)]);
Log(TrangaSettings.AsJObject().ToString());
} }
public MangaConnector? GetConnector(string name) public MangaConnector? GetConnector(string name)

View File

@ -15,8 +15,6 @@ public static class TrangaSettings
public static string workingDirectory { get; private set; } = Path.Join(RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/usr/share" : Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "tranga-api"); public static string workingDirectory { get; private set; } = Path.Join(RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/usr/share" : Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "tranga-api");
public static int apiPortNumber { get; private set; } = 6531; public static int apiPortNumber { get; private set; } = 6531;
public static string userAgent { get; private set; } = DefaultUserAgent; public static string userAgent { get; private set; } = DefaultUserAgent;
public static bool bufferLibraryUpdates { get; private set; } = false;
public static bool bufferNotifications { get; private set; } = false;
[JsonIgnore] public static string settingsFilePath => Path.Join(workingDirectory, "settings.json"); [JsonIgnore] public static string settingsFilePath => Path.Join(workingDirectory, "settings.json");
[JsonIgnore] public static string libraryConnectorsFilePath => Path.Join(workingDirectory, "libraryConnectors.json"); [JsonIgnore] public static string libraryConnectorsFilePath => Path.Join(workingDirectory, "libraryConnectors.json");
[JsonIgnore] public static string notificationConnectorsFilePath => Path.Join(workingDirectory, "notificationConnectors.json"); [JsonIgnore] public static string notificationConnectorsFilePath => Path.Join(workingDirectory, "notificationConnectors.json");
@ -49,17 +47,15 @@ public static class TrangaSettings
ExportSettings(); ExportSettings();
} }
public static void CreateOrUpdate(string? downloadDirectory = null, string? pWorkingDirectory = null, int? pApiPortNumber = null, string? pUserAgent = null, bool? pAprilFoolsMode = null, bool? pBufferLibraryUpdates = null, bool? pBufferNotifications = null) public static void CreateOrUpdate(string? downloadDirectory = null, string? pWorkingDirectory = null, int? pApiPortNumber = null, string? pUserAgent = null, bool? pAprilFoolsMode = null)
{ {
if(pWorkingDirectory is null && File.Exists(settingsFilePath)) if(pWorkingDirectory is null && File.Exists(settingsFilePath))
LoadFromWorkingDirectory(workingDirectory); LoadFromWorkingDirectory(workingDirectory);
downloadLocation = downloadDirectory ?? downloadLocation; TrangaSettings.downloadLocation = downloadDirectory ?? TrangaSettings.downloadLocation;
workingDirectory = pWorkingDirectory ?? workingDirectory; TrangaSettings.workingDirectory = pWorkingDirectory ?? TrangaSettings.workingDirectory;
apiPortNumber = pApiPortNumber ?? apiPortNumber; TrangaSettings.apiPortNumber = pApiPortNumber ?? TrangaSettings.apiPortNumber;
userAgent = pUserAgent ?? userAgent; TrangaSettings.userAgent = pUserAgent ?? TrangaSettings.userAgent;
aprilFoolsMode = pAprilFoolsMode ?? aprilFoolsMode; TrangaSettings.aprilFoolsMode = pAprilFoolsMode ?? TrangaSettings.aprilFoolsMode;
bufferLibraryUpdates = pBufferLibraryUpdates ?? bufferLibraryUpdates;
bufferNotifications = pBufferNotifications ?? bufferNotifications;
Directory.CreateDirectory(downloadLocation); Directory.CreateDirectory(downloadLocation);
Directory.CreateDirectory(workingDirectory); Directory.CreateDirectory(workingDirectory);
ExportSettings(); ExportSettings();
@ -95,7 +91,7 @@ public static class TrangaSettings
public static void UpdateAprilFoolsMode(bool enabled) public static void UpdateAprilFoolsMode(bool enabled)
{ {
aprilFoolsMode = enabled; TrangaSettings.aprilFoolsMode = enabled;
ExportSettings(); ExportSettings();
} }
@ -150,19 +146,19 @@ public static class TrangaSettings
public static void UpdateUserAgent(string? customUserAgent) public static void UpdateUserAgent(string? customUserAgent)
{ {
userAgent = customUserAgent ?? DefaultUserAgent; TrangaSettings.userAgent = customUserAgent ?? DefaultUserAgent;
ExportSettings(); ExportSettings();
} }
public static void UpdateRateLimit(RequestType requestType, int newLimit) public static void UpdateRateLimit(RequestType requestType, int newLimit)
{ {
requestLimits[requestType] = newLimit; TrangaSettings.requestLimits[requestType] = newLimit;
ExportSettings(); ExportSettings();
} }
public static void ResetRateLimits() public static void ResetRateLimits()
{ {
requestLimits = DefaultRequestLimits; TrangaSettings.requestLimits = DefaultRequestLimits;
ExportSettings(); ExportSettings();
} }
@ -181,15 +177,13 @@ public static class TrangaSettings
public static JObject AsJObject() public static JObject AsJObject()
{ {
JObject jobj = new JObject(); JObject jobj = new JObject();
jobj.Add("downloadLocation", JToken.FromObject(downloadLocation)); jobj.Add("downloadLocation", JToken.FromObject(TrangaSettings.downloadLocation));
jobj.Add("workingDirectory", JToken.FromObject(workingDirectory)); jobj.Add("workingDirectory", JToken.FromObject(TrangaSettings.workingDirectory));
jobj.Add("apiPortNumber", JToken.FromObject(apiPortNumber)); jobj.Add("apiPortNumber", JToken.FromObject(TrangaSettings.apiPortNumber));
jobj.Add("userAgent", JToken.FromObject(userAgent)); jobj.Add("userAgent", JToken.FromObject(TrangaSettings.userAgent));
jobj.Add("aprilFoolsMode", JToken.FromObject(aprilFoolsMode)); jobj.Add("aprilFoolsMode", JToken.FromObject(TrangaSettings.aprilFoolsMode));
jobj.Add("version", JToken.FromObject(version)); jobj.Add("version", JToken.FromObject(TrangaSettings.version));
jobj.Add("requestLimits", JToken.FromObject(requestLimits)); jobj.Add("requestLimits", JToken.FromObject(TrangaSettings.requestLimits));
jobj.Add("bufferLibraryUpdates", JToken.FromObject(bufferLibraryUpdates));
jobj.Add("bufferNotifications", JToken.FromObject(bufferNotifications));
return jobj; return jobj;
} }
@ -199,20 +193,16 @@ public static class TrangaSettings
{ {
JObject jobj = JObject.Parse(serialized); JObject jobj = JObject.Parse(serialized);
if (jobj.TryGetValue("downloadLocation", out JToken? dl)) if (jobj.TryGetValue("downloadLocation", out JToken? dl))
downloadLocation = dl.Value<string>()!; TrangaSettings.downloadLocation = dl.Value<string>()!;
if (jobj.TryGetValue("workingDirectory", out JToken? wd)) if (jobj.TryGetValue("workingDirectory", out JToken? wd))
workingDirectory = wd.Value<string>()!; TrangaSettings.workingDirectory = wd.Value<string>()!;
if (jobj.TryGetValue("apiPortNumber", out JToken? apn)) if (jobj.TryGetValue("apiPortNumber", out JToken? apn))
apiPortNumber = apn.Value<int>(); TrangaSettings.apiPortNumber = apn.Value<int>();
if (jobj.TryGetValue("userAgent", out JToken? ua)) if (jobj.TryGetValue("userAgent", out JToken? ua))
userAgent = ua.Value<string>()!; TrangaSettings.userAgent = ua.Value<string>()!;
if (jobj.TryGetValue("aprilFoolsMode", out JToken? afm)) if (jobj.TryGetValue("aprilFoolsMode", out JToken? afm))
aprilFoolsMode = afm.Value<bool>()!; TrangaSettings.aprilFoolsMode = afm.Value<bool>()!;
if (jobj.TryGetValue("requestLimits", out JToken? rl)) if (jobj.TryGetValue("requestLimits", out JToken? rl))
requestLimits = rl.ToObject<Dictionary<RequestType, int>>()!; TrangaSettings.requestLimits = rl.ToObject<Dictionary<RequestType, int>>()!;
if (jobj.TryGetValue("bufferLibraryUpdates", out JToken? blu))
bufferLibraryUpdates = blu.Value<bool>()!;
if (jobj.TryGetValue("bufferNotifications", out JToken? bn))
bufferNotifications = bn.Value<bool>()!;
} }
} }