New: Readarr 0.1

This commit is contained in:
ta264
2020-05-06 21:14:11 +01:00
parent 476f2d6047
commit 08496c82af
911 changed files with 14837 additions and 24442 deletions
@@ -4,9 +4,9 @@ using NzbDrone.Core.Music;
namespace NzbDrone.Core.MetadataSource
{
public interface IProvideAlbumInfo
public interface IProvideBookInfo
{
Tuple<string, Album, List<ArtistMetadata>> GetAlbumInfo(string id);
Tuple<string, Book, List<AuthorMetadata>> GetBookInfo(string id);
HashSet<string> GetChangedAlbums(DateTime startTime);
}
}
@@ -4,9 +4,9 @@ using NzbDrone.Core.Music;
namespace NzbDrone.Core.MetadataSource
{
public interface IProvideArtistInfo
public interface IProvideAuthorInfo
{
Artist GetArtistInfo(string readarrId, int metadataProfileId);
Author GetAuthorInfo(string readarrId);
HashSet<string> GetChangedArtists(DateTime startTime);
}
}
@@ -3,9 +3,12 @@ using NzbDrone.Core.Music;
namespace NzbDrone.Core.MetadataSource
{
public interface ISearchForNewAlbum
public interface ISearchForNewBook
{
List<Album> SearchForNewAlbum(string title, string artist);
List<Album> SearchForNewAlbumByRecordingIds(List<string> recordingIds);
List<Book> SearchForNewBook(string title, string artist);
List<Book> SearchByIsbn(string isbn);
List<Book> SearchByAsin(string asin);
List<Book> SearchByGoodreadsId(int goodreadsId);
List<Book> SearchForNewAlbumByRecordingIds(List<string> recordingIds);
}
}
@@ -3,8 +3,8 @@ using NzbDrone.Core.Music;
namespace NzbDrone.Core.MetadataSource
{
public interface ISearchForNewArtist
public interface ISearchForNewAuthor
{
List<Artist> SearchForNewArtist(string title);
List<Author> SearchForNewAuthor(string title);
}
}
@@ -6,7 +6,7 @@ using NzbDrone.Core.Music;
namespace NzbDrone.Core.MetadataSource
{
public class SearchArtistComparer : IComparer<Artist>
public class SearchArtistComparer : IComparer<Author>
{
private static readonly Regex RegexCleanPunctuation = new Regex("[-._:]", RegexOptions.Compiled);
private static readonly Regex RegexCleanCountryYearPostfix = new Regex(@"(?<=.+)( \([A-Z]{2}\)| \(\d{4}\)| \([A-Z]{2}\) \(\d{4}\))$", RegexOptions.Compiled);
@@ -33,7 +33,7 @@ namespace NzbDrone.Core.MetadataSource
}
}
public int Compare(Artist x, Artist y)
public int Compare(Author x, Author y)
{
int result = 0;
@@ -61,7 +61,7 @@ namespace NzbDrone.Core.MetadataSource
return Compare(x, y, s => SearchQuery.LevenshteinDistanceClean(s.Name));
}
public int Compare<T>(Artist x, Artist y, Func<Artist, T> keySelector)
public int Compare<T>(Author x, Author y, Func<Author, T> keySelector)
where T : IComparable<T>
{
var keyX = keySelector(x);
@@ -0,0 +1,117 @@
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using NzbDrone.Common.Http;
using NzbDrone.Core.MetadataSource.SkyHook;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
public static class HttpResponseExtensions
{
public static T Deserialize<T>(this HttpResponse response, string elementName = null)
where T : GoodreadsResource, new()
{
response.ThrowIfException();
try
{
var document = XDocument.Parse(response.Content);
if (document.Root == null ||
document.Root.Name == "error")
{
return null;
}
else
{
var root = document.Element("GoodreadsResponse") ?? (XNode)document;
var responseObject = new T();
var contentRoot = root.XPathSelectElement(elementName ?? responseObject.ElementName);
responseObject.Parse(contentRoot);
return responseObject;
}
}
catch (XmlException)
{
return null;
}
}
private static void ThrowIfException(this HttpResponse response)
{
// Try and find an error from the Goodreads response
string error = null;
try
{
var document = XDocument.Parse(response.Content);
// Goodreads returns several different types of errors...
if (document.Root != null)
{
if (document.Root.Name == "error")
{
// One is a single XML error node
var element = document.Element("error");
if (element != null)
{
error = element.Value;
}
}
else if (document.Root.Name == "errors")
{
// Another one is a list of XML error nodes
var element = document.Element("errors");
var children = element?.Descendants("error");
if (children.Any())
{
error = string.Join(Environment.NewLine, children.Select(x => x.Value));
}
}
else if (document.Root.Name == "hash")
{
// And another one is in a "hash" XML object
var element = document.Element("hash");
if (element != null)
{
var status = element.ElementAsString("status");
var message = element.ElementAsString("error");
if (!string.IsNullOrEmpty(message))
{
error = string.Join(" ", status, message);
}
}
}
else
{
// Yet another one is an entire XML structure with multiple messages...
var element = document.XPathSelectElement("GoodreadsResponse/error");
if (element != null)
{
// There are four total error messages
var plain = element.Value;
var genericMessage = element.ElementAsString("generic");
var detailMessage = element.ElementAsString("detail");
var friendlyMessage = element.ElementAsString("friendly");
// Use the best message that exists...
error = friendlyMessage ?? detailMessage ?? genericMessage ?? plain;
}
}
}
}
catch (XmlException)
{
// We don't really care if any exception was thrown above
// we're just trying to find an error message after all...
}
// If we found any error at all above, throw an exception
if (!string.IsNullOrWhiteSpace(error))
{
throw new SkyHookException("Received an error from Goodreads " + error);
}
}
}
}
@@ -0,0 +1,241 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
internal static class XmlExtensions
{
public static string ElementAsString(this XElement element, XName name, bool trim = false)
{
var el = element.Element(name);
return string.IsNullOrWhiteSpace(el?.Value)
? null
: (trim ? el.Value.Trim() : el.Value);
}
public static long ElementAsLong(this XElement element, XName name)
{
var el = element.Element(name);
return long.TryParse(el?.Value, out long value) ? value : default(long);
}
public static long? ElementAsNullableLong(this XElement element, XName name)
{
var el = element.Element(name);
return long.TryParse(el?.Value, out long value) ? new long?(value) : null;
}
public static int ElementAsInt(this XElement element, XName name)
{
var el = element.Element(name);
return int.TryParse(el?.Value, out int value) ? value : default(int);
}
public static int? ElementAsNullableInt(this XElement element, XName name)
{
var el = element.Element(name);
return int.TryParse(el?.Value, out int value) ? new int?(value) : null;
}
public static decimal ElementAsDecimal(this XElement element, XName name)
{
var el = element.Element(name);
return decimal.TryParse(el?.Value, out decimal value) ? value : default(decimal);
}
public static decimal? ElementAsNullableDecimal(this XElement element, XName name)
{
var el = element.Element(name);
return decimal.TryParse(el?.Value, out decimal value) ? new decimal?(value) : null;
}
public static DateTime? ElementAsDate(this XElement element, XName name)
{
var el = element.Element(name);
return DateTime.TryParseExact(el?.Value, "yyyy/MM/dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date)
? new DateTime?(date)
: null;
}
public static DateTime? ElementAsDateTime(this XElement element, XName name)
{
var dateElement = element.Element(name);
if (dateElement != null)
{
var value = dateElement.Value;
// The Goodreads date includes the timezone as -hhmm whereas C# wants it to be -hh:mm
// This regex corrects the format and hopefully doesn't mess anything else up...
var validDateFormat = Regex.Replace(value, @"(.*) ([+-]\d{2})(\d{2}) (.*)", "$1 $2:$3 $4");
DateTime localDate;
if (DateTime.TryParseExact(
validDateFormat,
"ddd MMM dd HH:mm:ss zzz yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out localDate))
{
return localDate.ToUniversalTime();
}
else if (DateTime.TryParseExact(
validDateFormat,
"yyyy-MM-ddTHH:mm:sszzz",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out localDate))
{
return localDate.ToUniversalTime();
}
}
return null;
}
public static DateTime? ElementAsMonthYear(this XElement element, XName name)
{
var el = element.Element(name);
return DateTime.TryParseExact(el?.Value, "MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date)
? new DateTime?(date)
: null;
}
/// <summary>
/// Goodreads sometimes returns dates as three separate fields.
/// This method parses out each one and returns a date object.
/// </summary>
/// <param name="element">The parent element of the date elements.</param>
/// <param name="prefix">The common prefix for the three Goodreads date elements.</param>
/// <returns>A date object after parsing the three Goodreads date fields.</returns>
public static DateTime? ElementAsMultiDateField(this XElement element, string prefix)
{
var publicationYear = element.ElementAsNullableInt(prefix + "_year");
var publicationMonth = element.ElementAsNullableInt(prefix + "_month");
var publicationDay = element.ElementAsNullableInt(prefix + "_day");
if (!publicationYear.HasValue &&
!publicationMonth.HasValue &&
!publicationDay.HasValue)
{
return null;
}
if (!publicationYear.HasValue)
{
return null;
}
if (!publicationDay.HasValue)
{
publicationDay = 1;
}
if (!publicationMonth.HasValue)
{
publicationMonth = 1;
}
try
{
return new DateTime(publicationYear.Value, publicationMonth.Value, publicationDay.Value);
}
catch
{
return null;
}
}
public static bool ElementAsBool(this XElement element, XName name)
{
var el = element.Element(name);
return bool.TryParse(el?.Value, out bool value) ? value : false;
}
public static List<T> ParseChildren<T>(this XElement element, XName parentName, XName childName)
where T : GoodreadsResource, new()
{
return ParseChildren(
element,
parentName,
childName,
(childElement) =>
{
var child = new T();
child.Parse(childElement);
return child;
});
}
public static List<T> ParseChildren<T>(this XElement element, XName parentName, XName childName, Func<XElement, T> parseChild)
{
var parentElement = element.Element(parentName);
if (parentElement != null)
{
var childElements = parentElement.Descendants(childName);
if (childElements.Any())
{
var children = new List<T>();
foreach (var childElement in childElements)
{
children.Add(parseChild(childElement));
}
return children;
}
}
return null;
}
public static List<T> ParseChildren<T>(this XElement element)
where T : GoodreadsResource, new()
{
var childElements = element.Elements();
if (childElements.Any())
{
var children = new List<T>();
foreach (var childElement in childElements)
{
var child = new T();
child.Parse(childElement);
children.Add(child);
}
return children;
}
return null;
}
public static string AttributeAsString(this XElement element, XName attributeName)
{
var attr = element.Attribute(attributeName);
return string.IsNullOrWhiteSpace(attr?.Value) ? null : attr.Value;
}
public static int AttributeAsInt(this XElement element, XName attributeName)
{
var attr = element.Attribute(attributeName);
return int.TryParse(attr?.Value, out int value) ? value : default(int);
}
public static long? AttributeAsNullableLong(this XElement element, XName attributeName)
{
var attr = element.Attribute(attributeName);
return long.TryParse(attr?.Value, out long value) ? new long?(value) : null;
}
public static bool AttributeAsBool(this XElement element, XName attributeName)
{
var attr = element.Attribute(attributeName);
return bool.TryParse(attr?.Value, out bool value) ? value : false;
}
}
}
@@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
/// <summary>
/// This class models the best book in a work, as defined by the Goodreads API.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class AuthorBookListResource : GoodreadsResource
{
public override string ElementName => "author";
public List<BookResource> List { get; private set; }
public override void Parse(XElement element)
{
var results = element.Descendants("books");
if (results.Count() == 1)
{
List = results.First().ParseChildren<BookResource>();
}
}
}
}
@@ -0,0 +1,145 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
/// <summary>
/// This class models an Author as defined by the Goodreads API.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class AuthorResource : GoodreadsResource
{
public override string ElementName => "author";
/// <summary>
/// The Goodreads Author Id.
/// </summary>
public long Id { get; private set; }
/// <summary>
/// The full name of the author.
/// </summary>
public string Name { get; private set; }
/// <summary>
/// The Url to the Goodreads author page.
/// </summary>
public string Link { get; private set; }
/// <summary>
/// The number of fans for this author.
/// The Goodreads Fan API has been replaced by Followers.
/// For this property, use FollowersCount instead.
/// </summary>
[Obsolete("Fans API has been deprecated by Goodreads. Use Followers instead.")]
public int FansCount { get; private set; }
/// <summary>
/// The number of Goodreads users that are following this author.
/// </summary>
public int FollowersCount { get; private set; }
/// <summary>
/// The Url to the author's image, large size.
/// </summary>
public string LargeImageUrl { get; private set; }
/// <summary>
/// The Url to the author's image.
/// </summary>
public string ImageUrl { get; private set; }
/// <summary>
/// The Url to the author's image, small size.
/// </summary>
public string SmallImageUrl { get; private set; }
/// <summary>
/// A brief description about this author. This field may contain HTML.
/// </summary>
public string About { get; private set; }
/// <summary>
/// People that may have influenced this author. This field may contain HTML.
/// </summary>
public string Influences { get; private set; }
/// <summary>
/// The total number of items the author has worked on and are listed within Goodreads.
/// </summary>
public int WorksCount { get; private set; }
/// <summary>
/// The gender of the author. This field might be limited to only "male" and "female"
/// but is left as a string in case any other options are possible through the Goodreads API.
/// </summary>
public string Gender { get; private set; }
/// <summary>
/// The hometown the author grew up in.
/// </summary>
public string Hometown { get; private set; }
/// <summary>
/// The author's birthdate.
/// </summary>
public DateTime? BornOnDate { get; private set; }
/// <summary>
/// The date on which the author died.
/// </summary>
public DateTime? DiedOnDate { get; private set; }
/// <summary>
/// Determines whether this author is also a regular Goodreads user or not.
/// </summary>
public bool IsGoodreadsAuthor { get; private set; }
/// <summary>
/// If <see cref="IsGoodreadsAuthor"/> is true, this property is set to the author's Goodreads user Id.
/// </summary>
public int? GoodreadsUserId { get; private set; }
internal string DebuggerDisplay
{
get
{
return string.Format(
CultureInfo.InvariantCulture,
"Author: Id: {0}, Name: {1}",
Id,
Name);
}
}
public override void Parse(XElement element)
{
Id = element.ElementAsLong("id");
Name = element.ElementAsString("name");
Link = element.ElementAsString("link");
FollowersCount = element.ElementAsInt("author_followers_count");
LargeImageUrl = element.ElementAsString("large_image_url");
ImageUrl = element.ElementAsString("image_url");
SmallImageUrl = element.ElementAsString("small_image_url");
About = element.ElementAsString("about");
Influences = element.ElementAsString("influences");
WorksCount = element.ElementAsInt("works_count");
Gender = element.ElementAsString("gender");
Hometown = element.ElementAsString("hometown");
BornOnDate = element.ElementAsDate("born_at");
DiedOnDate = element.ElementAsDate("died_at");
IsGoodreadsAuthor = element.ElementAsBool("goodreads_author");
if (IsGoodreadsAuthor)
{
var goodreadsUser = element.Element("user");
if (goodreadsUser != null)
{
GoodreadsUserId = goodreadsUser.ElementAsInt("id");
}
}
}
}
}
@@ -0,0 +1,51 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
/// <summary>
/// This class models the best book in a work, as defined by the Goodreads API.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class AuthorSeriesListResource : GoodreadsResource
{
public override string ElementName => "series_works";
public List<SeriesResource> List { get; private set; }
public override void Parse(XElement element)
{
var pairs = element.Descendants("series_work");
if (pairs.Any())
{
var dict = new Dictionary<long, SeriesResource>();
foreach (var pair in pairs)
{
var series = new SeriesResource();
series.Parse(pair.Element("series"));
if (!dict.TryGetValue(series.Id, out var cached))
{
dict[series.Id] = series;
cached = series;
}
var work = new WorkResource();
work.Parse(pair.Element("work"));
work.SetSeriesInfo(pair);
cached.Works.Add(work);
}
List = dict.Values.ToList();
}
else
{
List = new List<SeriesResource>();
}
}
}
}
@@ -0,0 +1,73 @@
using System.Diagnostics;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
/// <summary>
/// This class models areas of the API where Goodreads returns
/// very brief information about an Author instead of their entire profile.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class AuthorSummaryResource : GoodreadsResource
{
public override string ElementName => "author";
/// <summary>
/// The Goodreads Author Id.
/// </summary>
public long Id { get; private set; }
/// <summary>
/// The name of this author.
/// </summary>
public string Name { get; private set; }
/// <summary>
/// The role of this author.
/// </summary>
public string Role { get; private set; }
/// <summary>
/// The image of this author, regular size.
/// </summary>
public string ImageUrl { get; private set; }
/// <summary>
/// The image of this author, small size.
/// </summary>
public string SmallImageUrl { get; private set; }
/// <summary>
/// The link to the Goodreads page for this author.
/// </summary>
public string Link { get; private set; }
/// <summary>
/// The average rating for all of this author's books.
/// </summary>
public decimal? AverageRating { get; private set; }
/// <summary>
/// The total count of all ratings of this author's books.
/// </summary>
public int? RatingsCount { get; private set; }
/// <summary>
/// The total count of all the text reviews of this author's books.
/// </summary>
public int? TextReviewsCount { get; private set; }
public override void Parse(XElement element)
{
Id = element.ElementAsLong("id");
Name = element.ElementAsString("name");
Role = element.ElementAsString("role");
ImageUrl = element.ElementAsString("image_url");
SmallImageUrl = element.ElementAsString("small_image_url");
Link = element.ElementAsString("link");
AverageRating = element.ElementAsNullableDecimal("average_rating");
RatingsCount = element.ElementAsNullableInt("ratings_count");
TextReviewsCount = element.ElementAsNullableInt("text_reviews_count");
}
}
}
@@ -0,0 +1,57 @@
using System.Diagnostics;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
/// <summary>
/// This class models the best book in a work, as defined by the Goodreads API.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class BestBookResource : GoodreadsResource
{
public override string ElementName => "best_book";
/// <summary>
/// The Id of this book.
/// </summary>
public long Id { get; private set; }
/// <summary>
/// The title of this book.
/// </summary>
public string Title { get; private set; }
/// <summary>
/// The Goodreads id of the author.
/// </summary>
public long AuthorId { get; private set; }
/// <summary>
/// The name of the author.
/// </summary>
public string AuthorName { get; private set; }
/// <summary>
/// The cover image of this book.
/// </summary>
public string ImageUrl { get; private set; }
public string LargeImageUrl { get; private set; }
public override void Parse(XElement element)
{
Id = element.ElementAsLong("id");
Title = element.ElementAsString("title");
var authorElement = element.Element("author");
if (authorElement != null)
{
AuthorId = authorElement.ElementAsLong("id");
AuthorName = authorElement.ElementAsString("name");
}
ImageUrl = element.ElementAsString("image_url");
ImageUrl = element.ElementAsString("large_image_url");
}
}
}
@@ -0,0 +1,56 @@
using System.Diagnostics;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
/// <summary>
/// This class models a book link as defined by the Goodreads API.
/// This is usually a link to a third-party site to purchase the book.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class BookLinkResource : GoodreadsResource
{
public override string ElementName => "book_link";
/// <summary>
/// The Id of this book link.
/// </summary>
public long Id { get; private set; }
/// <summary>
/// The name of this book link provider.
/// </summary>
public string Name { get; private set; }
/// <summary>
/// The link to this book on the provider's site.
/// Be sure to append book_id as a query parameter
/// to actually be redirected to the correct page.
/// </summary>
public string Link { get; private set; }
public override void Parse(XElement element)
{
Id = element.ElementAsLong("id");
Name = element.ElementAsString("name");
Link = element.ElementAsString("link");
}
/// <summary>
/// Goodreads returns incomplete book links for some reason.
/// The link results in an error unless you append a book_id query parameter.
/// This method fixes up these book links with the given book id.
/// </summary>
/// <param name="bookId">The book id to append to the book link.</param>
internal void FixBookLink(long bookId)
{
if (!string.IsNullOrWhiteSpace(Link))
{
if (!Link.Contains("book_id"))
{
Link += (Link.Contains("?") ? "&" : "?") + "book_id=" + bookId;
}
}
}
}
}
@@ -0,0 +1,239 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
/// <summary>
/// This class models a single book as defined by the Goodreads API.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class BookResource : GoodreadsResource
{
public override string ElementName => "book";
/// <summary>
/// The Goodreads Id for this book.
/// </summary>
public long Id { get; private set; }
/// <summary>
/// The title of this book.
/// </summary>
public string Title { get; private set; }
/// <summary>
/// The description of this book.
/// </summary>
public string Description { get; private set; }
/// <summary>
/// The ISBN of this book.
/// </summary>
public string Isbn { get; private set; }
/// <summary>
/// The ISBN13 of this book.
/// </summary>
public string Isbn13 { get; private set; }
/// <summary>
/// The ASIN of this book.
/// </summary>
public string Asin { get; private set; }
/// <summary>
/// The Kindle ASIN of this book.
/// </summary>
public string KindleAsin { get; private set; }
/// <summary>
/// The marketplace Id of this book.
/// </summary>
public string MarketplaceId { get; private set; }
/// <summary>
/// The country code of this book.
/// </summary>
public string CountryCode { get; private set; }
/// <summary>
/// The cover image for this book.
/// </summary>
public string ImageUrl { get; private set; }
/// <summary>
/// The small cover image for this book.
/// </summary>
public string SmallImageUrl { get; private set; }
/// <summary>
/// The date this book was published.
/// </summary>
public DateTime? PublicationDate { get; private set; }
/// <summary>
/// The publisher of this book.
/// </summary>
public string Publisher { get; private set; }
/// <summary>
/// The language code of this book.
/// </summary>
public string LanguageCode { get; private set; }
/// <summary>
/// Signifies if this is an eBook or not.
/// </summary>
public bool IsEbook { get; private set; }
/// <summary>
/// The average rating of this book by Goodreads users.
/// </summary>
public decimal AverageRating { get; private set; }
/// <summary>
/// The number of pages in this book.
/// </summary>
public int Pages { get; private set; }
/// <summary>
/// The format of this book.
/// </summary>
public string Format { get; private set; }
/// <summary>
/// Brief information about this edition of the book.
/// </summary>
public string EditionInformation { get; private set; }
/// <summary>
/// The count of all Goodreads ratings for this book.
/// </summary>
public int RatingsCount { get; private set; }
/// <summary>
/// The count of all reviews that contain text for this book.
/// </summary>
public int TextReviewsCount { get; private set; }
/// <summary>
/// The Goodreads Url for this book.
/// </summary>
public string Url { get; private set; }
/// <summary>
/// The aggregate information for this work across all editions of the book.
/// </summary>
public WorkResource Work { get; private set; }
/// <summary>
/// The list of authors that worked on this book.
/// </summary>
public IReadOnlyList<AuthorSummaryResource> Authors { get; private set; }
/// <summary>
/// HTML and CSS for the Goodreads iFrame. Used to display the reviews for this book.
/// </summary>
public string ReviewsWidget { get; private set; }
/// <summary>
/// The most popular shelf names this book appears on. This is a
/// dictionary of shelf name -> count.
/// </summary>
public IReadOnlyDictionary<string, int> PopularShelves { get; private set; }
/// <summary>
/// The list of book links tracked by Goodreads.
/// This is usually a list of libraries that the user can borrow the book from.
/// </summary>
public IReadOnlyList<BookLinkResource> BookLinks { get; private set; }
/// <summary>
/// The list of buy links tracked by Goodreads.
/// This is usually a list of third-party sites that the
/// user can purchase the book from.
/// </summary>
public IReadOnlyList<BookLinkResource> BuyLinks { get; private set; }
/// <summary>
/// Summary information about similar books to this one.
/// </summary>
public IReadOnlyList<BookSummaryResource> SimilarBooks { get; private set; }
// TODO: parse series information once I get a better sense
// of what series are from the other API calls.
//// public List<Series> Series { get; private set; }
public override void Parse(XElement element)
{
Id = element.ElementAsLong("id");
Title = element.ElementAsString("title");
Isbn = element.ElementAsString("isbn");
Isbn13 = element.ElementAsString("isbn13");
Asin = element.ElementAsString("asin");
KindleAsin = element.ElementAsString("kindle_asin");
MarketplaceId = element.ElementAsString("marketplace_id");
CountryCode = element.ElementAsString("country_code");
ImageUrl = element.ElementAsString("image_url");
SmallImageUrl = element.ElementAsString("small_image_url");
PublicationDate = element.ElementAsMultiDateField("publication");
Publisher = element.ElementAsString("publisher");
LanguageCode = element.ElementAsString("language_code");
IsEbook = element.ElementAsBool("is_ebook");
Description = element.ElementAsString("description");
AverageRating = element.ElementAsDecimal("average_rating");
Pages = element.ElementAsInt("num_pages");
Format = element.ElementAsString("format");
EditionInformation = element.ElementAsString("edition_information");
RatingsCount = element.ElementAsInt("ratings_count");
TextReviewsCount = element.ElementAsInt("text_reviews_count");
Url = element.ElementAsString("url");
ReviewsWidget = element.ElementAsString("reviews_widget");
var workElement = element.Element("work");
if (workElement != null)
{
Work = new WorkResource();
Work.Parse(workElement);
}
Authors = element.ParseChildren<AuthorSummaryResource>("authors", "author");
SimilarBooks = element.ParseChildren<BookSummaryResource>("similar_books", "book");
var bookLinks = element.ParseChildren<BookLinkResource>("book_links", "book_link");
if (bookLinks != null)
{
bookLinks.ForEach(x => x.FixBookLink(Id));
BookLinks = bookLinks;
}
var buyLinks = element.ParseChildren<BookLinkResource>("buy_links", "buy_link");
if (buyLinks != null)
{
buyLinks.ForEach(x => x.FixBookLink(Id));
BuyLinks = buyLinks;
}
var shelves = element.ParseChildren(
"popular_shelves",
"shelf",
(shelfElement) =>
{
var shelfName = shelfElement?.Attribute("name")?.Value;
var shelfCountValue = shelfElement?.Attribute("count")?.Value;
int shelfCount = 0;
int.TryParse(shelfCountValue, out shelfCount);
return new KeyValuePair<string, int>(shelfName, shelfCount);
});
if (shelves != null)
{
PopularShelves = shelves.GroupBy(obj => obj.Key).ToDictionary(shelf => shelf.Key, shelf => shelf.Sum(x => x.Value));
}
}
}
}
@@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
/// <summary>
/// This class models the best book in a work, as defined by the Goodreads API.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class BookSearchResultResource : GoodreadsResource
{
public override string ElementName => "search";
public List<WorkResource> Results { get; private set; }
public override void Parse(XElement element)
{
var results = element.Descendants("results");
if (results.Count() == 1)
{
Results = results.First().ParseChildren<WorkResource>();
}
}
}
}
@@ -0,0 +1,150 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
/// <summary>
/// This class models areas of the API where Goodreads returns
/// very brief information about a Book instead of their entire object.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class BookSummaryResource : GoodreadsResource
{
public override string ElementName => "book";
/// <summary>
/// The Id of this book.
/// </summary>
public long Id { get; private set; }
public string Uri { get; set; }
/// <summary>
/// The title of this book.
/// </summary>
public string Title { get; private set; }
/// <summary>
/// The title of this book without series information in it.
/// </summary>
public string TitleWithoutSeries { get; private set; }
/// <summary>
/// The link to the Goodreads page for this book.
/// </summary>
public string Link { get; private set; }
/// <summary>
/// The cover image of this book, regular size.
/// </summary>
public string ImageUrl { get; private set; }
/// <summary>
/// The cover image of this book, small size.
/// </summary>
public string SmallImageUrl { get; private set; }
/// <summary>
/// The work id of this book.
/// </summary>
public long? WorkId { get; private set; }
/// <summary>
/// The ISBN of this book.
/// </summary>
public string Isbn { get; private set; }
/// <summary>
/// The ISBN13 of this book.
/// </summary>
public string Isbn13 { get; private set; }
/// <summary>
/// The average rating of the book.
/// </summary>
public decimal? AverageRating { get; private set; }
/// <summary>
/// The count of all ratings for the book.
/// </summary>
public int? RatingsCount { get; private set; }
/// <summary>
/// The date this book was published.
/// </summary>
public DateTime? PublicationDate { get; private set; }
/// <summary>
/// Summary information about the authors of this book.
/// </summary>
public IReadOnlyList<AuthorSummaryResource> Authors { get; private set; }
/// <summary>
/// The edition information about book.
/// </summary>
public string EditionInformation { get; private set; }
/// <summary>
/// The book format.
/// </summary>
public string Format { get; private set; }
/// <summary>
/// The book description.
/// </summary>
public string Description { get; private set; }
/// <summary>
/// Number of pages.
/// </summary>
public int NumberOfPages { get; private set; }
/// <summary>
/// The book publisher.
/// </summary>
public string Publisher { get; private set; }
/// <summary>
/// The image url, large size.
/// </summary>
public string LargeImageUrl { get; private set; }
/// <summary>
/// A count of text reviews for this book.
/// </summary>
public int TextReviewsCount { get; private set; }
public override void Parse(XElement element)
{
Id = element.ElementAsLong("id");
Uri = element.ElementAsString("uri");
Title = element.ElementAsString("title");
TitleWithoutSeries = element.ElementAsString("title_without_series");
Link = element.ElementAsString("link");
ImageUrl = element.ElementAsString("image_url");
SmallImageUrl = element.ElementAsString("small_image_url");
Isbn = element.ElementAsString("isbn");
Isbn13 = element.ElementAsString("isbn13");
AverageRating = element.ElementAsNullableDecimal("average_rating");
RatingsCount = element.ElementAsNullableInt("ratings_count");
PublicationDate = element.ElementAsMultiDateField("publication");
Authors = element.ParseChildren<AuthorSummaryResource>("authors", "author");
var workElement = element.Element("work");
if (workElement != null)
{
WorkId = workElement.ElementAsNullableInt("id");
}
EditionInformation = element.ElementAsString("edition_information");
Format = element.ElementAsString("format");
Description = element.ElementAsString("description");
NumberOfPages = element.ElementAsInt("num_pages");
Publisher = element.ElementAsString("publisher");
LargeImageUrl = element.ElementAsString("large_image_url");
TextReviewsCount = element.ElementAsInt("text_reviews_count");
}
}
}
@@ -0,0 +1,11 @@
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
public abstract class GoodreadsResource
{
public abstract string ElementName { get; }
public abstract void Parse(XElement element);
}
}
@@ -0,0 +1,82 @@
using System;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
/// <summary>
/// This class models areas of the API where Goodreads returns
/// information about an user owned books.
/// </summary>
public sealed class OwnedBookResource : GoodreadsResource
{
public override string ElementName => "owned_book";
/// <summary>
/// The owner book id.
/// </summary>
public long Id { get; private set; }
/// <summary>
/// The owner id.
/// </summary>
public long OwnerId { get; private set; }
/// <summary>
/// The original date when owner has bought a book.
/// </summary>
public DateTime? OriginalPurchaseDate { get; private set; }
/// <summary>
/// The original location where owner has bought a book.
/// </summary>
public string OriginalPurchaseLocation { get; private set; }
/// <summary>
/// The owned book condition.
/// </summary>
public string Condition { get; private set; }
/// <summary>
/// The traded count.
/// </summary>
public int TradedCount { get; private set; }
/// <summary>
/// The link to the owned book.
/// </summary>
public string Link { get; private set; }
/// <summary>
/// The book.
/// </summary>
public BookSummaryResource Book { get; private set; }
/// <summary>
/// The owned book review.
/// </summary>
public ReviewResource Review { get; private set; }
public override void Parse(XElement element)
{
Id = element.ElementAsLong("id");
OwnerId = element.ElementAsLong("current_owner_id");
OriginalPurchaseDate = element.ElementAsDateTime("original_purchase_date");
OriginalPurchaseLocation = element.ElementAsString("original_purchase_location");
Condition = element.ElementAsString("condition");
var review = element.Element("review");
if (review != null)
{
Review = new ReviewResource();
Review.Parse(review);
}
var book = element.Element("book");
if (book != null)
{
Book = new BookSummaryResource();
Book.Parse(book);
}
}
}
}
@@ -0,0 +1,47 @@
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
/// <summary>
/// Represents a paginated list of objects as returned by the Goodreads API,
/// along with pagination information about the page size, current page, etc...
/// </summary>
/// <typeparam name="T">The type of the object in the paginated list.</typeparam>
public class PaginatedList<T> : GoodreadsResource
where T : GoodreadsResource, new()
{
public override string ElementName => "";
/// <summary>
/// The list of objects for the current page.
/// </summary>
public IReadOnlyList<T> List { get; private set; }
/// <summary>
/// Pagination information about the list and current page.
/// </summary>
public PaginationModel Pagination { get; private set; }
public override void Parse(XElement element)
{
Pagination = new PaginationModel();
Pagination.Parse(element);
// Should have known search pagination would be different...
if (element.Name == "search")
{
var results = element.Descendants("results");
if (results.Count() == 1)
{
List = results.First().ParseChildren<T>();
}
}
else
{
List = element.ParseChildren<T>();
}
}
}
}
@@ -0,0 +1,58 @@
using System.Diagnostics;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
/// <summary>
/// Represents pagination information as returned by the Goodreads API.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class PaginationModel : GoodreadsResource
{
public override string ElementName => "";
/// <summary>
/// The item the current page starts on.
/// </summary>
public int Start { get; private set; }
/// <summary>
/// The item the current page ends on.
/// </summary>
public int End { get; private set; }
/// <summary>
/// The total number of items in the paginated list.
/// </summary>
public int TotalItems { get; private set; }
public override void Parse(XElement element)
{
// Search results have different pagination fields for some reason...
if (element.Name == "search")
{
Start = element.ElementAsInt("results-start");
End = element.ElementAsInt("results-end");
TotalItems = element.ElementAsInt("total-results");
return;
}
var startAttribute = element.Attribute("start");
var endAttribute = element.Attribute("end");
var totalAttribute = element.Attribute("total");
if (startAttribute != null &&
endAttribute != null &&
totalAttribute != null)
{
int.TryParse(startAttribute.Value, out int start);
int.TryParse(endAttribute.Value, out int end);
int.TryParse(totalAttribute.Value, out int total);
Start = start;
End = end;
TotalItems = total;
}
}
}
}
@@ -0,0 +1,131 @@
using System;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
/// <summary>
/// This class models a Review as defined by the Goodreads API.
/// </summary>
public class ReviewResource : GoodreadsResource
{
public override string ElementName => "review";
/// <summary>
/// The Goodreads review id.
/// </summary>
public long Id { get; protected set; }
/// <summary>
/// The summary information for the book this review is for.
/// </summary>
public BookSummaryResource Book { get; protected set; }
/// <summary>
/// The rating the user gave the book in this review.
/// </summary>
public int Rating { get; protected set; }
/// <summary>
/// The number of votes this review received from other Goodreads users.
/// </summary>
public int Votes { get; protected set; }
/// <summary>
/// A flag determining if the review contains spoilers.
/// </summary>
public bool IsSpoiler { get; protected set; }
/// <summary>
/// The state of the spoilers for this review.
/// </summary>
public string SpoilersState { get; protected set; }
/// <summary>
/// The shelves the user has added this review to.
/// </summary>
// public IReadOnlyList<ReviewShelf> Shelves { get; protected set; }
/// <summary>
/// Who the user would recommend reading this book.
/// </summary>
public string RecommendedFor { get; protected set; }
/// <summary>
/// Who recommended the user to read this book.
/// </summary>
public string RecommendedBy { get; protected set; }
/// <summary>
/// The date the user started reading this book.
/// </summary>
public DateTime? DateStarted { get; protected set; }
/// <summary>
/// The date the user finished reading this book.
/// </summary>
public DateTime? DateRead { get; protected set; }
/// <summary>
/// The date the user added this book to their shelves.
/// </summary>
public DateTime? DateAdded { get; protected set; }
/// <summary>
/// The date the user last updated this book on their shelves.
/// </summary>
public DateTime? DateUpdated { get; protected set; }
/// <summary>
/// The number of times this book has been read.
/// </summary>
public int? ReadCount { get; protected set; }
/// <summary>
/// The main text of this review. May contain HTML.
/// </summary>
public string Body { get; protected set; }
/// <summary>
/// The number of comments on this review.
/// </summary>
public int CommentsCount { get; protected set; }
/// <summary>
/// The Goodreads URL of this review.
/// </summary>
public string Url { get; protected set; }
/// <summary>
/// The owned count of the book.
/// </summary>
public int Owned { get; protected set; }
public override void Parse(XElement element)
{
Id = element.ElementAsLong("id");
var bookElement = element.Element("book");
if (bookElement != null)
{
Book = new BookSummaryResource();
Book.Parse(bookElement);
}
Rating = element.ElementAsInt("rating");
Votes = element.ElementAsInt("votes");
IsSpoiler = element.ElementAsBool("spoiler_flag");
SpoilersState = element.ElementAsString("spoilers_state");
RecommendedFor = element.ElementAsString("recommended_for");
RecommendedBy = element.ElementAsString("recommended_by");
DateStarted = element.ElementAsDateTime("started_at");
DateRead = element.ElementAsDateTime("read_at");
DateAdded = element.ElementAsDateTime("date_added");
DateUpdated = element.ElementAsDateTime("date_updated");
ReadCount = element.ElementAsInt("read_count");
Body = element.ElementAsString("body");
CommentsCount = element.ElementAsInt("comments_count");
Url = element.ElementAsString("url");
Owned = element.ElementAsInt("owned");
}
}
}
@@ -0,0 +1,72 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
/// <summary>
/// Represents information about a book series as defined by the Goodreads API.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class SeriesResource : GoodreadsResource
{
public SeriesResource()
{
Works = new List<WorkResource>();
}
public override string ElementName => "series";
/// <summary>
/// The Id of the series.
/// </summary>
public long Id { get; private set; }
/// <summary>
/// The title of the series.
/// </summary>
public string Title { get; private set; }
/// <summary>
/// The description of the series.
/// </summary>
public string Description { get; private set; }
/// <summary>
/// Any notes for the series.
/// </summary>
public string Note { get; private set; }
/// <summary>
/// How many works are contained in the series total.
/// </summary>
public int SeriesWorksCount { get; private set; }
/// <summary>
/// The count of works that are considered primary in the series.
/// </summary>
public int PrimaryWorksCount { get; private set; }
/// <summary>
/// Determines if the series is usually numbered or not.
/// </summary>
public bool IsNumbered { get; private set; }
/// <summary>
/// The list of works that are in this series.
/// Only populated if Goodreads returns it in the response.
/// </summary>
public List<WorkResource> Works { get; set; }
public override void Parse(XElement element)
{
Id = element.ElementAsLong("id");
Title = element.ElementAsString("title", true);
Description = element.ElementAsString("description", true);
Note = element.ElementAsString("note", true);
SeriesWorksCount = element.ElementAsInt("series_works_count");
PrimaryWorksCount = element.ElementAsInt("primary_work_count");
IsNumbered = element.ElementAsBool("numbered");
}
}
}
@@ -0,0 +1,113 @@
using System;
using System.Diagnostics;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
/// <summary>
/// Represents a user's shelf on their Goodreads profile.
/// </summary>
public sealed class UserShelfResource : GoodreadsResource
{
public override string ElementName => "shelf";
/// <summary>
/// The Id of this user shelf.
/// </summary>
public long Id { get; private set; }
/// <summary>
/// The name of this user shelf.
/// </summary>
public string Name { get; private set; }
/// <summary>
/// The number of books on this user shelf.
/// </summary>
public int BookCount { get; private set; }
/// <summary>
/// Determines if this shelf is exclusive or not.
/// A single book can only be on one exclusive shelf.
/// </summary>
public bool IsExclusive { get; private set; }
/// <summary>
/// The description of this user shelf.
/// </summary>
public string Description { get; private set; }
/// <summary>
/// Determines the default sort column of this user shelf.
/// </summary>
public string Sort { get; private set; }
/// <summary>
/// Determines the default sort order of this user shelf.
/// </summary>
// public Order? Order { get; private set; }
/// <summary>
/// Determines if this shelf will be featured on the user's profile.
/// </summary>
public bool IsFeatured { get; private set; }
/// <summary>
/// Determines if this user shelf is used in recommendations.
/// </summary>
public bool IsRecommendedFor { get; private set; }
/// <summary>
/// Determines if this user shelf is sticky.
/// </summary>
public bool Sticky { get; private set; }
/// <summary>
/// Determines if this user shelf is editable.
/// </summary>
public bool IsEditable { get; private set; }
/// <summary>
/// The shelf created date.
/// </summary>
public DateTime? CreatedAt { get; private set; }
/// <summary>
/// The shelf updated date.
/// </summary>
public DateTime? UpdatedAt { get; private set; }
public override void Parse(XElement element)
{
Id = element.ElementAsLong("id");
Name = element.ElementAsString("name");
BookCount = element.ElementAsInt("book_count");
Description = element.ElementAsString("description");
Sort = element.ElementAsString("sort");
IsExclusive = element.ElementAsBool("exclusive_flag");
IsFeatured = element.ElementAsBool("featured");
IsRecommendedFor = element.ElementAsBool("recommended_for");
Sticky = element.ElementAsBool("sticky");
IsEditable = element.ElementAsBool("editable_flag");
CreatedAt = element.ElementAsDateTime("created_at");
UpdatedAt = element.ElementAsDateTime("updated_at");
var orderElement = element.Element("order");
if (orderElement != null)
{
var orderValue = orderElement.Value;
if (!string.IsNullOrWhiteSpace(orderValue))
{
// if (orderValue == "a")
// {
// Order = Response.Order.Ascending;
// }
// else if (orderValue == "d")
// {
// Order = Response.Order.Descending;
// }
}
}
}
}
}
@@ -0,0 +1,168 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Xml.Linq;
namespace NzbDrone.Core.MetadataSource.Goodreads
{
/// <summary>
/// This class models a work as defined by the Goodreads API.
/// A work is the root concept of something written. Each book
/// is a published edition of a piece of work. Most work properties
/// are aggregate information over all the editions of a work.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class WorkResource : GoodreadsResource
{
public override string ElementName => "work";
/// <summary>
/// The Goodreads Id for this work.
/// </summary>
public long Id { get; private set; }
/// <summary>
/// The number of books for this work.
/// </summary>
public int BooksCount { get; private set; }
/// <summary>
/// The Goodreads Book Id that is considered the best version of this work.
/// Might not be populated. See the <see cref="BestBook"/> property for details, if provided.
/// </summary>
public long? BestBookId { get; private set; }
/// <summary>
/// The details for the best book of this work. Only populated
/// if Goodreads provides it as part of the response.
/// </summary>
public BestBookResource BestBook { get; private set; }
public long SeriesLinkId { get; private set; }
/// <summary>
/// If included in a list, this defines this work's position.
/// </summary>
public string UserPosition { get; private set; }
/// <summary>
/// The number of reviews of this work.
/// </summary>
public int ReviewsCount { get; private set; }
/// <summary>
/// The average rating of this work.
/// </summary>
public decimal AverageRating { get; private set; }
/// <summary>
/// The number of ratings of this work.
/// </summary>
public int RatingsCount { get; private set; }
/// <summary>
/// The number of text reviews of this work.
/// </summary>
public int TextReviewsCount { get; private set; }
/// <summary>
/// The original publication date of this work.
/// </summary>
public DateTime? OriginalPublicationDate { get; private set; }
/// <summary>
/// The original title of this work.
/// </summary>
public string OriginalTitle { get; private set; }
/// <summary>
/// The original language of this work.
/// </summary>
public int? OriginalLanguageId { get; private set; }
/// <summary>
/// The type of media for this work.
/// </summary>
public string MediaType { get; private set; }
/// <summary>
/// The distribution of all the ratings for this work.
/// A dictionary of star rating -> number of ratings.
/// </summary>
public IReadOnlyDictionary<int, int> RatingDistribution { get; private set; }
public override void Parse(XElement element)
{
Id = element.ElementAsLong("id");
var bestBookElement = element.Element("best_book");
if (bestBookElement != null)
{
BestBook = new BestBookResource();
BestBook.Parse(bestBookElement);
}
BestBookId = element.ElementAsNullableLong("best_book_id");
BooksCount = element.ElementAsInt("books_count");
ReviewsCount = element.ElementAsInt("reviews_count");
RatingsCount = element.ElementAsInt("ratings_count");
var average = element.ElementAsDecimal("average_rating");
if (average == 0 && RatingsCount > 0)
{
average = element.ElementAsDecimal("ratings_sum") / RatingsCount;
}
AverageRating = average;
TextReviewsCount = element.ElementAsInt("text_reviews_count");
// Merge the Goodreads publication fields into one date property
var originalPublicationYear = element.ElementAsInt("original_publication_year");
var originalPublicationMonth = element.ElementAsInt("original_publication_month");
var originalPublicationDay = element.ElementAsInt("original_publication_day");
if (originalPublicationYear != 0)
{
OriginalPublicationDate = new DateTime(originalPublicationYear, Math.Max(originalPublicationMonth, 1), Math.Max(originalPublicationDay, 1));
}
OriginalTitle = element.ElementAsString("original_title");
OriginalLanguageId = element.ElementAsNullableInt("original_language_id");
MediaType = element.ElementAsString("media_type");
// Parse out the rating distribution
var ratingDistributionElement = element.ElementAsString("rating_dist");
if (ratingDistributionElement != null)
{
var parts = ratingDistributionElement.Split('|');
if (parts.Length > 0)
{
var ratingDistribution = new Dictionary<int, int>();
var ratings = parts.Select(x => x.Split(':'))
.Where(x => x[0] != "total")
.OrderBy(x => x[0]);
foreach (var rating in ratings)
{
int star = 0, count = 0;
int.TryParse(rating[0], out star);
int.TryParse(rating[1], out count);
ratingDistribution.Add(star, count);
}
RatingDistribution = ratingDistribution;
}
}
}
internal void SetSeriesInfo(XElement element)
{
SeriesLinkId = element.ElementAsLong("id");
UserPosition = element.ElementAsString("user_position");
}
}
}
@@ -1,25 +0,0 @@
using System;
using System.Collections.Generic;
namespace NzbDrone.Core.MetadataSource.SkyHook.Resource
{
public class AlbumResource
{
public string ArtistId { get; set; }
public List<ArtistResource> Artists { get; set; }
public string Disambiguation { get; set; }
public string Overview { get; set; }
public string Id { get; set; }
public List<string> OldIds { get; set; }
public List<ImageResource> Images { get; set; }
public List<LinkResource> Links { get; set; }
public List<string> Genres { get; set; }
public RatingResource Rating { get; set; }
public DateTime? ReleaseDate { get; set; }
public List<ReleaseResource> Releases { get; set; }
public List<string> SecondaryTypes { get; set; }
public string Title { get; set; }
public string Type { get; set; }
public List<string> ReleaseStatuses { get; set; }
}
}
@@ -1,28 +0,0 @@
using System.Collections.Generic;
namespace NzbDrone.Core.MetadataSource.SkyHook.Resource
{
public class ArtistResource
{
public ArtistResource()
{
Albums = new List<AlbumResource>();
Genres = new List<string>();
}
public List<string> Genres { get; set; }
public string AristUrl { get; set; }
public string Overview { get; set; }
public string Type { get; set; }
public string Disambiguation { get; set; }
public string Id { get; set; }
public List<string> OldIds { get; set; }
public List<ImageResource> Images { get; set; }
public List<LinkResource> Links { get; set; }
public string ArtistName { get; set; }
public List<string> ArtistAliases { get; set; }
public List<AlbumResource> Albums { get; set; }
public string Status { get; set; }
public RatingResource Rating { get; set; }
}
}
@@ -1,9 +0,0 @@
namespace NzbDrone.Core.MetadataSource.SkyHook.Resource
{
public class EntityResource
{
public int Score { get; set; }
public ArtistResource Artist { get; set; }
public AlbumResource Album { get; set; }
}
}
@@ -1,10 +0,0 @@
namespace NzbDrone.Core.MetadataSource.SkyHook.Resource
{
public class ImageResource
{
public string CoverType { get; set; }
public string Url { get; set; }
public int Height { get; set; }
public int Width { get; set; }
}
}
@@ -1,8 +0,0 @@
namespace NzbDrone.Core.MetadataSource.SkyHook.Resource
{
public class LinkResource
{
public string Target { get; set; }
public string Type { get; set; }
}
}
@@ -1,9 +0,0 @@
namespace NzbDrone.Core.MetadataSource.SkyHook.Resource
{
public class MediumResource
{
public string Name { get; set; }
public string Format { get; set; }
public int Position { get; set; }
}
}
@@ -1,8 +0,0 @@
namespace NzbDrone.Core.MetadataSource.SkyHook.Resource
{
public class RatingResource
{
public int Count { get; set; }
public decimal Value { get; set; }
}
}
@@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
namespace NzbDrone.Core.MetadataSource.SkyHook.Resource
{
public class RecentUpdatesResource
{
public int Count { get; set; }
public bool Limited { get; set; }
public DateTime Since { get; set; }
public List<string> Items { get; set; }
}
}
@@ -1,20 +0,0 @@
using System;
using System.Collections.Generic;
namespace NzbDrone.Core.MetadataSource.SkyHook.Resource
{
public class ReleaseResource
{
public string Disambiguation { get; set; }
public List<string> Country { get; set; }
public DateTime? ReleaseDate { get; set; }
public string Id { get; set; }
public List<string> OldIds { get; set; }
public List<string> Label { get; set; }
public List<MediumResource> Media { get; set; }
public string Title { get; set; }
public string Status { get; set; }
public int TrackCount { get; set; }
public List<TrackResource> Tracks { get; set; }
}
}
@@ -1,23 +0,0 @@
using System.Collections.Generic;
namespace NzbDrone.Core.MetadataSource.SkyHook.Resource
{
public class TrackResource
{
public TrackResource()
{
}
public string ArtistId { get; set; }
public int DurationMs { get; set; }
public string Id { get; set; }
public List<string> OldIds { get; set; }
public string RecordingId { get; set; }
public List<string> OldRecordingIds { get; set; }
public string TrackName { get; set; }
public string TrackNumber { get; set; }
public int TrackPosition { get; set; }
public bool Explicit { get; set; }
public int MediumNumber { get; set; }
}
}
@@ -6,87 +6,63 @@ using NLog;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Exceptions;
using NzbDrone.Core.MediaCover;
using NzbDrone.Core.MetadataSource.SkyHook.Resource;
using NzbDrone.Core.Music;
using NzbDrone.Core.Profiles.Metadata;
namespace NzbDrone.Core.MetadataSource.SkyHook
{
public class SkyHookProxy : IProvideArtistInfo, ISearchForNewArtist, IProvideAlbumInfo, ISearchForNewAlbum, ISearchForNewEntity
public class SkyHookProxy : IProvideAuthorInfo, ISearchForNewAuthor, IProvideBookInfo, ISearchForNewBook, ISearchForNewEntity
{
private readonly IHttpClient _httpClient;
private readonly Logger _logger;
private readonly IArtistService _artistService;
private readonly IAlbumService _albumService;
private readonly IArtistService _authorService;
private readonly IAlbumService _bookService;
private readonly IMetadataRequestBuilder _requestBuilder;
private readonly IMetadataProfileService _metadataProfileService;
private readonly ICached<HashSet<string>> _cache;
private static readonly List<string> NonAudioMedia = new List<string> { "DVD", "DVD-Video", "Blu-ray", "HD-DVD", "VCD", "SVCD", "UMD", "VHS" };
private static readonly List<string> SkippedTracks = new List<string> { "[data track]" };
public SkyHookProxy(IHttpClient httpClient,
IMetadataRequestBuilder requestBuilder,
IArtistService artistService,
IArtistService authorService,
IAlbumService albumService,
Logger logger,
IMetadataProfileService metadataProfileService,
ICacheManager cacheManager)
{
_httpClient = httpClient;
_metadataProfileService = metadataProfileService;
_requestBuilder = requestBuilder;
_artistService = artistService;
_albumService = albumService;
_authorService = authorService;
_bookService = albumService;
_cache = cacheManager.GetCache<HashSet<string>>(GetType());
_logger = logger;
}
public HashSet<string> GetChangedArtists(DateTime startTime)
{
var startTimeUtc = (DateTimeOffset)DateTime.SpecifyKind(startTime, DateTimeKind.Utc);
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
.SetSegment("route", "recent/artist")
.AddQueryParam("since", startTimeUtc.ToUnixTimeSeconds())
.Build();
httpRequest.SuppressHttpError = true;
var httpResponse = _httpClient.Get<RecentUpdatesResource>(httpRequest);
if (httpResponse.Resource.Limited)
{
return null;
}
return new HashSet<string>(httpResponse.Resource.Items);
return null;
}
public Artist GetArtistInfo(string foreignArtistId, int metadataProfileId)
public Author GetAuthorInfo(string foreignAuthorId)
{
_logger.Debug("Getting Artist with ReadarrAPI.MetadataID of {0}", foreignArtistId);
_logger.Debug("Getting Author details ReadarrAPI.MetadataID of {0}", foreignAuthorId);
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
.SetSegment("route", "artist/" + foreignArtistId)
.Build();
.SetSegment("route", $"author/{foreignAuthorId}")
.Build();
httpRequest.AllowAutoRedirect = true;
httpRequest.SuppressHttpError = true;
var httpResponse = _httpClient.Get<ArtistResource>(httpRequest);
var httpResponse = _httpClient.Get<AuthorResource>(httpRequest);
if (httpResponse.HasHttpError)
{
if (httpResponse.StatusCode == HttpStatusCode.NotFound)
{
throw new ArtistNotFoundException(foreignArtistId);
throw new ArtistNotFoundException(foreignAuthorId);
}
else if (httpResponse.StatusCode == HttpStatusCode.BadRequest)
{
throw new BadRequestException(foreignArtistId);
throw new BadRequestException(foreignAuthorId);
}
else
{
@@ -94,15 +70,7 @@ namespace NzbDrone.Core.MetadataSource.SkyHook
}
}
var artist = new Artist();
artist.Metadata = MapArtistMetadata(httpResponse.Resource);
artist.CleanName = Parser.Parser.CleanArtistName(artist.Metadata.Value.Name);
artist.SortName = Parser.Parser.NormalizeTitle(artist.Metadata.Value.Name);
artist.Albums = FilterAlbums(httpResponse.Resource.Albums, metadataProfileId)
.Select(x => MapAlbum(x, null)).ToList();
return artist;
return MapAuthor(httpResponse.Resource);
}
public HashSet<string> GetChangedAlbums(DateTime startTime)
@@ -112,59 +80,31 @@ namespace NzbDrone.Core.MetadataSource.SkyHook
private HashSet<string> GetChangedAlbumsUncached(DateTime startTime)
{
var startTimeUtc = (DateTimeOffset)DateTime.SpecifyKind(startTime, DateTimeKind.Utc);
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
.SetSegment("route", "recent/album")
.AddQueryParam("since", startTimeUtc.ToUnixTimeSeconds())
.Build();
httpRequest.SuppressHttpError = true;
var httpResponse = _httpClient.Get<RecentUpdatesResource>(httpRequest);
if (httpResponse.Resource.Limited)
{
return null;
}
return new HashSet<string>(httpResponse.Resource.Items);
return null;
}
public IEnumerable<AlbumResource> FilterAlbums(IEnumerable<AlbumResource> albums, int metadataProfileId)
public Tuple<string, Book, List<AuthorMetadata>> GetBookInfo(string foreignBookId)
{
var metadataProfile = _metadataProfileService.Exists(metadataProfileId) ? _metadataProfileService.Get(metadataProfileId) : _metadataProfileService.All().First();
var primaryTypes = new HashSet<string>(metadataProfile.PrimaryAlbumTypes.Where(s => s.Allowed).Select(s => s.PrimaryAlbumType.Name));
var secondaryTypes = new HashSet<string>(metadataProfile.SecondaryAlbumTypes.Where(s => s.Allowed).Select(s => s.SecondaryAlbumType.Name));
var releaseStatuses = new HashSet<string>(metadataProfile.ReleaseStatuses.Where(s => s.Allowed).Select(s => s.ReleaseStatus.Name));
return albums.Where(album => primaryTypes.Contains(album.Type) &&
((!album.SecondaryTypes.Any() && secondaryTypes.Contains("Studio")) ||
album.SecondaryTypes.Any(x => secondaryTypes.Contains(x))) &&
album.ReleaseStatuses.Any(x => releaseStatuses.Contains(x)));
}
public Tuple<string, Album, List<ArtistMetadata>> GetAlbumInfo(string foreignAlbumId)
{
_logger.Debug("Getting Album with ReadarrAPI.MetadataID of {0}", foreignAlbumId);
_logger.Debug("Getting Book with ReadarrAPI.MetadataID of {0}", foreignBookId);
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
.SetSegment("route", "album/" + foreignAlbumId)
.Build();
.SetSegment("route", $"book/{foreignBookId}")
.Build();
httpRequest.AllowAutoRedirect = true;
httpRequest.SuppressHttpError = true;
var httpResponse = _httpClient.Get<AlbumResource>(httpRequest);
var httpResponse = _httpClient.Get<BookResource>(httpRequest);
if (httpResponse.HasHttpError)
{
if (httpResponse.StatusCode == HttpStatusCode.NotFound)
{
throw new AlbumNotFoundException(foreignAlbumId);
throw new AlbumNotFoundException(foreignBookId);
}
else if (httpResponse.StatusCode == HttpStatusCode.BadRequest)
{
throw new BadRequestException(foreignAlbumId);
throw new BadRequestException(foreignBookId);
}
else
{
@@ -172,60 +112,75 @@ namespace NzbDrone.Core.MetadataSource.SkyHook
}
}
var artists = httpResponse.Resource.Artists.Select(MapArtistMetadata).ToList();
var artistDict = artists.ToDictionary(x => x.ForeignArtistId, x => x);
var album = MapAlbum(httpResponse.Resource, artistDict);
album.ArtistMetadata = artistDict[httpResponse.Resource.ArtistId];
var b = httpResponse.Resource;
var book = MapBook(b);
return new Tuple<string, Album, List<ArtistMetadata>>(httpResponse.Resource.ArtistId, album, artists);
var authors = httpResponse.Resource.AuthorMetadata.SelectList(MapAuthor);
var authorid = GetAuthorId(b);
book.AuthorMetadata = authors.First(x => x.ForeignAuthorId == authorid);
return new Tuple<string, Book, List<AuthorMetadata>>(authorid, book, authors);
}
public List<Artist> SearchForNewArtist(string title)
public List<Author> SearchForNewAuthor(string title)
{
var books = SearchForNewBook(title, null);
return books.Select(x => x.Author.Value).ToList();
}
public List<Book> SearchForNewBook(string title, string artist)
{
try
{
var lowerTitle = title.ToLowerInvariant();
if (lowerTitle.StartsWith("readarr:") || lowerTitle.StartsWith("readarrid:") || lowerTitle.StartsWith("mbid:"))
var split = lowerTitle.Split(':');
var prefix = split[0];
if (split.Length == 2 && new[] { "readarr", "readarrid", "goodreads", "isbn", "asin" }.Contains(prefix))
{
var slug = lowerTitle.Split(':')[1].Trim();
var slug = split[1].Trim();
Guid searchGuid;
bool isValid = Guid.TryParse(slug, out searchGuid);
if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace) || isValid == false)
if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace))
{
return new List<Artist>();
return new List<Book>();
}
try
if (prefix == "goodreads" || prefix == "readarr" || prefix == "readarrid")
{
var existingArtist = _artistService.FindById(searchGuid.ToString());
if (existingArtist != null)
var isValid = int.TryParse(slug, out var searchId);
if (!isValid)
{
return new List<Artist> { existingArtist };
return new List<Book>();
}
var metadataProfile = _metadataProfileService.All().First().Id; //Change this to Use last Used profile?
return new List<Artist> { GetArtistInfo(searchGuid.ToString(), metadataProfile) };
return SearchByGoodreadsId(searchId);
}
catch (ArtistNotFoundException)
else if (prefix == "isbn")
{
return new List<Artist>();
return SearchByIsbn(slug);
}
else if (prefix == "asin")
{
return SearchByAsin(slug);
}
}
var q = title.ToLower().Trim();
if (artist != null)
{
q += " " + artist;
}
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
.SetSegment("route", "search")
.AddQueryParam("type", "artist")
.AddQueryParam("query", title.ToLower().Trim())
.Build();
.SetSegment("route", "search")
.AddQueryParam("q", q)
.Build();
var httpResponse = _httpClient.Get<List<ArtistResource>>(httpRequest);
var result = _httpClient.Get<BookSearchResource>(httpRequest);
return httpResponse.Resource.SelectList(MapSearchResult);
return MapSearchResult(result.Resource);
}
catch (HttpException)
{
@@ -238,382 +193,263 @@ namespace NzbDrone.Core.MetadataSource.SkyHook
}
}
public List<Album> SearchForNewAlbum(string title, string artist)
public List<Book> SearchByIsbn(string isbn)
{
return SearchByAlternateId("isbn", isbn);
}
public List<Book> SearchByAsin(string asin)
{
return SearchByAlternateId("asin", asin.ToUpper());
}
public List<Book> SearchByGoodreadsId(int goodreadsId)
{
return SearchByAlternateId("goodreads", goodreadsId.ToString());
}
private List<Book> SearchByAlternateId(string type, string id)
{
try
{
var lowerTitle = title.ToLowerInvariant();
if (lowerTitle.StartsWith("readarr:") || lowerTitle.StartsWith("readarrid:") || lowerTitle.StartsWith("mbid:"))
{
var slug = lowerTitle.Split(':')[1].Trim();
Guid searchGuid;
bool isValid = Guid.TryParse(slug, out searchGuid);
if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace) || isValid == false)
{
return new List<Album>();
}
try
{
var existingAlbum = _albumService.FindById(searchGuid.ToString());
if (existingAlbum == null)
{
var data = GetAlbumInfo(searchGuid.ToString());
var album = data.Item2;
album.Artist = _artistService.FindById(data.Item1) ?? new Artist
{
Metadata = data.Item3.Single(x => x.ForeignArtistId == data.Item1)
};
return new List<Album> { album };
}
existingAlbum.Artist = _artistService.GetArtist(existingAlbum.ArtistId);
return new List<Album> { existingAlbum };
}
catch (ArtistNotFoundException)
{
return new List<Album>();
}
}
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
.SetSegment("route", "search")
.AddQueryParam("type", "album")
.AddQueryParam("query", title.ToLower().Trim())
.AddQueryParam("artist", artist.IsNotNullOrWhiteSpace() ? artist.ToLower().Trim() : string.Empty)
.AddQueryParam("includeTracks", "1")
.Build();
.SetSegment("route", $"book/{type}/{id}")
.Build();
var httpResponse = _httpClient.Get<List<AlbumResource>>(httpRequest);
var httpResponse = _httpClient.Get<BookSearchResource>(httpRequest);
return httpResponse.Resource.SelectList(MapSearchResult);
var result = _httpClient.Get<BookSearchResource>(httpRequest);
return MapSearchResult(result.Resource);
}
catch (HttpException)
{
throw new SkyHookException("Search for '{0}' failed. Unable to communicate with ReadarrAPI.", title);
throw new SkyHookException("Search for {0} '{1}' failed. Unable to communicate with ReadarrAPI.", type, id);
}
catch (Exception ex)
{
_logger.Warn(ex, ex.Message);
throw new SkyHookException("Search for '{0}' failed. Invalid response received from ReadarrAPI.", title);
throw new SkyHookException("Search for {0 }'{1}' failed. Invalid response received from ReadarrAPI.", type, id);
}
}
public List<Album> SearchForNewAlbumByRecordingIds(List<string> recordingIds)
public List<Book> SearchForNewAlbumByRecordingIds(List<string> recordingIds)
{
var ids = recordingIds.Where(x => x.IsNotNullOrWhiteSpace()).Distinct();
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
.SetSegment("route", "search/fingerprint")
.Build();
httpRequest.SetContent(ids.ToJson());
httpRequest.Headers.ContentType = "application/json";
var httpResponse = _httpClient.Post<List<AlbumResource>>(httpRequest);
return httpResponse.Resource.SelectList(MapSearchResult);
return null;
}
public List<object> SearchForNewEntity(string title)
{
try
var books = SearchForNewBook(title, null);
var result = new List<object>();
foreach (var book in books)
{
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
.SetSegment("route", "search")
.AddQueryParam("type", "all")
.AddQueryParam("query", title.ToLower().Trim())
.Build();
var author = book.Author.Value;
var httpResponse = _httpClient.Get<List<EntityResource>>(httpRequest);
return httpResponse.Resource.SelectList(MapSearchResult);
}
catch (HttpException)
{
throw new SkyHookException("Search for '{0}' failed. Unable to communicate with ReadarrAPI.", title);
}
catch (Exception ex)
{
_logger.Warn(ex, ex.Message);
throw new SkyHookException("Search for '{0}' failed. Invalid response received from ReadarrAPI.", title);
}
}
private Artist MapSearchResult(ArtistResource resource)
{
var artist = _artistService.FindById(resource.Id);
if (artist == null)
{
artist = new Artist();
artist.Metadata = MapArtistMetadata(resource);
}
return artist;
}
private Album MapSearchResult(AlbumResource resource)
{
var artists = resource.Artists.Select(MapArtistMetadata).ToDictionary(x => x.ForeignArtistId, x => x);
var artist = _artistService.FindById(resource.ArtistId);
if (artist == null)
{
artist = new Artist();
artist.Metadata = artists[resource.ArtistId];
}
var album = _albumService.FindById(resource.Id) ?? MapAlbum(resource, artists);
album.Artist = artist;
album.ArtistMetadata = artist.Metadata.Value;
return album;
}
private object MapSearchResult(EntityResource resource)
{
if (resource.Artist != null)
{
return MapSearchResult(resource.Artist);
}
else
{
return MapSearchResult(resource.Album);
}
}
private static Album MapAlbum(AlbumResource resource, Dictionary<string, ArtistMetadata> artistDict)
{
Album album = new Album();
album.ForeignAlbumId = resource.Id;
album.OldForeignAlbumIds = resource.OldIds;
album.Title = resource.Title;
album.Overview = resource.Overview;
album.Disambiguation = resource.Disambiguation;
album.ReleaseDate = resource.ReleaseDate;
if (resource.Images != null)
{
album.Images = resource.Images.Select(MapImage).ToList();
}
album.AlbumType = resource.Type;
album.SecondaryTypes = resource.SecondaryTypes.Select(MapSecondaryTypes).ToList();
album.Ratings = MapRatings(resource.Rating);
album.Links = resource.Links?.Select(MapLink).ToList();
album.Genres = resource.Genres;
album.CleanTitle = Parser.Parser.CleanArtistName(album.Title);
if (resource.Releases != null)
{
album.AlbumReleases = resource.Releases.Select(x => MapRelease(x, artistDict)).Where(x => x.TrackCount > 0).ToList();
// Monitor the release with most tracks
var mostTracks = album.AlbumReleases.Value.OrderByDescending(x => x.TrackCount).FirstOrDefault();
if (mostTracks != null)
if (!result.Contains(author))
{
mostTracks.Monitored = true;
result.Add(author);
}
}
else
{
album.AlbumReleases = new List<AlbumRelease>();
result.Add(book);
}
album.AnyReleaseOk = true;
return album;
return result;
}
private static AlbumRelease MapRelease(ReleaseResource resource, Dictionary<string, ArtistMetadata> artistDict)
private Author MapAuthor(AuthorResource resource)
{
AlbumRelease release = new AlbumRelease();
release.ForeignReleaseId = resource.Id;
release.OldForeignReleaseIds = resource.OldIds;
release.Title = resource.Title;
release.Status = resource.Status;
release.Label = resource.Label;
release.Disambiguation = resource.Disambiguation;
release.Country = resource.Country;
release.ReleaseDate = resource.ReleaseDate;
var metadata = MapAuthor(resource.AuthorMetadata.First(x => x.ForeignId == resource.ForeignId));
// Get the complete set of media/tracks returned by the API, adding missing media if necessary
var allMedia = resource.Media.Select(MapMedium).ToList();
var allTracks = resource.Tracks.Select(x => MapTrack(x, artistDict));
if (!allMedia.Any())
var books = resource.Books
.Where(x => GetAuthorId(x) == resource.ForeignId)
.Select(MapBook)
.ToList();
books.ForEach(x => x.AuthorMetadata = metadata);
var series = resource.Series.Select(MapSeries).ToList();
MapSeriesLinks(series, books, resource);
var result = new Author
{
foreach (int n in allTracks.Select(x => x.MediumNumber).Distinct())
Metadata = metadata,
CleanName = Parser.Parser.CleanArtistName(metadata.Name),
SortName = Parser.Parser.NormalizeTitle(metadata.Name),
Books = books,
Series = series
};
return result;
}
private void MapSeriesLinks(List<Series> series, List<Book> books, BulkResource resource)
{
var bookDict = books.ToDictionary(x => x.ForeignBookId);
var seriesDict = series.ToDictionary(x => x.ForeignSeriesId);
// only take series where there are some works
foreach (var s in resource.Series.Where(x => x.BookLinks.Any()))
{
if (seriesDict.TryGetValue(s.ForeignId, out var curr))
{
allMedia.Add(new Medium { Name = "Unknown", Number = n, Format = "Unknown" });
curr.LinkItems = s.BookLinks.Where(x => bookDict.ContainsKey(x.BookId)).Select(l => new SeriesBookLink
{
Book = bookDict[l.BookId],
Series = curr,
IsPrimary = l.Primary
}).ToList();
}
}
// Skip non-audio media
var audioMediaNumbers = allMedia.Where(x => !NonAudioMedia.Contains(x.Format)).Select(x => x.Number);
// Get tracks on the audio media and omit any that are skipped
release.Tracks = allTracks.Where(x => audioMediaNumbers.Contains(x.MediumNumber) && !SkippedTracks.Contains(x.Title)).ToList();
release.TrackCount = release.Tracks.Value.Count;
// Only include the media that contain the tracks we have selected
var usedMediaNumbers = release.Tracks.Value.Select(track => track.MediumNumber);
release.Media = allMedia.Where(medium => usedMediaNumbers.Contains(medium.Number)).ToList();
release.Duration = release.Tracks.Value.Sum(x => x.Duration);
return release;
}
private static Medium MapMedium(MediumResource resource)
{
Medium medium = new Medium
foreach (var b in resource.Books)
{
Name = resource.Name,
Number = resource.Position,
Format = resource.Format
};
return medium;
}
private static Track MapTrack(TrackResource resource, Dictionary<string, ArtistMetadata> artistDict)
{
Track track = new Track
{
ArtistMetadata = artistDict[resource.ArtistId],
Title = resource.TrackName,
ForeignTrackId = resource.Id,
OldForeignTrackIds = resource.OldIds,
ForeignRecordingId = resource.RecordingId,
OldForeignRecordingIds = resource.OldRecordingIds,
TrackNumber = resource.TrackNumber,
AbsoluteTrackNumber = resource.TrackPosition,
Duration = resource.DurationMs,
MediumNumber = resource.MediumNumber
};
return track;
}
private static ArtistMetadata MapArtistMetadata(ArtistResource resource)
{
ArtistMetadata artist = new ArtistMetadata();
artist.Name = resource.ArtistName;
artist.Aliases = resource.ArtistAliases;
artist.ForeignArtistId = resource.Id;
artist.OldForeignArtistIds = resource.OldIds;
artist.Genres = resource.Genres;
artist.Overview = resource.Overview;
artist.Disambiguation = resource.Disambiguation;
artist.Type = resource.Type;
artist.Status = MapArtistStatus(resource.Status);
artist.Ratings = MapRatings(resource.Rating);
artist.Images = resource.Images?.Select(MapImage).ToList();
artist.Links = resource.Links?.Select(MapLink).ToList();
return artist;
}
private static ArtistStatusType MapArtistStatus(string status)
{
if (status == null)
{
return ArtistStatusType.Continuing;
if (bookDict.TryGetValue(b.ForeignId, out var curr))
{
curr.SeriesLinks = b.SeriesLinks.Where(l => seriesDict.ContainsKey(l.SeriesId)).Select(l => new SeriesBookLink
{
Series = seriesDict[l.SeriesId],
Position = l.Position,
Book = curr
}).ToList();
}
}
if (status.Equals("ended", StringComparison.InvariantCultureIgnoreCase))
{
return ArtistStatusType.Ended;
}
return ArtistStatusType.Continuing;
_ = series.SelectMany(x => x.LinkItems.Value)
.Join(books.SelectMany(x => x.SeriesLinks.Value),
sl => Tuple.Create(sl.Series.Value.ForeignSeriesId, sl.Book.Value.ForeignBookId),
bl => Tuple.Create(bl.Series.Value.ForeignSeriesId, bl.Book.Value.ForeignBookId),
(sl, bl) =>
{
sl.Position = bl.Position;
bl.IsPrimary = sl.IsPrimary;
return sl;
}).ToList();
}
private static Ratings MapRatings(RatingResource rating)
private static AuthorMetadata MapAuthor(AuthorSummaryResource resource)
{
if (rating == null)
var author = new AuthorMetadata
{
return new Ratings();
}
return new Ratings
{
Votes = rating.Count,
Value = rating.Value
ForeignAuthorId = resource.ForeignId,
GoodreadsId = resource.GoodreadsId,
TitleSlug = resource.TitleSlug,
Name = resource.Name.CleanSpaces(),
Overview = resource.Description,
Ratings = new Ratings { Votes = resource.RatingsCount, Value = (decimal)resource.AverageRating }
};
}
private static MediaCover.MediaCover MapImage(ImageResource arg)
{
return new MediaCover.MediaCover
if (resource.ImageUrl.IsNotNullOrWhiteSpace())
{
Url = arg.Url,
CoverType = MapCoverType(arg.CoverType)
};
}
private static Links MapLink(LinkResource arg)
{
return new Links
{
Url = arg.Target,
Name = arg.Type
};
}
private static MediaCoverTypes MapCoverType(string coverType)
{
switch (coverType.ToLower())
{
case "poster":
return MediaCoverTypes.Poster;
case "banner":
return MediaCoverTypes.Banner;
case "fanart":
return MediaCoverTypes.Fanart;
case "cover":
return MediaCoverTypes.Cover;
case "disc":
return MediaCoverTypes.Disc;
case "logo":
return MediaCoverTypes.Logo;
default:
return MediaCoverTypes.Unknown;
author.Images.Add(new MediaCover.MediaCover
{
Url = resource.ImageUrl,
CoverType = MediaCoverTypes.Poster
});
}
author.Links.Add(new Links { Url = resource.WebUrl, Name = "Goodreads" });
return author;
}
public static SecondaryAlbumType MapSecondaryTypes(string albumType)
private static Series MapSeries(SeriesResource resource)
{
switch (albumType.ToLowerInvariant())
var series = new Series
{
case "compilation":
return SecondaryAlbumType.Compilation;
case "soundtrack":
return SecondaryAlbumType.Soundtrack;
case "spokenword":
return SecondaryAlbumType.Spokenword;
case "interview":
return SecondaryAlbumType.Interview;
case "audiobook":
return SecondaryAlbumType.Audiobook;
case "live":
return SecondaryAlbumType.Live;
case "remix":
return SecondaryAlbumType.Remix;
case "dj-mix":
return SecondaryAlbumType.DJMix;
case "mixtape/street":
return SecondaryAlbumType.Mixtape;
case "demo":
return SecondaryAlbumType.Demo;
default:
return SecondaryAlbumType.Studio;
ForeignSeriesId = resource.ForeignId,
Title = resource.Title,
Description = resource.Description
};
return series;
}
private static Book MapBook(BookResource resource)
{
var book = new Book
{
ForeignBookId = resource.ForeignId,
ForeignWorkId = resource.WorkForeignId,
GoodreadsId = resource.GoodreadsId,
TitleSlug = resource.TitleSlug,
Isbn13 = resource.Isbn13,
Asin = resource.Asin,
Title = resource.Title.CleanSpaces(),
Language = resource.Language,
Publisher = resource.Publisher,
CleanTitle = Parser.Parser.CleanArtistName(resource.Title),
Overview = resource.Description,
ReleaseDate = resource.ReleaseDate,
Ratings = new Ratings { Votes = resource.RatingCount, Value = (decimal)resource.AverageRating }
};
if (resource.ImageUrl.IsNotNullOrWhiteSpace())
{
book.Images.Add(new MediaCover.MediaCover
{
Url = resource.ImageUrl,
CoverType = MediaCoverTypes.Cover
});
}
book.Links.Add(new Links { Url = resource.WebUrl, Name = "Goodreads" });
return book;
}
private List<Book> MapSearchResult(BookSearchResource resource)
{
var metadata = resource.AuthorMetadata.SelectList(MapAuthor).ToDictionary(x => x.ForeignAuthorId);
var result = new List<Book>();
foreach (var b in resource.Books)
{
var book = _bookService.FindById(b.ForeignId);
if (book == null)
{
book = MapBook(b);
var authorid = GetAuthorId(b);
if (authorid == null)
{
continue;
}
var author = _authorService.FindById(authorid);
if (author == null)
{
var authorMetadata = metadata[authorid];
author = new Author
{
CleanName = Parser.Parser.CleanArtistName(authorMetadata.Name),
Metadata = authorMetadata
};
}
book.Author = author;
book.AuthorMetadata = author.Metadata.Value;
}
result.Add(book);
}
var seriesList = resource.Series.Select(MapSeries).ToList();
MapSeriesLinks(seriesList, result, resource);
return result;
}
private string GetAuthorId(BookResource b)
{
return b.Contributors.FirstOrDefault()?.ForeignId;
}
}
}
@@ -0,0 +1,7 @@
namespace NzbDrone.Core.MetadataSource.SkyHook
{
public class AuthorResource : BulkResource
{
public string ForeignId { get; set; }
}
}
@@ -0,0 +1,19 @@
namespace NzbDrone.Core.MetadataSource.SkyHook
{
public class AuthorSummaryResource
{
public string ForeignId { get; set; }
public int GoodreadsId { get; set; }
public string TitleSlug { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string ImageUrl { get; set; }
public string ProfileUri { get; set; }
public string WebUrl { get; set; }
public int ReviewCount { get; set; }
public int RatingsCount { get; set; }
public double AverageRating { get; set; }
}
}
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
namespace NzbDrone.Core.MetadataSource.SkyHook
{
public class BookResource
{
public string ForeignId { get; set; }
public int GoodreadsId { get; set; }
public string TitleSlug { get; set; }
public string Asin { get; set; }
public string Description { get; set; }
public string Isbn13 { get; set; }
public long Rvn { get; set; }
public string Title { get; set; }
public string Publisher { get; set; }
public string Language { get; set; }
public string DisplayGroup { get; set; }
public string ImageUrl { get; set; }
public string KindleMappingStatus { get; set; }
public string Marketplace { get; set; }
public int? NumPages { get; set; }
public int ReviewsCount { get; set; }
public int RatingCount { get; set; }
public double AverageRating { get; set; }
public IList<BookSeriesLinkResource> SeriesLinks { get; set; } = new List<BookSeriesLinkResource>();
public string WebUrl { get; set; }
public string WorkForeignId { get; set; }
public DateTime? ReleaseDate { get; set; }
public List<ContributorResource> Contributors { get; set; } = new List<ContributorResource>();
public List<AuthorSummaryResource> AuthorMetadata { get; set; } = new List<AuthorSummaryResource>();
}
public class BookSeriesLinkResource
{
public string SeriesId { get; set; }
public string Position { get; set; }
}
}
@@ -0,0 +1,6 @@
namespace NzbDrone.Core.MetadataSource.SkyHook
{
public class BookSearchResource : BulkResource
{
}
}
@@ -0,0 +1,11 @@
using System.Collections.Generic;
namespace NzbDrone.Core.MetadataSource.SkyHook
{
public class BulkResource
{
public List<AuthorSummaryResource> AuthorMetadata { get; set; } = new List<AuthorSummaryResource>();
public List<BookResource> Books { get; set; }
public List<SeriesResource> Series { get; set; }
}
}
@@ -0,0 +1,8 @@
namespace NzbDrone.Core.MetadataSource.SkyHook
{
public class ContributorResource
{
public string ForeignId { get; set; }
public string Role { get; set; }
}
}
@@ -0,0 +1,19 @@
using System.Collections.Generic;
namespace NzbDrone.Core.MetadataSource.SkyHook
{
public class SeriesResource
{
public string ForeignId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public List<SeriesBookLinkResource> BookLinks { get; set; }
}
public class SeriesBookLinkResource
{
public string BookId { get; set; }
public bool Primary { get; set; }
}
}