Compare commits

...

5 Commits

Author SHA1 Message Date
715244ff1b Mangasee more logging 2023-07-16 18:15:28 +02:00
2333cd9095 Mangasee more bad words 2023-07-16 18:15:11 +02:00
c8225db4fe #30 #31 2023-07-16 17:47:00 +02:00
6741ca096b Startup Message 2023-07-16 17:38:42 +02:00
a897a7b3a2 Better Logger.
Includes a formatted Console-Log
2023-07-16 17:33:15 +02:00
11 changed files with 80 additions and 79 deletions

View File

@ -17,6 +17,10 @@ public class Program
Directory.CreateDirectory(logsFolderPath); Directory.CreateDirectory(logsFolderPath);
Logger logger = new(new[] { Logger.LoggerType.FileLogger, Logger.LoggerType.ConsoleLogger }, Console.Out, Console.Out.Encoding, logFilePath); Logger logger = new(new[] { Logger.LoggerType.FileLogger, Logger.LoggerType.ConsoleLogger }, Console.Out, Console.Out.Encoding, logFilePath);
logger.WriteLine("Tranga",value: "\n"+
"-------------------------------------------\n"+
" Starting Tranga-API\n"+
"-------------------------------------------");
logger.WriteLine("Tranga", "Loading settings."); logger.WriteLine("Tranga", "Loading settings.");
TrangaSettings settings; TrangaSettings settings;

View File

@ -1,5 +1,4 @@
using System.Text; using System.Text;
using System.Text.Json.Serialization;
namespace Logging; namespace Logging;
@ -8,7 +7,7 @@ public class FileLogger : LoggerBase
private string logFilePath { get; } private string logFilePath { get; }
private const int MaxNumberOfLogFiles = 5; private const int MaxNumberOfLogFiles = 5;
public FileLogger(string logFilePath, TextWriter? stdOut, Encoding? encoding = null) : base (stdOut, encoding) public FileLogger(string logFilePath, Encoding? encoding = null) : base (encoding)
{ {
this.logFilePath = logFilePath; this.logFilePath = logFilePath;
@ -22,11 +21,11 @@ public class FileLogger : LoggerBase
{ {
try try
{ {
File.AppendAllText(logFilePath, logMessage.ToString()); File.AppendAllText(logFilePath, logMessage.formattedMessage);
} }
catch (Exception e) catch (Exception)
{ {
stdOut?.WriteLine(e); // ignored
} }
} }
} }

View File

@ -4,14 +4,14 @@ namespace Logging;
public class FormattedConsoleLogger : LoggerBase public class FormattedConsoleLogger : LoggerBase
{ {
private readonly TextWriter _stdOut;
public FormattedConsoleLogger(TextWriter? stdOut, Encoding? encoding = null) : base(stdOut, encoding) public FormattedConsoleLogger(TextWriter stdOut, Encoding? encoding = null) : base(encoding)
{ {
this._stdOut = stdOut;
} }
protected override void Write(LogMessage message) protected override void Write(LogMessage message)
{ {
//Nothing to do yet this._stdOut.Write(message.formattedMessage);
} }
} }

23
Logging/LogMessage.cs Normal file
View File

@ -0,0 +1,23 @@
namespace Logging;
public class LogMessage
{
public DateTime logTime { get; }
public string caller { get; }
public string value { get; }
public string formattedMessage => ToString();
public LogMessage(DateTime messageTime, string caller, string value)
{
this.logTime = messageTime;
this.caller = caller;
this.value = value;
}
public override string ToString()
{
string dateTimeString = $"{logTime.ToShortDateString()} {logTime.ToLongTimeString()}.{logTime.Millisecond,-3}";
string name = caller.Split(new char[] { '.', '+' }).Last();
return $"[{dateTimeString}] {name.Substring(0, name.Length >= 13 ? 13 : name.Length),13} | {value}";
}
}

View File

