Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit tests for ComicInfo and ComicBookInfo formats #70

Merged
merged 13 commits into from
Oct 18, 2023
11 changes: 11 additions & 0 deletions Jellyfin.Plugin.Bookshelf.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jellyfin.Plugin.Bookshelf", "Jellyfin.Plugin.Bookshelf\Jellyfin.Plugin.Bookshelf.csproj", "{8D744D83-5403-4BA4-8794-760AF69DAC06}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{6215AFB5-402D-4B86-BD96-E33FC37B44EF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jellyfin.Plugin.Bookshelf.Tests", "tests\Jellyfin.Plugin.Bookshelf.Tests\Jellyfin.Plugin.Bookshelf.Tests.csproj", "{FC333648-2AEE-4759-9088-35386909CB58}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -13,5 +17,12 @@ Global
{8D744D83-5403-4BA4-8794-760AF69DAC06}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D744D83-5403-4BA4-8794-760AF69DAC06}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8D744D83-5403-4BA4-8794-760AF69DAC06}.Release|Any CPU.Build.0 = Release|Any CPU
{FC333648-2AEE-4759-9088-35386909CB58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FC333648-2AEE-4759-9088-35386909CB58}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC333648-2AEE-4759-9088-35386909CB58}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC333648-2AEE-4759-9088-35386909CB58}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{FC333648-2AEE-4759-9088-35386909CB58} = {6215AFB5-402D-4B86-BD96-E33FC37B44EF}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -18,7 +17,7 @@ namespace Jellyfin.Plugin.Bookshelf.Providers.ComicBookInfo
/// <summary>
/// Comic book info provider.
/// </summary>
public class ComicBookInfoProvider : IComicFileProvider
public class ComicBookInfoProvider : IComicFileProvider, IComicBookInfoUtilities
{
private readonly ILogger<ComicBookInfoProvider> _logger;

Expand Down Expand Up @@ -113,27 +112,19 @@ private MetadataResult<Book> SaveMetadata(ComicBookInfoFormat comic)

if (comic.Metadata.Language is not null)
{
metadataResult.ResultLanguage = ReadCultureInfoAsThreeLetterIsoInto(comic.Metadata.Language);
metadataResult.ResultLanguage = ReadCultureInfoInto(comic.Metadata.Language);
}

if (comic.Metadata.Credits.Count > 0)
{
foreach (var person in comic.Metadata.Credits)
{
if (person.Person is null || person.Role is null)
{
continue;
}

var personInfo = new PersonInfo { Name = person.Person, Type = person.Role };
metadataResult.AddPerson(personInfo);
}
ReadPeopleMetadata(comic.Metadata, metadataResult);
}

return metadataResult;
}

private Book? ReadComicBookMetadata(ComicBookInfoMetadata comic)
/// <inheritdoc />
public Book? ReadComicBookMetadata(ComicBookInfoMetadata comic)
{
var book = new Book();
var hasFoundMetadata = false;
Expand Down Expand Up @@ -178,6 +169,41 @@ private MetadataResult<Book> SaveMetadata(ComicBookInfoFormat comic)
}
}

/// <inheritdoc />
public void ReadPeopleMetadata(ComicBookInfoMetadata comic, MetadataResult<Book> metadataResult)
{
foreach (var person in comic.Credits)
{
if (person.Person is null || person.Role is null)
{
continue;
}

if (person.Person.Contains(',', StringComparison.InvariantCultureIgnoreCase))
{
var name = person.Person.Split(',');
person.Person = name[1].Trim(' ') + " " + name[0].Trim(' ');
}

var personInfo = new PersonInfo { Name = person.Person, Type = person.Role };
metadataResult.AddPerson(personInfo);
}
}

/// <inheritdoc />
public string? ReadCultureInfoInto(string language)
{
try
{
return new CultureInfo(language).DisplayName;
carif marked this conversation as resolved.
Show resolved Hide resolved
}
catch (Exception)
{
// Ignored
return null;
}
carif marked this conversation as resolved.
Show resolved Hide resolved
}

private bool ReadStringInto(string? data, Action<string> commitResult)
{
if (!string.IsNullOrWhiteSpace(data))
Expand Down Expand Up @@ -205,19 +231,6 @@ private bool ReadStringInto(string? data, Action<string> commitResult)
}
}

private string? ReadCultureInfoAsThreeLetterIsoInto(string language)
{
try
{
return new CultureInfo(language).ThreeLetterISOLanguageName;
}
catch (Exception)
{
// Ignored
return null;
}
}

private FileSystemMetadata? GetComicBookFile(string path)
{
var fileInfo = _fileSystem.GetFileSystemInfo(path);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;

namespace Jellyfin.Plugin.Bookshelf.Providers.ComicBookInfo
{
/// <summary>
/// Utilities to help read JSON metadata.
/// </summary>
public interface IComicBookInfoUtilities
{
/// <summary>
/// Read comic book metadata.
/// </summary>
/// <param name="comic">The comic to read metadata from.</param>
/// <returns>The resulting book.</returns>
Book? ReadComicBookMetadata(ComicBookInfoMetadata comic);

/// <summary>
/// Read people metadata.
/// </summary>
/// <param name="comic">The comic to read metadata from.</param>
/// <param name="metadataResult">The metadata result to update.</param>
void ReadPeopleMetadata(ComicBookInfoMetadata comic, MetadataResult<Book> metadataResult);

/// <summary>
/// Returns the language display name of a given language.
/// </summary>
/// <param name="language">The language to convert.</param>
/// <returns>The language display name.</returns>
string? ReadCultureInfoInto(string language);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,31 @@ public class ComicInfoXmlUtilities : IComicInfoXmlUtilities
var hasFoundMetadata = false;

hasFoundMetadata |= ReadStringInto(xml, "ComicInfo/Title", title => book.Name = title);
hasFoundMetadata |= ReadStringInto(xml, "ComicInfo/AlternateSeries", title => book.OriginalTitle = title);
// this value is used internally only, as Jellyfin has no field to save it to
var isManga = false;

hasFoundMetadata |= ReadStringInto(xml, "ComicInfo/Title", title => book.Name = title);
hasFoundMetadata |= ReadStringInto(xml, "ComicInfo/Manga", manga =>
{
if (manga.Equals("Yes", StringComparison.OrdinalIgnoreCase))
{
isManga = true;
}
});
hasFoundMetadata |= ReadStringInto(xml, "ComicInfo/AlternateSeries", title =>
{
if (isManga)
{
// Software like ComicTagger (https://github.com/comictagger/comictagger) uses
// this field for the series name in the original language when tagging manga
book.OriginalTitle = title;
}
else
{
// Based on the The Anansi Project, some US comics can be part of cross-over
// story arcs. This field is then used to specify an alternate series
}
});
hasFoundMetadata |= ReadStringInto(xml, "ComicInfo/Series", series => book.SeriesName = series);
hasFoundMetadata |= ReadIntInto(xml, "ComicInfo/Number", issue => book.IndexNumber = issue);
hasFoundMetadata |= ReadStringInto(xml, "ComicInfo/Summary", summary => book.Overview = summary);
Expand Down
Loading