using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using API.Entities; using API.Entities.Enums; using API.Helpers; using API.Services; using Kavita.Common.Extensions; using Nager.ArticleNumber; namespace API.Data.Metadata; #nullable enable /// /// A representation of a ComicInfo.xml file /// /// See reference of the loose spec here: https://anansi-project.github.io/docs/comicinfo/documentation public class ComicInfo { public string Summary { get; set; } = string.Empty; public string Title { get; set; } = string.Empty; public string Series { get; set; } = string.Empty; /// /// Localized Series name. Not standard. /// public string LocalizedSeries { get; set; } = string.Empty; public string SeriesSort { get; set; } = string.Empty; public string Number { get; set; } = string.Empty; /// /// The total number of items in the series. /// [System.ComponentModel.DefaultValueAttribute(0)] public int Count { get; set; } = 0; public string Volume { get; set; } = string.Empty; public string Notes { get; set; } = string.Empty; public string Genre { get; set; } = string.Empty; public int PageCount { get; set; } // ReSharper disable once InconsistentNaming /// /// IETF BCP 47 Code to represent the language of the content /// public string LanguageISO { get; set; } = string.Empty; // ReSharper disable once InconsistentNaming /// /// ISBN for the underlying document /// /// ComicInfo.xml will actually output a GTIN (Global Trade Item Number) and it is the responsibility of the Parser to extract the ISBN. EPub will return ISBN. public string Isbn { get; set; } = string.Empty; /// /// This is only for deserialization and used within . Use for the actual value. /// public string GTIN { get; set; } = string.Empty; /// /// This is the link to where the data was scraped from /// /// This can be comma-separated public string Web { get; set; } = string.Empty; [System.ComponentModel.DefaultValueAttribute(0)] public int Day { get; set; } = 0; [System.ComponentModel.DefaultValueAttribute(0)] public int Month { get; set; } = 0; [System.ComponentModel.DefaultValueAttribute(0)] public int Year { get; set; } = 0; /// /// Rating based on the content. Think PG-13, R for movies. See for valid types /// public string AgeRating { get; set; } = string.Empty; /// /// User's rating of the content /// public float UserRating { get; set; } /// /// Can contain multiple comma separated strings, each create a /// public string SeriesGroup { get; set; } = string.Empty; /// /// Can contain multiple comma separated numbers that match with StoryArcNumber /// public string StoryArc { get; set; } = string.Empty; /// /// Can contain multiple comma separated numbers that match with StoryArc /// public string StoryArcNumber { get; set; } = string.Empty; public string AlternateNumber { get; set; } = string.Empty; public string AlternateSeries { get; set; } = string.Empty; /// /// Not used /// [System.ComponentModel.DefaultValueAttribute(0)] public int AlternateCount { get; set; } = 0; /// /// This is Epub only: calibre:title_sort /// Represents the sort order for the title /// public string TitleSort { get; set; } = string.Empty; /// /// This comes from ComicInfo and is free form text. We use this to validate against a set of tags and mark a file as /// special. /// public string Format { get; set; } = string.Empty; /// /// The translator, can be comma separated. This is part of ComicInfo.xml draft v2.1 /// /// See https://github.com/anansi-project/comicinfo/issues/2 for information about this tag public string Translator { get; set; } = string.Empty; /// /// Misc tags. This is part of ComicInfo.xml draft v2.1 /// /// See https://github.com/anansi-project/comicinfo/issues/1 for information about this tag public string Tags { get; set; } = string.Empty; /// /// This is the Author. For Books, we map creator tag in OPF to this field. Comma separated if multiple. /// public string Writer { get; set; } = string.Empty; public string Penciller { get; set; } = string.Empty; public string Inker { get; set; } = string.Empty; public string Colorist { get; set; } = string.Empty; public string Letterer { get; set; } = string.Empty; public string CoverArtist { get; set; } = string.Empty; public string Editor { get; set; } = string.Empty; public string Publisher { get; set; } = string.Empty; public string Imprint { get; set; } = string.Empty; public string Characters { get; set; } = string.Empty; public string Teams { get; set; } = string.Empty; public string Locations { get; set; } = string.Empty; public IList GetPeopleForRole(PersonRole role) => role switch { PersonRole.Other => [], PersonRole.Writer => TagHelper.GetTagValues(Writer), PersonRole.Penciller => TagHelper.GetTagValues(Penciller), PersonRole.Inker => TagHelper.GetTagValues(Inker), PersonRole.Colorist => TagHelper.GetTagValues(Colorist), PersonRole.Letterer => TagHelper.GetTagValues(Letterer), PersonRole.CoverArtist => TagHelper.GetTagValues(CoverArtist), PersonRole.Editor => TagHelper.GetTagValues(Editor), PersonRole.Publisher => TagHelper.GetTagValues(Publisher), PersonRole.Character => TagHelper.GetTagValues(Characters), PersonRole.Translator => TagHelper.GetTagValues(Translator), PersonRole.Imprint => TagHelper.GetTagValues(Imprint), PersonRole.Team => TagHelper.GetTagValues(Teams), PersonRole.Location => TagHelper.GetTagValues(Locations), _ => throw new ArgumentOutOfRangeException(nameof(role), role, null) }; public static AgeRating ConvertAgeRatingToEnum(string value) { if (string.IsNullOrEmpty(value)) return Entities.Enums.AgeRating.Unknown; return Enum.GetValues() .SingleOrDefault(t => t.ToDescription().ToUpperInvariant().Equals(value.ToUpperInvariant()), Entities.Enums.AgeRating.Unknown); } public static void CleanComicInfo(ComicInfo? info) { if (info == null) return; info.Series = info.Series.Trim(); info.SeriesSort = info.SeriesSort.Trim(); info.LocalizedSeries = info.LocalizedSeries.Trim(); info.Writer = Services.Tasks.Scanner.Parser.Parser.CleanAuthor(info.Writer); info.Colorist = Services.Tasks.Scanner.Parser.Parser.CleanAuthor(info.Colorist); info.Editor = Services.Tasks.Scanner.Parser.Parser.CleanAuthor(info.Editor); info.Inker = Services.Tasks.Scanner.Parser.Parser.CleanAuthor(info.Inker); info.Letterer = Services.Tasks.Scanner.Parser.Parser.CleanAuthor(info.Letterer); info.Penciller = Services.Tasks.Scanner.Parser.Parser.CleanAuthor(info.Penciller); info.Publisher = Services.Tasks.Scanner.Parser.Parser.CleanAuthor(info.Publisher); info.Imprint = Services.Tasks.Scanner.Parser.Parser.CleanAuthor(info.Imprint); info.Characters = Services.Tasks.Scanner.Parser.Parser.CleanAuthor(info.Characters); info.Translator = Services.Tasks.Scanner.Parser.Parser.CleanAuthor(info.Translator); info.CoverArtist = Services.Tasks.Scanner.Parser.Parser.CleanAuthor(info.CoverArtist); info.Teams = Services.Tasks.Scanner.Parser.Parser.CleanAuthor(info.Teams); info.Locations = Services.Tasks.Scanner.Parser.Parser.CleanAuthor(info.Locations); // We need to convert GTIN to ISBN info.Isbn = ParseGtin(info.GTIN); if (!string.IsNullOrEmpty(info.Number)) { info.Number = info.Number.Trim().Replace(",", "."); // Corrective measure for non English OSes } if (!string.IsNullOrEmpty(info.Volume)) { info.Volume = info.Volume.Trim(); } } /// /// Uses both Volume and Number to make an educated guess as to what count refers to and it's highest number. /// /// public int CalculatedCount() { try { if (float.TryParse(Number, CultureInfo.InvariantCulture, out var chpCount) && chpCount > 0) { return (int) Math.Floor(chpCount); } if (float.TryParse(Volume, CultureInfo.InvariantCulture, out var volCount) && volCount > 0) { return (int) Math.Floor(volCount); } } catch (Exception) { return 0; } return 0; } /// /// For a given GTIN, attempts to parse out an ISBN and set the Isbn property. /// /// /// public static string ParseGtin(string? gtin) { if (string.IsNullOrEmpty(gtin)) return string.Empty; // This is likely a valid ISBN if (gtin[0] == '0') { var offset = gtin[1] == '-' ? 0 : 1; var potentialIsbn = gtin[offset..]; if (ArticleNumberHelper.IsValidIsbn13(potentialIsbn)) { return potentialIsbn; } } if (ArticleNumberHelper.IsValidIsbn10(gtin) || ArticleNumberHelper.IsValidIsbn13(gtin)) { return gtin; } return string.Empty; } }