@ -1,5 +1,4 @@
using System.Net.Mime; using System.Text;
using System.Text;
namespace Logging; namespace Logging;
@ -12,24 +11,31 @@ public class Logger : TextWriter
ConsoleLogger ConsoleLogger
} }
private FileLogger? _fileLogger; private readonly FileLogger? _fileLogger;
private FormattedConsoleLogger? _formattedConsoleLogger; private readonly FormattedConsoleLogger? _formattedConsoleLogger;
private MemoryLogger _memoryLogger; private readonly MemoryLogger _memoryLogger;
private TextWriter? stdOut;
public Logger(LoggerType[] enabledLoggers, TextWriter? stdOut, Encoding? encoding, string? logFilePath) public Logger(LoggerType[] enabledLoggers, TextWriter? stdOut, Encoding? encoding, string? logFilePath)
{ {
this.Encoding = encoding ?? Encoding.ASCII; this.Encoding = encoding ?? Encoding.ASCII;
this.stdOut = stdOut ?? null;
if (enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is not null) if (enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is not null)
_fileLogger = new FileLogger(logFilePath, null, encoding); _fileLogger = new FileLogger(logFilePath, encoding);
else else
{ {
_fileLogger = null; _fileLogger = null;
throw new ArgumentException($"logFilePath can not be null for LoggerType {LoggerType.FileLogger}"); throw new ArgumentException($"logFilePath can not be null for LoggerType {LoggerType.FileLogger}");
} }
_formattedConsoleLogger = enabledLoggers.Contains(LoggerType.ConsoleLogger) ? new FormattedConsoleLogger(null, encoding) : null;
_memoryLogger = new MemoryLogger(null, encoding); if (enabledLoggers.Contains(LoggerType.ConsoleLogger) && stdOut is not null)
{
_formattedConsoleLogger = new FormattedConsoleLogger(stdOut, encoding);
}
else
{
_formattedConsoleLogger = null;
throw new ArgumentException($"stdOut can not be null for LoggerType {LoggerType.ConsoleLogger}");
}
_memoryLogger = new MemoryLogger(encoding);
} }
public void WriteLine(string caller, string? value) public void WriteLine(string caller, string? value)
@ -46,9 +52,7 @@ public class Logger : TextWriter
_fileLogger?.Write(caller, value); _fileLogger?.Write(caller, value);
_formattedConsoleLogger?.Write(caller, value); _formattedConsoleLogger?.Write(caller, value);
_memoryLogger.Write(caller, value); _memoryLogger.Write(caller, value);
stdOut?.Write(value);
} }
public string[] Tail(uint? lines) public string[] Tail(uint? lines)

View File

@ -5,21 +5,10 @@ namespace Logging;
public abstract class LoggerBase : TextWriter public abstract class LoggerBase : TextWriter
{ {
public override Encoding Encoding { get; } public override Encoding Encoding { get; }
protected TextWriter? stdOut { get; }
public LoggerBase(TextWriter? stdOut, Encoding? encoding = null) public LoggerBase(Encoding? encoding = null)
{ {
this.Encoding = encoding ?? Encoding.ASCII; this.Encoding = encoding ?? Encoding.ASCII;
this.stdOut = stdOut;
}
public void WriteLine(string caller, string? value)
{
value = value is null ? Environment.NewLine : string.Join(value, Environment.NewLine);
LogMessage message = new LogMessage(DateTime.Now, caller, value);
Write(message);
} }
public void Write(string caller, string? value) public void Write(string caller, string? value)
@ -27,32 +16,10 @@ public abstract class LoggerBase : TextWriter
if (value is null) if (value is null)
return; return;
LogMessage message = new LogMessage(DateTime.Now, caller, value); LogMessage message = new (DateTime.Now, caller, value);
stdOut?.Write(message.ToString());
Write(message); Write(message);
} }
protected abstract void Write(LogMessage message); protected abstract void Write(LogMessage message);
public class LogMessage
{
public DateTime logTime { get; }
public string caller { get; }
public string value { get; }
public LogMessage(DateTime now, string caller, string value)
{
this.logTime = now;
this.caller = caller;
this.value = value;
}
public override string ToString()
{
string dateTimeString = $"{logTime.ToShortDateString()} {logTime.ToLongTimeString()}";
return $"[{dateTimeString}] {caller.Split(new char[]{'.','+'}).Last(),15} | {value}";
}
}
} }

View File

@ -7,7 +7,7 @@ public class MemoryLogger : LoggerBase
private readonly SortedList<DateTime, LogMessage> _logMessages = new(); private readonly SortedList<DateTime, LogMessage> _logMessages = new();
private int _lastLogMessageIndex = 0; private int _lastLogMessageIndex = 0;
public MemoryLogger(TextWriter? stdOut, Encoding? encoding = null) : base(stdOut, encoding) public MemoryLogger(Encoding? encoding = null) : base(encoding)
{ {
} }

View File

