Tranga-Website/Tranga/Publication.cs

130 lines
5.7 KiB
C#
Raw Normal View History

2023-06-01 18:28:58 +02:00
using System.Runtime.InteropServices;
using System.Text;
2023-05-25 16:47:24 +02:00
using System.Text.RegularExpressions;
2023-05-22 00:33:58 +02:00
using Newtonsoft.Json;
2023-06-01 18:28:58 +02:00
using static System.IO.UnixFileMode;
2023-05-18 20:26:47 +02:00
namespace Tranga;
2023-05-19 19:53:59 +02:00
/// <summary>
/// Contains information on a Publication (Manga)
/// </summary>
public readonly struct Publication
{
public string sortName { get; }
2023-06-10 14:05:23 +02:00
public List<string> authors { get; }
2023-05-21 23:27:28 +02:00
public Dictionary<string,string> altTitles { get; }
2023-05-20 01:06:12 +02:00
// ReSharper disable trice MemberCanBePrivate.Global, trust
2023-05-18 15:47:05 +02:00
public string? description { get; }
public string[] tags { get; }
2023-05-18 18:55:11 +02:00
public string? posterUrl { get; }
2023-05-25 14:23:33 +02:00
public string? coverFileNameInCache { get; }
2023-05-21 23:27:28 +02:00
public Dictionary<string,string> links { get; }
2023-05-18 15:47:05 +02:00
public int? year { get; }
public string? originalLanguage { get; }
public string status { get; }
2023-05-18 19:54:40 +02:00
public string folderName { get; }
public string publicationId { get; }
public string internalId { get; }
2023-07-16 17:47:00 +02:00
public uint ignoreChaptersBelow { get; }
2023-05-21 21:12:32 +02:00
2023-06-04 21:14:45 +02:00
private static readonly Regex LegalCharacters = new Regex(@"[A-Z]*[a-z]*[0-9]* *\.*-*,*'*\'*\)*\(*~*!*");
2023-05-25 16:47:24 +02:00
2023-07-16 17:47:00 +02:00
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)
{
this.sortName = sortName;
2023-06-10 14:05:23 +02:00
this.authors = authors;
this.description = description;
2023-05-18 15:47:05 +02:00
this.altTitles = altTitles;
this.tags = tags;
2023-05-25 14:23:33 +02:00
this.coverFileNameInCache = coverFileNameInCache;
this.posterUrl = posterUrl;
2023-05-21 21:12:32 +02:00
this.links = links ?? new Dictionary<string, string>();
2023-05-18 15:47:05 +02:00
this.year = year;
this.originalLanguage = originalLanguage;
this.status = status;
this.publicationId = publicationId;
2023-07-16 17:47:00 +02:00
this.folderName = folderName ?? string.Concat(LegalCharacters.Matches(sortName));
2023-05-25 22:22:57 +02:00
while (this.folderName.EndsWith('.'))
this.folderName = this.folderName.Substring(0, this.folderName.Length - 1);
2023-05-22 18:28:42 +02:00
string onlyLowerLetters = string.Concat(this.sortName.ToLower().Where(Char.IsLetter));
this.internalId = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{onlyLowerLetters}{this.year}"));
2023-07-16 17:47:00 +02:00
this.ignoreChaptersBelow = ignoreChaptersBelow;
}
public string CreatePublicationFolder(string downloadDirectory)
{
string publicationFolder = Path.Join(downloadDirectory, this.folderName);
if(!Directory.Exists(publicationFolder))
Directory.CreateDirectory(publicationFolder);
if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
File.SetUnixFileMode(publicationFolder, GroupRead | GroupWrite | GroupExecute | OtherRead | OtherWrite | OtherExecute | UserRead | UserWrite | UserExecute);
return publicationFolder;
}
public void SaveSeriesInfoJson(string downloadDirectory)
{
string publicationFolder = CreatePublicationFolder(downloadDirectory);
string seriesInfoPath = Path.Join(publicationFolder, "series.json");
if(!File.Exists(seriesInfoPath))
File.WriteAllText(seriesInfoPath,this.GetSeriesInfoJson());
2023-06-01 18:28:58 +02:00
if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
File.SetUnixFileMode(seriesInfoPath, GroupRead | GroupWrite | OtherRead | OtherWrite | UserRead | UserWrite);
}
2023-05-20 15:05:41 +02:00
2023-05-19 19:53:59 +02:00
/// <returns>Serialized JSON String for series.json</returns>
public string GetSeriesInfoJson()
2023-05-18 20:26:47 +02:00
{
SeriesInfo si = new (new Metadata(this.sortName, this.year.ToString() ?? string.Empty, this.status, this.description ?? ""));
return System.Text.Json.JsonSerializer.Serialize(si);
2023-05-18 20:26:47 +02:00
}
2023-05-19 19:53:59 +02:00
//Only for series.json
private struct SeriesInfo
2023-05-18 20:26:47 +02:00
{
2023-05-20 01:06:12 +02:00
// ReSharper disable once UnusedAutoPropertyAccessor.Local we need it, trust
2023-05-18 21:08:09 +02:00
[JsonRequired]public Metadata metadata { get; }
2023-05-18 20:26:47 +02:00
public SeriesInfo(Metadata metadata) => this.metadata = metadata;
}
2023-05-19 19:53:59 +02:00
2023-05-19 23:02:08 +02:00
//Only for series.json what an abomination, why are all the fields not-null????
2023-05-19 19:53:59 +02:00
private struct Metadata
2023-05-18 20:26:47 +02:00
{
2023-05-20 15:05:41 +02:00
// ReSharper disable UnusedAutoPropertyAccessor.Local we need them all, trust me
2023-05-19 22:59:16 +02:00
[JsonRequired] public string type { get; }
[JsonRequired] public string publisher { get; }
// ReSharper disable twice IdentifierTypo
[JsonRequired] public int comicid { get; }
[JsonRequired] public string booktype { get; }
2023-05-20 15:05:41 +02:00
// ReSharper disable InconsistentNaming This one property is capitalized. Why?
2023-05-19 22:59:16 +02:00
[JsonRequired] public string ComicImage { get; }
[JsonRequired] public int total_issues { get; }
[JsonRequired] public string publication_run { get; }
2023-05-18 21:08:09 +02:00
[JsonRequired]public string name { get; }
[JsonRequired]public string year { get; }
[JsonRequired]public string status { get; }
[JsonRequired]public string description_text { get; }
2023-05-18 20:26:47 +02:00
public Metadata(string name, string year, string status, string description_text)
{
this.name = name;
this.year = year;
2023-06-01 14:58:58 +02:00
if(status.ToLower() == "ongoing" || status.ToLower() == "hiatus")
2023-05-19 22:59:16 +02:00
this.status = "Continuing";
2023-06-05 19:46:22 +02:00
else if (status.ToLower() == "completed" || status.ToLower() == "cancelled" || status.ToLower() == "discontinued")
2023-05-19 22:59:16 +02:00
this.status = "Ended";
else
this.status = status;
2023-05-18 20:26:47 +02:00
this.description_text = description_text;
2023-05-19 22:59:16 +02:00
2023-05-20 15:05:41 +02:00
//kill it with fire, but otherwise Komga will not parse
2023-05-19 22:59:16 +02:00
type = "Manga";
publisher = "";
comicid = 0;
booktype = "";
ComicImage = "";
total_issues = 0;
publication_run = "";
2023-05-18 20:26:47 +02:00
}
}
}