Make ComicBookInfoProvider testable

This commit is contained in:
Patrick Farwick 2022-09-30 20:51:37 +00:00 committed by Patrick Farwick
parent 69f90e5c4c
commit 869ee93bcc
2 changed files with 65 additions and 26 deletions

View File

@ -2,7 +2,6 @@ using System;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -18,7 +17,7 @@ namespace Jellyfin.Plugin.Bookshelf.Providers.ComicBookInfo
/// <summary> /// <summary>
/// Comic book info provider. /// Comic book info provider.
/// </summary> /// </summary>
public class ComicBookInfoProvider : IComicFileProvider public class ComicBookInfoProvider : IComicFileProvider, IComicBookInfoUtilities
{ {
private readonly ILogger<ComicBookInfoProvider> _logger; private readonly ILogger<ComicBookInfoProvider> _logger;
@ -118,22 +117,14 @@ namespace Jellyfin.Plugin.Bookshelf.Providers.ComicBookInfo
if (comic.Metadata.Credits.Count > 0) if (comic.Metadata.Credits.Count > 0)
{ {
foreach (var person in comic.Metadata.Credits) ReadPeopleMetadata(comic.Metadata, metadataResult);
{
if (person.Person is null || person.Role is null)
{
continue;
}
var personInfo = new PersonInfo { Name = person.Person, Type = person.Role };
metadataResult.AddPerson(personInfo);
}
} }
return metadataResult; return metadataResult;
} }
private Book? ReadComicBookMetadata(ComicBookInfoMetadata comic) /// <inheritdoc />
public Book? ReadComicBookMetadata(ComicBookInfoMetadata comic)
{ {
var book = new Book(); var book = new Book();
var hasFoundMetadata = false; var hasFoundMetadata = false;
@ -178,6 +169,35 @@ namespace Jellyfin.Plugin.Bookshelf.Providers.ComicBookInfo
} }
} }
/// <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;
}
var personInfo = new PersonInfo { Name = person.Person, Type = person.Role };
metadataResult.AddPerson(personInfo);
}
}
/// <inheritdoc />
public string? ReadCultureInfoAsThreeLetterIsoInto(string language)
{
try
{
return new CultureInfo(language).ThreeLetterISOLanguageName;
}
catch (Exception)
{
// Ignored
return null;
}
}
private bool ReadStringInto(string? data, Action<string> commitResult) private bool ReadStringInto(string? data, Action<string> commitResult)
{ {
if (!string.IsNullOrWhiteSpace(data)) if (!string.IsNullOrWhiteSpace(data))
@ -205,19 +225,6 @@ namespace Jellyfin.Plugin.Bookshelf.Providers.ComicBookInfo
} }
} }
private string? ReadCultureInfoAsThreeLetterIsoInto(string language)
{
try
{
return new CultureInfo(language).ThreeLetterISOLanguageName;
}
catch (Exception)
{
// Ignored
return null;
}
}
private FileSystemMetadata? GetComicBookFile(string path) private FileSystemMetadata? GetComicBookFile(string path)
{ {
var fileInfo = _fileSystem.GetFileSystemInfo(path); var fileInfo = _fileSystem.GetFileSystemInfo(path);

View File

@ -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>
/// Converts a language to the three letter iso name of the language.
/// </summary>
/// <param name="language">The language to convert.</param>
/// <returns>The language represented by an three letter iso name.</returns>
string? ReadCultureInfoAsThreeLetterIsoInto(string language);
}
}