Skip to content

Commit

Permalink
Update opf and add ISBN (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
AshleyDeo authored Jul 24, 2024
1 parent f6cf809 commit 05559fd
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
26 changes: 26 additions & 0 deletions Jellyfin.Plugin.Bookshelf/Providers/ISBNExternalId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;

namespace Jellyfin.Plugin.Bookshelf.Providers
{
/// <inheritdoc />
public class ISBNExternalId : IExternalId
{
/// <inheritdoc />
public string ProviderName => "ISBN";

/// <inheritdoc />
public string Key => "ISBN";

/// <inheritdoc />
public ExternalIdMediaType? Type => null;

/// <inheritdoc />
public string? UrlFormatString => "https://search.worldcat.org/search?q=bn:{0}";

/// <inheritdoc />
public bool Supports(IHasProviderIds item) => item is Book;
}
}
53 changes: 48 additions & 5 deletions Jellyfin.Plugin.Bookshelf/Providers/OpfReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -104,11 +106,7 @@ public MetadataResult<Book> ReadOpfData(

var book = CreateBookFromOpf();
var bookResult = new MetadataResult<Book> { Item = book, HasMetadata = true };
ReadStringInto("//dc:creator", author =>
{
var person = new PersonInfo { Name = author, Type = PersonKind.Author };
bookResult.AddPerson(person);
});
FindAuthors(bookResult);

ReadStringInto("//dc:language", language => bookResult.ResultLanguage = language);

Expand All @@ -126,6 +124,7 @@ private Book CreateBookFromOpf()
ReadStringInto("//dc:publisher", publisher => book.AddStudio(publisher));
ReadStringInto("//dc:identifier[@opf:scheme='ISBN']", isbn => book.SetProviderId("ISBN", isbn));
ReadStringInto("//dc:identifier[@opf:scheme='AMAZON']", amazon => book.SetProviderId("Amazon", amazon));
ReadStringInto("//dc:identifier[@opf:scheme='GOOGLE']", google => book.SetProviderId("GoogleBooks", google));

ReadStringInto("//dc:date", date =>
{
Expand Down Expand Up @@ -222,6 +221,50 @@ private string FindMainTitle()
return titleSort;
}

private void FindAuthors(MetadataResult<Book> book)
{
var resultElement = _document.SelectNodes("//dc:creator", _namespaceManager);
if (resultElement != null && resultElement.Count > 0)
{
foreach (XmlElement creator in resultElement)
{
var creatorName = creator.InnerText;
string? role = creator.GetAttribute("opf:role");
var person = new PersonInfo { Name = creatorName, Type = GetRole(role) };
book.AddPerson(person);
}
}
}

private PersonKind GetRole(string role)
{
switch (role)
{
case "arr":
return PersonKind.Arranger;
case "art":
return PersonKind.Artist;
case "aut":
case "aqt":
case "aft":
case "aui":
default:
return PersonKind.Author;
case "edt":
return PersonKind.Editor;
case "ill":
return PersonKind.Illustrator;
case "lyr":
return PersonKind.Lyricist;
case "mus":
return PersonKind.AlbumArtist;
case "oth":
return PersonKind.Unknown;
case "trl":
return PersonKind.Translator;
}
}

private void ReadStringInto(string xPath, Action<string> commitResult)
{
var resultElement = _document.SelectSingleNode(xPath, _namespaceManager);
Expand Down

0 comments on commit 05559fd

Please sign in to comment.