parent
225f0b310a
commit
6bdfe01fbc
@ -0,0 +1,14 @@
|
||||
using FluentMigrator;
|
||||
using NzbDrone.Core.Datastore.Migration.Framework;
|
||||
|
||||
namespace NzbDrone.Core.Datastore.Migration
|
||||
{
|
||||
[Migration(16)]
|
||||
public class AddRelatedBooks : NzbDroneMigrationBase
|
||||
{
|
||||
protected override void MainDbUpgrade()
|
||||
{
|
||||
Alter.Table("Books").AddColumn("RelatedBooks").AsString().WithDefaultValue("[]");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
using System.Net;
|
||||
using NzbDrone.Core.Exceptions;
|
||||
|
||||
namespace NzbDrone.Core.MetadataSource.BookInfo
|
||||
{
|
||||
public class BookInfoException : NzbDroneClientException
|
||||
{
|
||||
public BookInfoException(string message)
|
||||
: base(HttpStatusCode.ServiceUnavailable, message)
|
||||
{
|
||||
}
|
||||
|
||||
public BookInfoException(string message, params object[] args)
|
||||
: base(HttpStatusCode.ServiceUnavailable, message, args)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,315 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Cache;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Core.Books;
|
||||
using NzbDrone.Core.Exceptions;
|
||||
using NzbDrone.Core.MediaCover;
|
||||
|
||||
namespace NzbDrone.Core.MetadataSource.BookInfo
|
||||
{
|
||||
public class BookInfoProxy : IProvideAuthorInfo
|
||||
{
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly Logger _logger;
|
||||
private readonly IMetadataRequestBuilder _requestBuilder;
|
||||
private readonly ICached<HashSet<string>> _cache;
|
||||
|
||||
public BookInfoProxy(IHttpClient httpClient,
|
||||
IMetadataRequestBuilder requestBuilder,
|
||||
Logger logger,
|
||||
ICacheManager cacheManager)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_requestBuilder = requestBuilder;
|
||||
_cache = cacheManager.GetCache<HashSet<string>>(GetType());
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public HashSet<string> GetChangedAuthors(DateTime startTime)
|
||||
{
|
||||
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
|
||||
.SetSegment("route", "author/changed")
|
||||
.AddQueryParam("since", startTime.ToString("o"))
|
||||
.Build();
|
||||
|
||||
httpRequest.SuppressHttpError = true;
|
||||
|
||||
var httpResponse = _httpClient.Get<RecentUpdatesResource>(httpRequest);
|
||||
|
||||
if (httpResponse.Resource.Limited)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new HashSet<string>(httpResponse.Resource.Ids.Select(x => x.ToString()));
|
||||
}
|
||||
|
||||
public Author GetAuthorInfo(string foreignAuthorId, bool useCache = false, bool includeBooks = true)
|
||||
{
|
||||
_logger.Debug("Getting Author details GoodreadsId of {0}", foreignAuthorId);
|
||||
|
||||
return PollAuthor(foreignAuthorId, includeBooks);
|
||||
}
|
||||
|
||||
private Author PollAuthor(string foreignAuthorId, bool includeBooks)
|
||||
{
|
||||
AuthorResource resource = null;
|
||||
|
||||
for (var i = 0; i < 60; i++)
|
||||
{
|
||||
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
|
||||
.SetSegment("route", $"author/{foreignAuthorId}")
|
||||
.Build();
|
||||
|
||||
httpRequest.AllowAutoRedirect = true;
|
||||
httpRequest.SuppressHttpError = true;
|
||||
|
||||
var httpResponse = _httpClient.Get<AuthorResource>(httpRequest);
|
||||
|
||||
if (httpResponse.HasHttpError)
|
||||
{
|
||||
if (httpResponse.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
throw new AuthorNotFoundException(foreignAuthorId);
|
||||
}
|
||||
else if (httpResponse.StatusCode == HttpStatusCode.BadRequest)
|
||||
{
|
||||
throw new BadRequestException(foreignAuthorId);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new HttpException(httpRequest, httpResponse);
|
||||
}
|
||||
}
|
||||
|
||||
resource = httpResponse.Resource;
|
||||
|
||||
if (resource.Works != null || !includeBooks)
|
||||
{
|
||||
resource.Works ??= new List<WorkResource>();
|
||||
resource.Series ??= new List<SeriesResource>();
|
||||
break;
|
||||
}
|
||||
|
||||
Thread.Sleep(2000);
|
||||
}
|
||||
|
||||
if (resource?.Works == null)
|
||||
{
|
||||
throw new BookInfoException($"Failed to get works for {foreignAuthorId}");
|
||||
}
|
||||
|
||||
return MapAuthor(resource);
|
||||
}
|
||||
|
||||
public Author GetAuthorAndBooks(string foreignAuthorId, double minPopularity = 0)
|
||||
{
|
||||
return GetAuthorInfo(foreignAuthorId);
|
||||
}
|
||||
|
||||
public HashSet<string> GetChangedBooks(DateTime startTime)
|
||||
{
|
||||
return _cache.Get("ChangedBooks", () => GetChangedBooksUncached(startTime), TimeSpan.FromMinutes(30));
|
||||
}
|
||||
|
||||
private HashSet<string> GetChangedBooksUncached(DateTime startTime)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public Tuple<string, Book, List<AuthorMetadata>> GetBookInfo(string foreignBookId)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
private Author MapAuthor(AuthorResource resource)
|
||||
{
|
||||
var metadata = new AuthorMetadata
|
||||
{
|
||||
ForeignAuthorId = resource.ForeignId.ToString(),
|
||||
TitleSlug = resource.ForeignId.ToString(),
|
||||
Name = resource.Name.CleanSpaces(),
|
||||
Overview = resource.Description,
|
||||
Ratings = new Ratings { Votes = resource.RatingCount, Value = (decimal)resource.AverageRating },
|
||||
Status = AuthorStatusType.Continuing
|
||||
};
|
||||
|
||||
metadata.SortName = metadata.Name.ToLower();
|
||||
metadata.NameLastFirst = metadata.Name.ToLastFirst();
|
||||
metadata.SortNameLastFirst = metadata.NameLastFirst.ToLower();
|
||||
|
||||
if (resource.ImageUrl.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
metadata.Images.Add(new MediaCover.MediaCover
|
||||
{
|
||||
Url = resource.ImageUrl,
|
||||
CoverType = MediaCoverTypes.Poster
|
||||
});
|
||||
}
|
||||
|
||||
if (resource.Url.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
metadata.Links.Add(new Links { Url = resource.Url, Name = "Goodreads" });
|
||||
}
|
||||
|
||||
var books = resource.Works
|
||||
.Where(x => x.ForeignId > 0 && 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
|
||||
{
|
||||
Metadata = metadata,
|
||||
CleanName = Parser.Parser.CleanAuthorName(metadata.Name),
|
||||
Books = books,
|
||||
Series = series
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void MapSeriesLinks(List<Series> series, List<Book> books, AuthorResource 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.LinkItems.Any()))
|
||||
{
|
||||
if (seriesDict.TryGetValue(s.ForeignId.ToString(), out var curr))
|
||||
{
|
||||
curr.LinkItems = s.LinkItems.Where(x => x.ForeignWorkId.IsNotNullOrWhiteSpace() && bookDict.ContainsKey(x.ForeignWorkId.ToString())).Select(l => new SeriesBookLink
|
||||
{
|
||||
Book = bookDict[l.ForeignWorkId.ToString()],
|
||||
Series = curr,
|
||||
IsPrimary = l.Primary,
|
||||
Position = l.PositionInSeries
|
||||
}).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Series MapSeries(SeriesResource resource)
|
||||
{
|
||||
var series = new Series
|
||||
{
|
||||
ForeignSeriesId = resource.ForeignId.ToString(),
|
||||
Title = resource.Title,
|
||||
Description = resource.Description
|
||||
};
|
||||
|
||||
return series;
|
||||
}
|
||||
|
||||
private static Book MapBook(WorkResource resource)
|
||||
{
|
||||
var book = new Book
|
||||
{
|
||||
ForeignBookId = resource.ForeignId.ToString(),
|
||||
Title = resource.Title,
|
||||
TitleSlug = resource.ForeignId.ToString(),
|
||||
CleanTitle = Parser.Parser.CleanAuthorName(resource.Title),
|
||||
ReleaseDate = resource.ReleaseDate,
|
||||
Genres = resource.Genres,
|
||||
RelatedBooks = resource.RelatedWorks
|
||||
};
|
||||
|
||||
book.Links.Add(new Links { Url = resource.Url, Name = "Goodreads Editions" });
|
||||
|
||||
if (resource.Books != null)
|
||||
{
|
||||
book.Editions = resource.Books.Select(x => MapEdition(x)).ToList();
|
||||
|
||||
// monitor the most rated release
|
||||
var mostPopular = book.Editions.Value.OrderByDescending(x => x.Ratings.Votes).FirstOrDefault();
|
||||
if (mostPopular != null)
|
||||
{
|
||||
mostPopular.Monitored = true;
|
||||
|
||||
// fix work title if missing
|
||||
if (book.Title.IsNullOrWhiteSpace())
|
||||
{
|
||||
book.Title = mostPopular.Title;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
book.Editions = new List<Edition>();
|
||||
}
|
||||
|
||||
Debug.Assert(!book.Editions.Value.Any() || book.Editions.Value.Count(x => x.Monitored) == 1, "one edition monitored");
|
||||
|
||||
book.AnyEditionOk = true;
|
||||
|
||||
var ratingCount = book.Editions.Value.Sum(x => x.Ratings.Votes);
|
||||
|
||||
if (ratingCount > 0)
|
||||
{
|
||||
book.Ratings = new Ratings
|
||||
{
|
||||
Votes = ratingCount,
|
||||
Value = book.Editions.Value.Sum(x => x.Ratings.Votes * x.Ratings.Value) / ratingCount
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
book.Ratings = new Ratings { Votes = 0, Value = 0 };
|
||||
}
|
||||
|
||||
return book;
|
||||
}
|
||||
|
||||
private static Edition MapEdition(BookResource resource)
|
||||
{
|
||||
var edition = new Edition
|
||||
{
|
||||
ForeignEditionId = resource.ForeignId.ToString(),
|
||||
TitleSlug = resource.ForeignId.ToString(),
|
||||
Isbn13 = resource.Isbn13,
|
||||
Asin = resource.Asin,
|
||||
Title = resource.Title.CleanSpaces(),
|
||||
Language = resource.Language,
|
||||
Overview = resource.Description,
|
||||
Format = resource.Format,
|
||||
IsEbook = resource.IsEbook,
|
||||
Disambiguation = resource.EditionInformation,
|
||||
Publisher = resource.Publisher,
|
||||
PageCount = resource.NumPages ?? 0,
|
||||
ReleaseDate = resource.ReleaseDate,
|
||||
Ratings = new Ratings { Votes = resource.RatingCount, Value = (decimal)resource.AverageRating }
|
||||
};
|
||||
|
||||
if (resource.ImageUrl.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
edition.Images.Add(new MediaCover.MediaCover
|
||||
{
|
||||
Url = resource.ImageUrl,
|
||||
CoverType = MediaCoverTypes.Cover
|
||||
});
|
||||
}
|
||||
|
||||
edition.Links.Add(new Links { Url = resource.Url, Name = "Goodreads Book" });
|
||||
|
||||
return edition;
|
||||
}
|
||||
|
||||
private int GetAuthorId(WorkResource b)
|
||||
{
|
||||
return b.Books.First().Contributors.FirstOrDefault()?.ForeignId ?? 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NzbDrone.Core.MetadataSource.BookInfo
|
||||
{
|
||||
public class AuthorResource
|
||||
{
|
||||
public int ForeignId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string TitleSlug { get; set; }
|
||||
|
||||
public string Description { get; set; }
|
||||
public string ImageUrl { get; set; }
|
||||
public string Url { get; set; }
|
||||
|
||||
public int ReviewCount { get; set; }
|
||||
public int RatingCount { get; set; }
|
||||
public double AverageRating { get; set; }
|
||||
|
||||
public DateTime LastChange { get; set; }
|
||||
|
||||
public DateTime LastRefresh { get; set; }
|
||||
|
||||
public List<WorkResource> Works { get; set; }
|
||||
|
||||
public List<SeriesResource> Series { get; set; }
|
||||
}
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NzbDrone.Core.MetadataSource.SkyHook
|
||||
namespace NzbDrone.Core.MetadataSource.BookInfo
|
||||
{
|
||||
public class BookResource
|
||||
{
|
||||
public int GoodreadsId { get; set; }
|
||||
public int ForeignId { get; set; }
|
||||
public string TitleSlug { get; set; }
|
||||
public string Asin { get; set; }
|
||||
public string Description { get; set; }
|
@ -1,8 +1,8 @@
|
||||
namespace NzbDrone.Core.MetadataSource.SkyHook
|
||||
namespace NzbDrone.Core.MetadataSource.BookInfo
|
||||
{
|
||||
public class ContributorResource
|
||||
{
|
||||
public int GoodreadsId { get; set; }
|
||||
public int ForeignId { get; set; }
|
||||
public string Role { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NzbDrone.Core.MetadataSource.BookInfo
|
||||
{
|
||||
public class RecentUpdatesResource
|
||||
{
|
||||
public bool Limited { get; set; }
|
||||
public DateTime Since { get; set; }
|
||||
public List<int> Ids { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NzbDrone.Core.MetadataSource.BookInfo
|
||||
{
|
||||
public class SeriesResource
|
||||
{
|
||||
public int ForeignId { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Description { get; set; }
|
||||
|
||||
public List<SeriesWorkLinkResource> LinkItems { get; set; }
|
||||
}
|
||||
|
||||
public class SeriesWorkLinkResource
|
||||
{
|
||||
public string ForeignSeriesId { get; set; }
|
||||
public string ForeignWorkId { get; set; }
|
||||
public string PositionInSeries { get; set; }
|
||||
public int SeriesPosition { get; set; }
|
||||
public bool Primary { get; set; }
|
||||
}
|
||||
}
|
@ -1,15 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NzbDrone.Core.MetadataSource.SkyHook
|
||||
namespace NzbDrone.Core.MetadataSource.BookInfo
|
||||
{
|
||||
public class WorkResource
|
||||
{
|
||||
public int GoodreadsId { get; set; }
|
||||
public int ForeignId { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string TitleSlug { get; set; }
|
||||
public string Url { get; set; }
|
||||
public DateTime? ReleaseDate { get; set; }
|
||||
public List<string> Genres { get; set; }
|
||||
public List<int> RelatedWorks { get; set; }
|
||||
public List<BookResource> Books { get; set; } = new List<BookResource>();
|
||||
}
|
||||
}
|
@ -1,491 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Cache;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Core.Books;
|
||||
using NzbDrone.Core.Exceptions;
|
||||
using NzbDrone.Core.MediaCover;
|
||||
|
||||
namespace NzbDrone.Core.MetadataSource.SkyHook
|
||||
{
|
||||
public class SkyHookProxy
|
||||
{
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly Logger _logger;
|
||||
private readonly IAuthorService _authorService;
|
||||
private readonly IBookService _bookService;
|
||||
private readonly IMetadataRequestBuilder _requestBuilder;
|
||||
private readonly ICached<HashSet<string>> _cache;
|
||||
|
||||
public SkyHookProxy(IHttpClient httpClient,
|
||||
IMetadataRequestBuilder requestBuilder,
|
||||
IAuthorService authorService,
|
||||
IBookService bookService,
|
||||
Logger logger,
|
||||
ICacheManager cacheManager)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_requestBuilder = requestBuilder;
|
||||
_authorService = authorService;
|
||||
_bookService = bookService;
|
||||
_cache = cacheManager.GetCache<HashSet<string>>(GetType());
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public HashSet<string> GetChangedAuthors(DateTime startTime)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public Author GetAuthorInfo(string foreignAuthorId)
|
||||
{
|
||||
_logger.Debug("Getting Author details ReadarrAPI.MetadataID of {0}", foreignAuthorId);
|
||||
|
||||
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
|
||||
.SetSegment("route", $"author/{foreignAuthorId}")
|
||||
.Build();
|
||||
|
||||
httpRequest.AllowAutoRedirect = true;
|
||||
httpRequest.SuppressHttpError = true;
|
||||
|
||||
var httpResponse = _httpClient.Get<AuthorResource>(httpRequest);
|
||||
|
||||
if (httpResponse.HasHttpError)
|
||||
{
|
||||
if (httpResponse.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
throw new AuthorNotFoundException(foreignAuthorId);
|
||||
}
|
||||
else if (httpResponse.StatusCode == HttpStatusCode.BadRequest)
|
||||
{
|
||||
throw new BadRequestException(foreignAuthorId);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new HttpException(httpRequest, httpResponse);
|
||||
}
|
||||
}
|
||||
|
||||
return MapAuthor(httpResponse.Resource);
|
||||
}
|
||||
|
||||
public HashSet<string> GetChangedBooks(DateTime startTime)
|
||||
{
|
||||
return _cache.Get("ChangedBooks", () => GetChangedBooksUncached(startTime), TimeSpan.FromMinutes(30));
|
||||
}
|
||||
|
||||
private HashSet<string> GetChangedBooksUncached(DateTime startTime)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public Tuple<string, Book, List<AuthorMetadata>> GetBookInfo(string foreignBookId)
|
||||
{
|
||||
return null;
|
||||
/*
|
||||
_logger.Debug("Getting Book with ReadarrAPI.MetadataID of {0}", foreignBookId);
|
||||
|
||||
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
|
||||
.SetSegment("route", $"book/{foreignBookId}")
|
||||
.Build();
|
||||
|
||||
httpRequest.AllowAutoRedirect = true;
|
||||
httpRequest.SuppressHttpError = true;
|
||||
|
||||
var httpResponse = _httpClient.Get<BookResource>(httpRequest);
|
||||
|
||||
if (httpResponse.HasHttpError)
|
||||
{
|
||||
if (httpResponse.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
throw new BookNotFoundException(foreignBookId);
|
||||
}
|
||||
else if (httpResponse.StatusCode == HttpStatusCode.BadRequest)
|
||||
{
|
||||
throw new BadRequestException(foreignBookId);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new HttpException(httpRequest, httpResponse);
|
||||
}
|
||||
}
|
||||
|
||||
var b = httpResponse.Resource;
|
||||
var book = MapBook(b);
|
||||
|
||||
// var authors = httpResponse.Resource.AuthorMetadata.SelectList(MapAuthor);
|
||||
var authorid = GetAuthorId(b).ToString();
|
||||
|
||||
// book.AuthorMetadata = authors.First(x => x.ForeignAuthorId == authorid);
|
||||
return new Tuple<string, Book, List<AuthorMetadata>>(authorid, book, null);
|
||||
*/
|
||||
}
|
||||
|
||||
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 author)
|
||||
{
|
||||
try
|
||||
{
|
||||
var lowerTitle = title.ToLowerInvariant();
|
||||
|
||||
var split = lowerTitle.Split(':');
|
||||
var prefix = split[0];
|
||||
|
||||
if (split.Length == 2 && new[] { "readarr", "readarrid", "goodreads", "isbn", "asin" }.Contains(prefix))
|
||||
{
|
||||
var slug = split[1].Trim();
|
||||
|
||||
if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace))
|
||||
{
|
||||
return new List<Book>();
|
||||
}
|
||||
|
||||
if (prefix == "goodreads" || prefix == "readarr" || prefix == "readarrid")
|
||||
{
|
||||
var isValid = int.TryParse(slug, out var searchId);
|
||||
if (!isValid)
|
||||
{
|
||||
return new List<Book>();
|
||||
}
|
||||
|
||||
return SearchByGoodreadsId(searchId);
|
||||
}
|
||||
else if (prefix == "isbn")
|
||||
{
|
||||
return SearchByIsbn(slug);
|
||||
}
|
||||
else if (prefix == "asin")
|
||||
{
|
||||
return SearchByAsin(slug);
|
||||
}
|
||||
}
|
||||
|
||||
var q = title.ToLower().Trim();
|
||||
if (author != null)
|
||||
{
|
||||
q += " " + author;
|
||||
}
|
||||
|
||||
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
|
||||
.SetSegment("route", "search")
|
||||
.AddQueryParam("q", q)
|
||||
.Build();
|
||||
|
||||
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);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warn(ex, ex.Message);
|
||||
throw new SkyHookException("Search for '{0}' failed. Invalid response received from ReadarrAPI.", title);
|
||||
}
|
||||
}
|
||||
|
||||
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 httpRequest = _requestBuilder.GetRequestBuilder().Create()
|
||||
.SetSegment("route", $"book/{type}/{id}")
|
||||
.Build();
|
||||
|
||||
var httpResponse = _httpClient.Get<BookSearchResource>(httpRequest);
|
||||
|
||||
var result = _httpClient.Get<BookSearchResource>(httpRequest);
|
||||
|
||||
return MapSearchResult(result.Resource);
|
||||
}
|
||||
catch (HttpException)
|
||||
{
|
||||
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 }'{1}' failed. Invalid response received from ReadarrAPI.", type, id);
|
||||
}
|
||||
}
|
||||
|
||||
public List<object> SearchForNewEntity(string title)
|
||||
{
|
||||
var books = SearchForNewBook(title, null);
|
||||
|
||||
var result = new List<object>();
|
||||
foreach (var book in books)
|
||||
{
|
||||
var author = book.Author.Value;
|
||||
|
||||
if (!result.Contains(author))
|
||||
{
|
||||
result.Add(author);
|
||||
}
|
||||
|
||||
result.Add(book);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private Author MapAuthor(AuthorResource resource)
|
||||
{
|
||||
var metadata = MapAuthor(resource.AuthorMetadata.First(x => x.GoodreadsId == resource.GoodreadsId));
|
||||
|
||||
var books = resource.Works
|
||||
.Where(x => GetAuthorId(x) == resource.GoodreadsId)
|
||||
.Select(MapBook)
|
||||
.ToList();
|
||||
|
||||
books.ForEach(x => x.AuthorMetadata = metadata);
|
||||
|
||||
var series = resource.Series.Select(MapSeries).ToList();
|
||||
|
||||
MapSeriesLinks(series, books, resource);
|
||||
|
||||
var result = new Author
|
||||
{
|
||||
Metadata = metadata,
|
||||
CleanName = Parser.Parser.CleanAuthorName(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.Works.Any()))
|
||||
{
|
||||
if (seriesDict.TryGetValue(s.GoodreadsId.ToString(), out var curr))
|
||||
{
|
||||
curr.LinkItems = s.Works.Where(x => bookDict.ContainsKey(x.GoodreadsId.ToString())).Select(l => new SeriesBookLink
|
||||
{
|
||||
Book = bookDict[l.GoodreadsId.ToString()],
|
||||
Series = curr,
|
||||
IsPrimary = l.Primary,
|
||||
Position = l.Position
|
||||
}).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static AuthorMetadata MapAuthor(AuthorSummaryResource resource)
|
||||
{
|
||||
var author = new AuthorMetadata
|
||||
{
|
||||
ForeignAuthorId = resource.GoodreadsId.ToString(),
|
||||
TitleSlug = resource.TitleSlug,
|
||||
Name = resource.Name.CleanSpaces(),
|
||||
Overview = resource.Description,
|
||||
Ratings = new Ratings { Votes = resource.RatingsCount, Value = (decimal)resource.AverageRating }
|
||||
};
|
||||
|
||||
author.NameLastFirst = author.Name.ToLastFirst();
|
||||
author.SortName = author.Name.ToLower();
|
||||
author.SortNameLastFirst = author.Name.ToLastFirst().ToLower();
|
||||
|
||||
if (resource.ImageUrl.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
author.Images.Add(new MediaCover.MediaCover
|
||||
{
|
||||
Url = resource.ImageUrl,
|
||||
CoverType = MediaCoverTypes.Poster
|
||||
});
|
||||
}
|
||||
|
||||
author.Links.Add(new Links { Url = resource.Url, Name = "Goodreads" });
|
||||
|
||||
return author;
|
||||
}
|
||||
|
||||
private static Series MapSeries(SeriesResource resource)
|
||||
{
|
||||
var series = new Series
|
||||
{
|
||||
ForeignSeriesId = resource.GoodreadsId.ToString(),
|
||||
Title = resource.Title,
|
||||
Description = resource.Description
|
||||
};
|
||||
|
||||
return series;
|
||||
}
|
||||
|
||||
private static Book MapBook(WorkResource resource)
|
||||
{
|
||||
var book = new Book
|
||||
{
|
||||
ForeignBookId = resource.GoodreadsId.ToString(),
|
||||
Title = resource.Title,
|
||||
TitleSlug = resource.TitleSlug,
|
||||
CleanTitle = Parser.Parser.CleanAuthorName(resource.Title),
|
||||
ReleaseDate = resource.ReleaseDate,
|
||||
};
|
||||
|
||||
book.Links.Add(new Links { Url = resource.Url, Name = "Goodreads Editions" });
|
||||
|
||||
if (resource.Books != null)
|
||||
{
|
||||
book.Editions = resource.Books.Select(x => MapEdition(x)).ToList();
|
||||
|
||||
// monitor the most rated release
|
||||
var mostPopular = book.Editions.Value.OrderByDescending(x => x.Ratings.Votes).FirstOrDefault();
|
||||
if (mostPopular != null)
|
||||
{
|
||||
mostPopular.Monitored = true;
|
||||
|
||||
// fix work title if missing
|
||||
if (book.Title.IsNullOrWhiteSpace())
|
||||
{
|
||||
book.Title = mostPopular.Title;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
book.Editions = new List<Edition>();
|
||||
}
|
||||
|
||||
Debug.Assert(!book.Editions.Value.Any() || book.Editions.Value.Count(x => x.Monitored) == 1, "one edition monitored");
|
||||
|
||||
book.AnyEditionOk = true;
|
||||
|
||||
var ratingCount = book.Editions.Value.Sum(x => x.Ratings.Votes);
|
||||
|
||||
if (ratingCount > 0)
|
||||
{
|
||||
book.Ratings = new Ratings
|
||||
{
|
||||
Votes = ratingCount,
|
||||
Value = book.Editions.Value.Sum(x => x.Ratings.Votes * x.Ratings.Value) / ratingCount
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
book.Ratings = new Ratings { Votes = 0, Value = 0 };
|
||||
}
|
||||
|
||||
return book;
|
||||
}
|
||||
|
||||
private static Edition MapEdition(BookResource resource)
|
||||
{
|
||||
var edition = new Edition
|
||||
{
|
||||
ForeignEditionId = resource.GoodreadsId.ToString(),
|
||||
TitleSlug = resource.TitleSlug,
|
||||
Isbn13 = resource.Isbn13,
|
||||
Asin = resource.Asin,
|
||||
Title = resource.Title.CleanSpaces(),
|
||||
Language = resource.Language,
|
||||
Overview = resource.Description,
|
||||
Format = resource.Format,
|
||||
IsEbook = resource.IsEbook,
|
||||
Disambiguation = resource.EditionInformation,
|
||||
Publisher = resource.Publisher,
|
||||
PageCount = resource.NumPages ?? 0,
|
||||
ReleaseDate = resource.ReleaseDate,
|
||||
Ratings = new Ratings { Votes = resource.RatingCount, Value = (decimal)resource.AverageRating }
|
||||
};
|
||||
|
||||
if (resource.ImageUrl.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
edition.Images.Add(new MediaCover.MediaCover
|
||||
{
|
||||
Url = resource.ImageUrl,
|
||||
CoverType = MediaCoverTypes.Cover
|
||||
});
|
||||
}
|
||||
|
||||
edition.Links.Add(new Links { Url = resource.Url, Name = "Goodreads Book" });
|
||||
|
||||
return edition;
|
||||
}
|
||||
|
||||
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.Works)
|
||||
{
|
||||
var book = _bookService.FindById(b.GoodreadsId.ToString());
|
||||
if (book == null)
|
||||
{
|
||||
book = MapBook(b);
|
||||
|
||||
var authorid = GetAuthorId(b);
|
||||
|
||||
if (authorid == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var author = _authorService.FindById(authorid.ToString());
|
||||
|
||||
if (author == null)
|
||||
{
|
||||
var authorMetadata = metadata[authorid.ToString()];
|
||||
|
||||
author = new Author
|
||||
{
|
||||
CleanName = Parser.Parser.CleanAuthorName(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 int GetAuthorId(WorkResource b)
|
||||
{
|
||||
return b.Books.First().Contributors.FirstOrDefault()?.GoodreadsId ?? 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
namespace NzbDrone.Core.MetadataSource.SkyHook
|
||||
{
|
||||
public class AuthorResource : BulkResource
|
||||
{
|
||||
public int GoodreadsId { get; set; }
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
namespace NzbDrone.Core.MetadataSource.SkyHook
|
||||
{
|
||||
public class AuthorSummaryResource
|
||||
{
|
||||
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 Url { get; set; }
|
||||
|
||||
public int ReviewCount { get; set; }
|
||||
public int RatingsCount { get; set; }
|
||||
public double AverageRating { get; set; }
|
||||
}
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
namespace NzbDrone.Core.MetadataSource.SkyHook
|
||||
{
|
||||
public class BookSearchResource : BulkResource
|
||||
{
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NzbDrone.Core.MetadataSource.SkyHook
|
||||
{
|
||||
public class BulkResource
|
||||
{
|
||||
public List<AuthorSummaryResource> AuthorMetadata { get; set; } = new List<AuthorSummaryResource>();
|
||||
public List<WorkResource> Works { get; set; }
|
||||
public List<SeriesResource> Series { get; set; }
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NzbDrone.Core.MetadataSource.SkyHook
|
||||
{
|
||||
public class SeriesResource
|
||||
{
|
||||
public int GoodreadsId { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Description { get; set; }
|
||||
|
||||
public List<SeriesWorkLinkResource> Works { get; set; }
|
||||
}
|
||||
|
||||
public class SeriesWorkLinkResource
|
||||
{
|
||||
public int GoodreadsId { get; set; }
|
||||
public string Position { get; set; }
|
||||
public bool Primary { get; set; }
|
||||
}
|
||||
}
|
Loading…
Reference in new issue