@ -1,4 +1,5 @@
using System.IO.Compression; using System.Globalization;
using System.IO.Compression;
using System.Net; using System.Net;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -66,8 +67,11 @@ public abstract class Connector
{ {
Chapter[] newChapters = this.GetChapters(publication, language); Chapter[] newChapters = this.GetChapters(publication, language);
collection.Add(publication); collection.Add(publication);
NumberFormatInfo decimalPoint = new (){ NumberDecimalSeparator = "." };
logger?.WriteLine(this.GetType().ToString(), "Checking for duplicates"); logger?.WriteLine(this.GetType().ToString(), "Checking for duplicates");
List<Chapter> newChaptersList = newChapters.Where(nChapter => !nChapter.CheckChapterIsDownloaded(settings.downloadLocation)).ToList(); List<Chapter> newChaptersList = newChapters.Where(nChapter =>
float.Parse(nChapter.chapterNumber, decimalPoint) > publication.ignoreChaptersBelow &&
!nChapter.CheckChapterIsDownloaded(settings.downloadLocation)).ToList();
logger?.WriteLine(this.GetType().ToString(), $"{newChaptersList.Count} new chapters."); logger?.WriteLine(this.GetType().ToString(), $"{newChaptersList.Count} new chapters.");
return newChaptersList; return newChaptersList;

View File

@ -98,18 +98,23 @@ public class Mangasee : Connector
queryFiltered = queryFiltered.Where(item => item.Value >= publicationTitle.Split(' ').Length - 1) queryFiltered = queryFiltered.Where(item => item.Value >= publicationTitle.Split(' ').Length - 1)
.ToDictionary(item => item.Key, item => item.Value); .ToDictionary(item => item.Key, item => item.Value);
logger?.WriteLine(this.GetType().ToString(), $"Got {queryFiltered.Count} Publications (title={publicationTitle})");
HashSet<Publication> ret = new(); HashSet<Publication> ret = new();
List<SearchResultItem> orderedFiltered = List<SearchResultItem> orderedFiltered =
queryFiltered.OrderBy(item => item.Value).ToDictionary(item => item.Key, item => item.Value).Keys.ToList(); queryFiltered.OrderBy(item => item.Value).ToDictionary(item => item.Key, item => item.Value).Keys.ToList();
uint index = 1;
foreach (SearchResultItem orderedItem in orderedFiltered) foreach (SearchResultItem orderedItem in orderedFiltered)
{ {
DownloadClient.RequestResult requestResult = DownloadClient.RequestResult requestResult =
downloadClient.MakeRequest($"https://mangasee123.com/manga/{orderedItem.i}", (byte)1); downloadClient.MakeRequest($"https://mangasee123.com/manga/{orderedItem.i}", (byte)1);
if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) if ((int)requestResult.statusCode >= 200 || (int)requestResult.statusCode < 300)
return Array.Empty<Publication>(); {
ret.Add(ParseSinglePublicationFromHtml(requestResult.result, orderedItem.s, orderedItem.i, orderedItem.a)); logger?.WriteLine(this.GetType().ToString(), $"Retrieving Publication info: {orderedItem.s} {index++}/{orderedFiltered.Count}");
ret.Add(ParseSinglePublicationFromHtml(requestResult.result, orderedItem.s, orderedItem.i, orderedItem.a));
}
} }
return ret.ToArray(); return ret.ToArray();
} }
@ -118,9 +123,8 @@ public class Mangasee : Connector
private Publication ParseSinglePublicationFromHtml(Stream html, string sortName, string publicationId, string[] a) private Publication ParseSinglePublicationFromHtml(Stream html, string sortName, string publicationId, string[] a)
{ {
StreamReader reader = new (html); StreamReader reader = new (html);
string htmlString = reader.ReadToEnd();
HtmlDocument document = new (); HtmlDocument document = new ();
document.LoadHtml(htmlString); document.LoadHtml(reader.ReadToEnd());
string originalLanguage = "", status = ""; string originalLanguage = "", status = "";
Dictionary<string, string> altTitles = new(), links = new(); Dictionary<string, string> altTitles = new(), links = new();
@ -184,7 +188,7 @@ public class Mangasee : Connector
{ {
int ret = 0; int ret = 0;
Regex cleanRex = new("[A-z0-9]*"); Regex cleanRex = new("[A-z0-9]*");
string[] badWords = { "a", "so", "as", "and", "the", "of", "that", "in", "is", "for" }; string[] badWords = { "a", "an", "no", "ni", "so", "as", "and", "the", "of", "that", "in", "is", "for" };
string[] titleTerms = title.Split(new[] { ' ', '-' }).Where(str => !badWords.Contains(str)).ToArray(); string[] titleTerms = title.Split(new[] { ' ', '-' }).Where(str => !badWords.Contains(str)).ToArray();

View File

@ -26,22 +26,11 @@ public readonly struct Publication
public string folderName { get; } public string folderName { get; }
public string publicationId { get; } public string publicationId { get; }
public string internalId { get; } public string internalId { get; }
public uint ignoreChaptersBelow { get; }
private static readonly Regex LegalCharacters = new Regex(@"[A-Z]*[a-z]*[0-9]* *\.*-*,*'*\'*\)*\(*~*!*"); private static readonly Regex LegalCharacters = new Regex(@"[A-Z]*[a-z]*[0-9]* *\.*-*,*'*\'*\)*\(*~*!*");
[JsonConstructor] //Legacy public Publication(string sortName, List<string> authors, string? description, Dictionary<string,string> altTitles, string[] tags, string? posterUrl, string? coverFileNameInCache, Dictionary<string,string>? links, int? year, string? originalLanguage, string status, string publicationId, string? folderName = null, uint ignoreChaptersBelow = 0)
public Publication(string sortName, string? author, string? description, Dictionary<string, string> altTitles,
string[] tags, string? posterUrl, string? coverFileNameInCache, Dictionary<string, string>? links, int? year,
string? originalLanguage, string status, string publicationId)
{
List<string> pAuthors = new();
if(author is not null)
pAuthors.Add(author);
this = new Publication(sortName, pAuthors, description, altTitles, tags, posterUrl,
coverFileNameInCache, links, year, originalLanguage, status, publicationId);
}
public Publication(string sortName, List<string> authors, string? description, Dictionary<string,string> altTitles, string[] tags, string? posterUrl, string? coverFileNameInCache, Dictionary<string,string>? links, int? year, string? originalLanguage, string status, string publicationId)
{ {
this.sortName = sortName; this.sortName = sortName;
this.authors = authors; this.authors = authors;
@ -55,11 +44,12 @@ public readonly struct Publication
this.originalLanguage = originalLanguage; this.originalLanguage = originalLanguage;
this.status = status; this.status = status;
this.publicationId = publicationId; this.publicationId = publicationId;
this.folderName = string.Concat(LegalCharacters.Matches(sortName)); this.folderName = folderName ?? string.Concat(LegalCharacters.Matches(sortName));
while (this.folderName.EndsWith('.')) while (this.folderName.EndsWith('.'))
this.folderName = this.folderName.Substring(0, this.folderName.Length - 1); this.folderName = this.folderName.Substring(0, this.folderName.Length - 1);
string onlyLowerLetters = string.Concat(this.sortName.ToLower().Where(Char.IsLetter)); string onlyLowerLetters = string.Concat(this.sortName.ToLower().Where(Char.IsLetter));
this.internalId = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{onlyLowerLetters}{this.year}")); this.internalId = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{onlyLowerLetters}{this.year}"));
this.ignoreChaptersBelow = ignoreChaptersBelow;
} }
public string CreatePublicationFolder(string downloadDirectory) public string CreatePublicationFolder(string downloadDirectory)

View File

@ -22,6 +22,12 @@ public class TaskManager
public TaskManager(TrangaSettings settings, Logger? logger = null) public TaskManager(TrangaSettings settings, Logger? logger = null)
{ {
this.logger = logger; this.logger = logger;
logger?.WriteLine("Tranga", value: "\n"+
@"-----------------------------------------------------------------"+"\n"+
@" |¯¯¯¯¯¯|°|¯¯¯¯¯¯\ /¯¯¯¯¯¯| |¯¯¯\|¯¯¯| /¯¯¯¯¯¯\' /¯¯¯¯¯¯| "+"\n"+
@" | | | x <|' / ! | | '| | (/¯¯¯\° / ! | "+ "\n"+
@" ¯|__|¯ |__|\\__\\ /___/¯|_'| |___|\\__| \\_____/' /___/¯|_'| "+ "\n"+
@"-----------------------------------------------------------------");
this._connectors = new Connector[] this._connectors = new Connector[]
{ {
new MangaDex(settings, logger), new MangaDex(settings, logger),