2
0

Chapter as Comparable

This commit is contained in:
glax 2024-01-03 18:37:12 +01:00
parent 5df63b00c2
commit d986c808e3
9 changed files with 36 additions and 14 deletions

View File

@ -7,7 +7,7 @@ namespace Tranga;
/// Has to be Part of a publication /// Has to be Part of a publication
/// Includes the Chapter-Name, -VolumeNumber, -ChapterNumber, the location of the chapter on the internet and the saveName of the local file. /// Includes the Chapter-Name, -VolumeNumber, -ChapterNumber, the location of the chapter on the internet and the saveName of the local file.
/// </summary> /// </summary>
public readonly struct Chapter public readonly struct Chapter : IComparable
{ {
// ReSharper disable once MemberCanBePrivate.Global // ReSharper disable once MemberCanBePrivate.Global
public Manga parentManga { get; } public Manga parentManga { get; }
@ -41,6 +41,33 @@ public readonly struct Chapter
return $"Chapter {parentManga.sortName} {parentManga.internalId} {chapterNumber} {name}"; return $"Chapter {parentManga.sortName} {parentManga.internalId} {chapterNumber} {name}";
} }
public int CompareTo(object? obj)
{
if (obj is Chapter otherChapter)
{
if (float.TryParse(volumeNumber, GlobalBase.numberFormatDecimalPoint, out float volumeNumberFloat) &&
float.TryParse(chapterNumber, GlobalBase.numberFormatDecimalPoint, out float chapterNumberFloat) &&
float.TryParse(otherChapter.volumeNumber, GlobalBase.numberFormatDecimalPoint,
out float otherVolumeNumberFloat) &&
float.TryParse(otherChapter.chapterNumber, GlobalBase.numberFormatDecimalPoint,
out float otherChapterNumberFloat))
{
switch (volumeNumberFloat.CompareTo(otherVolumeNumberFloat))
{
case < 0:
return -1;
case > 0:
return 1;
default:
return chapterNumberFloat.CompareTo(otherChapterNumberFloat);
}
}
else throw new FormatException($"Value could not be parsed");
}
throw new ArgumentException($"{obj} can not be compared to {this}");
}
/// <summary> /// <summary>
/// Checks if a chapter-archive is already present /// Checks if a chapter-archive is already present
/// </summary> /// </summary>

View File

@ -136,7 +136,7 @@ public class Bato : MangaConnector
//Return Chapters ordered by Chapter-Number //Return Chapters ordered by Chapter-Number
List<Chapter> chapters = ParseChaptersFromHtml(manga, requestUrl); List<Chapter> chapters = ParseChaptersFromHtml(manga, requestUrl);
Log($"Got {chapters.Count} chapters. {manga}"); Log($"Got {chapters.Count} chapters. {manga}");
return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, numberFormatDecimalPoint)).ToArray(); return chapters.Order().ToArray();
} }
private List<Chapter> ParseChaptersFromHtml(Manga manga, string mangaUrl) private List<Chapter> ParseChaptersFromHtml(Manga manga, string mangaUrl)

View File

@ -70,7 +70,7 @@ public abstract class MangaConnector : GlobalBase
try try
{ {
Chapter latestChapterAvailable = Chapter latestChapterAvailable =
allChapters.MaxBy(chapter => Convert.ToSingle(chapter.chapterNumber, numberFormatDecimalPoint)); allChapters.Max();
manga.latestChapterAvailable = manga.latestChapterAvailable =
Convert.ToSingle(latestChapterAvailable.chapterNumber, numberFormatDecimalPoint); Convert.ToSingle(latestChapterAvailable.chapterNumber, numberFormatDecimalPoint);
} }

View File

