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

Update opf and add ISBN #108

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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