@ -253,7 +253,7 @@ public class MangaDex : MangaConnector
//Return Chapters ordered by Chapter-Number //Return Chapters ordered by Chapter-Number
Log($"Got {chapters.Count} chapters. {manga}"); Log($"Got {chapters.Count} chapters. {manga}");
return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, numberFormatDecimalPoint)).ToArray(); return chapters.Order().ToArray();
} }
public override HttpStatusCode DownloadChapter(Chapter chapter, ProgressToken? progressToken = null) public override HttpStatusCode DownloadChapter(Chapter chapter, ProgressToken? progressToken = null)

View File

@ -165,7 +165,7 @@ public class MangaKatana : MangaConnector
//Return Chapters ordered by Chapter-Number //Return Chapters ordered by Chapter-Number
List<Chapter> chapters = ParseChaptersFromHtml(manga, requestUrl); List<Chapter> chapters = ParseChaptersFromHtml(manga, requestUrl);
Log($"Got {chapters.Count} chapters. {manga}"); Log($"Got {chapters.Count} chapters. {manga}");
return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, numberFormatDecimalPoint)).ToArray(); return chapters.Order().ToArray();
} }
private List<Chapter> ParseChaptersFromHtml(Manga manga, string mangaUrl) private List<Chapter> ParseChaptersFromHtml(Manga manga, string mangaUrl)

View File

@ -159,7 +159,7 @@ public class MangaLife : MangaConnector
} }
//Return Chapters ordered by Chapter-Number //Return Chapters ordered by Chapter-Number
Log($"Got {chapters.Count} chapters. {manga}"); Log($"Got {chapters.Count} chapters. {manga}");
return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, numberFormatDecimalPoint)).ToArray(); return chapters.Order().ToArray();
} }
public override HttpStatusCode DownloadChapter(Chapter chapter, ProgressToken? progressToken = null) public override HttpStatusCode DownloadChapter(Chapter chapter, ProgressToken? progressToken = null)

View File

@ -152,12 +152,7 @@ public class Manganato : MangaConnector
return Array.Empty<Chapter>(); return Array.Empty<Chapter>();
List<Chapter> chapters = ParseChaptersFromHtml(manga, requestResult.htmlDocument); List<Chapter> chapters = ParseChaptersFromHtml(manga, requestResult.htmlDocument);
Log($"Got {chapters.Count} chapters. {manga}"); Log($"Got {chapters.Count} chapters. {manga}");
return chapters.OrderBy(chapter => return chapters.Order().ToArray();
{
if (float.TryParse(chapter.chapterNumber, numberFormatDecimalPoint, out float chapterNumber))
return chapterNumber;
else return 0;
}).ToArray();
} }
private List<Chapter> ParseChaptersFromHtml(Manga manga, HtmlDocument document) private List<Chapter> ParseChaptersFromHtml(Manga manga, HtmlDocument document)

View File

@ -206,7 +206,7 @@ public class Mangasee : MangaConnector
//Return Chapters ordered by Chapter-Number //Return Chapters ordered by Chapter-Number
Log($"Got {chapters.Count} chapters. {manga}"); Log($"Got {chapters.Count} chapters. {manga}");
return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, numberFormatDecimalPoint)).ToArray(); return chapters.Order().ToArray();
} }
catch (HttpRequestException e) catch (HttpRequestException e)
{ {

View File

@ -141,7 +141,7 @@ public class Mangaworld: MangaConnector
return Array.Empty<Chapter>(); return Array.Empty<Chapter>();
List<Chapter> chapters = ParseChaptersFromHtml(manga, requestResult.htmlDocument); List<Chapter> chapters = ParseChaptersFromHtml(manga, requestResult.htmlDocument);
Log($"Got {chapters.Count} chapters. {manga}"); Log($"Got {chapters.Count} chapters. {manga}");
return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, numberFormatDecimalPoint)).ToArray(); return chapters.Order().ToArray();
} }
private List<Chapter> ParseChaptersFromHtml(Manga manga, HtmlDocument document) private List<Chapter> ParseChaptersFromHtml(Manga manga, HtmlDocument document)