Move all data fetching to BookInfo

This commit is contained in:
BookInfo
2021-12-24 15:13:08 +00:00
parent 2dff18490e
commit f6ff53ca31
28 changed files with 578 additions and 928 deletions
@@ -8,25 +8,39 @@ using NLog;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Books;
using NzbDrone.Core.Exceptions;
using NzbDrone.Core.MediaCover;
using NzbDrone.Core.MetadataSource.Goodreads;
namespace NzbDrone.Core.MetadataSource.BookInfo
{
public class BookInfoProxy : IProvideAuthorInfo
public class BookInfoProxy : IProvideAuthorInfo, IProvideBookInfo, ISearchForNewBook, ISearchForNewAuthor, ISearchForNewEntity
{
private readonly IHttpClient _httpClient;
private readonly IGoodreadsSearchProxy _goodreadsSearchProxy;
private readonly IAuthorService _authorService;
private readonly IBookService _bookService;
private readonly IEditionService _editionService;
private readonly Logger _logger;
private readonly IMetadataRequestBuilder _requestBuilder;
private readonly ICached<HashSet<string>> _cache;
public BookInfoProxy(IHttpClient httpClient,
IMetadataRequestBuilder requestBuilder,
Logger logger,
ICacheManager cacheManager)
IGoodreadsSearchProxy goodreadsSearchProxy,
IAuthorService authorService,
IBookService bookService,
IEditionService editionService,
IMetadataRequestBuilder requestBuilder,
Logger logger,
ICacheManager cacheManager)
{
_httpClient = httpClient;
_goodreadsSearchProxy = goodreadsSearchProxy;
_authorService = authorService;
_bookService = bookService;
_editionService = editionService;
_requestBuilder = requestBuilder;
_cache = cacheManager.GetCache<HashSet<string>>(GetType());
_logger = logger;
@@ -124,12 +138,307 @@ namespace NzbDrone.Core.MetadataSource.BookInfo
return null;
}
public Tuple<string, Book, List<AuthorMetadata>> GetBookInfo(string foreignBookId)
public Tuple<string, Book, List<AuthorMetadata>> GetBookInfo(string foreignBookId, bool useCache = false)
{
return null;
return PollBook(foreignBookId);
}
private Author MapAuthor(AuthorResource resource)
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)
{
var q = title.ToLower().Trim();
if (author != null)
{
q += " " + author;
}
try
{
var lowerTitle = title.ToLowerInvariant();
var split = lowerTitle.Split(':');
var prefix = split[0];
if (split.Length == 2 && new[] { "author", "work", "edition", "isbn", "asin" }.Contains(prefix))
{
var slug = split[1].Trim();
if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace))
{
return new List<Book>();
}
if (prefix == "author" || prefix == "work" || prefix == "edition")
{
var isValid = int.TryParse(slug, out var searchId);
if (!isValid)
{
return new List<Book>();
}
if (prefix == "author")
{
return SearchByGoodreadsAuthorId(searchId);
}
if (prefix == "work")
{
return SearchByGoodreadsWorkId(searchId);
}
if (prefix == "edition")
{
return SearchByGoodreadsBookId(searchId);
}
}
q = slug;
}
return Search(q);
}
catch (HttpException ex)
{
_logger.Warn(ex, ex.Message);
throw new GoodreadsException("Search for '{0}' failed. Unable to communicate with Goodreads.", title);
}
catch (Exception ex)
{
_logger.Warn(ex, ex.Message);
throw new GoodreadsException("Search for '{0}' failed. Invalid response received from Goodreads.",
title);
}
}
public List<Book> SearchByIsbn(string isbn)
{
return Search(isbn);
}
public List<Book> SearchByAsin(string asin)
{
return Search(asin);
}
private List<Book> SearchByGoodreadsAuthorId(int id)
{
try
{
var authorId = id.ToString();
var result = GetAuthorInfo(authorId);
var books = result.Books.Value.OrderByDescending(x => x.Ratings.Popularity).Take(10).ToList();
var authors = new Dictionary<string, AuthorMetadata> { { authorId, result.Metadata.Value } };
foreach (var book in books)
{
AddDbIds(authorId, book, authors);
}
return books;
}
catch (AuthorNotFoundException)
{
return new List<Book>();
}
}
public List<Book> SearchByGoodreadsWorkId(int id)
{
try
{
var tuple = GetBookInfo(id.ToString());
AddDbIds(tuple.Item1, tuple.Item2, tuple.Item3.ToDictionary(x => x.ForeignAuthorId));
return new List<Book> { tuple.Item2 };
}
catch (BookNotFoundException)
{
return new List<Book>();
}
}
public List<Book> SearchByGoodreadsBookId(int id)
{
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
.SetSegment("route", $"book/{id}")
.Build();
httpRequest.SuppressHttpError = true;
var httpResponse = _httpClient.Get<BulkBookResource>(httpRequest);
return MapBulkBook(httpResponse.Resource);
}
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 List<Book> Search(string query)
{
var result = _goodreadsSearchProxy.Search(query);
var ids = result.Select(x => x.BookId).ToList();
return MapSearchResult(ids);
}
private List<Book> MapSearchResult(List<int> ids)
{
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
.SetSegment("route", $"book/bulk")
.SetHeader("Content-Type", "application/json")
.Build();
httpRequest.SetContent(ids.ToJson());
httpRequest.AllowAutoRedirect = true;
var httpResponse = _httpClient.Post<BulkBookResource>(httpRequest);
var mapped = MapBulkBook(httpResponse.Resource);
var idStr = ids.Select(x => x.ToString()).ToList();
return mapped.OrderBy(b => idStr.IndexOf(b.Editions.Value.First().ForeignEditionId)).ToList();
}
private List<Book> MapBulkBook(BulkBookResource resource)
{
var authors = resource.Authors.Select(MapAuthorMetadata).ToDictionary(x => x.ForeignAuthorId, x => x);
var series = resource.Series.Select(MapSeries).ToList();
var books = new List<Book>();
foreach (var work in resource.Works)
{
var book = MapBook(work);
var authorId = work.Books.OrderByDescending(b => b.AverageRating * b.RatingCount).First().Contributors.First().ForeignId.ToString();
AddDbIds(authorId, book, authors);
books.Add(book);
}
MapSeriesLinks(series, books, resource.Series);
return books;
}
private void AddDbIds(string authorId, Book book, Dictionary<string, AuthorMetadata> authors)
{
var dbBook = _bookService.FindById(book.ForeignBookId);
if (dbBook != null)
{
book.UseDbFieldsFrom(dbBook);
var editions = _editionService.GetEditionsByBook(dbBook.Id).ToDictionary(x => x.ForeignEditionId);
foreach (var edition in book.Editions.Value)
{
if (editions.TryGetValue(edition.ForeignEditionId, out var dbEdition))
{
edition.UseDbFieldsFrom(dbEdition);
}
}
}
var author = _authorService.FindById(authorId);
if (author == null)
{
var metadata = authors[authorId];
author = new Author
{
CleanName = Parser.Parser.CleanAuthorName(metadata.Name),
Metadata = metadata
};
}
book.Author = author;
book.AuthorMetadata = author.Metadata.Value;
book.AuthorMetadataId = author.AuthorMetadataId;
}
private Tuple<string, Book, List<AuthorMetadata>> PollBook(string foreignBookId)
{
WorkResource resource = null;
for (var i = 0; i < 60; i++)
{
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
.SetSegment("route", $"work/{foreignBookId}")
.Build();
httpRequest.AllowAutoRedirect = true;
httpRequest.SuppressHttpError = true;
var httpResponse = _httpClient.Get<WorkResource>(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);
}
}
resource = httpResponse.Resource;
if (resource.Books != null)
{
break;
}
Thread.Sleep(2000);
}
if (resource?.Books == null)
{
throw new BookInfoException($"Failed to get books for {foreignBookId}");
}
var book = MapBook(resource);
var authorId = resource.Books.OrderByDescending(x => x.AverageRating * x.RatingCount).First().Contributors.First().ForeignId.ToString();
var metadata = resource.Authors.Select(MapAuthorMetadata).ToList();
var series = resource.Series.Select(MapSeries).ToList();
MapSeriesLinks(series, new List<Book> { book }, resource.Series);
return Tuple.Create(authorId, book, metadata);
}
private static AuthorMetadata MapAuthorMetadata(AuthorResource resource)
{
var metadata = new AuthorMetadata
{
@@ -159,6 +468,13 @@ namespace NzbDrone.Core.MetadataSource.BookInfo
metadata.Links.Add(new Links { Url = resource.Url, Name = "Goodreads" });
}
return metadata;
}
private static Author MapAuthor(AuthorResource resource)
{
var metadata = MapAuthorMetadata(resource);
var books = resource.Works
.Where(x => x.ForeignId > 0 && GetAuthorId(x) == resource.ForeignId)
.Select(MapBook)
@@ -168,7 +484,7 @@ namespace NzbDrone.Core.MetadataSource.BookInfo
var series = resource.Series.Select(MapSeries).ToList();
MapSeriesLinks(series, books, resource);
MapSeriesLinks(series, books, resource.Series);
var result = new Author
{
@@ -181,13 +497,18 @@ namespace NzbDrone.Core.MetadataSource.BookInfo
return result;
}
private static void MapSeriesLinks(List<Series> series, List<Book> books, AuthorResource resource)
private static void MapSeriesLinks(List<Series> series, List<Book> books, List<SeriesResource> resource)
{
var bookDict = books.ToDictionary(x => x.ForeignBookId);
var seriesDict = series.ToDictionary(x => x.ForeignSeriesId);
foreach (var book in books)
{
book.SeriesLinks = new List<SeriesBookLink>();
}
// only take series where there are some works
foreach (var s in resource.Series.Where(x => x.LinkItems.Any()))
foreach (var s in resource.Where(x => x.LinkItems.Any()))
{
if (seriesDict.TryGetValue(s.ForeignId.ToString(), out var curr))
{
@@ -199,6 +520,11 @@ namespace NzbDrone.Core.MetadataSource.BookInfo
Position = l.PositionInSeries,
SeriesPosition = l.SeriesPosition
}).ToList();
foreach (var l in curr.LinkItems.Value)
{
l.Book.Value.SeriesLinks.Value.Add(l);
}
}
}
}
@@ -234,8 +560,8 @@ namespace NzbDrone.Core.MetadataSource.BookInfo
{
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();
// monitor the most popular release
var mostPopular = book.Editions.Value.OrderByDescending(x => x.Ratings.Popularity).FirstOrDefault();
if (mostPopular != null)
{
mostPopular.Monitored = true;
@@ -252,17 +578,24 @@ namespace NzbDrone.Core.MetadataSource.BookInfo
book.Editions = new List<Edition>();
}
// sometimes the work release date is after the earliest good edition release
var editionReleases = book.Editions.Value
.Where(x => x.ReleaseDate.HasValue && x.ReleaseDate.Value.Month != 1 && x.ReleaseDate.Value.Day != 1)
.ToList();
if (editionReleases.Any())
// If we are missing the book release date, set as the earliest edition release date
if (!book.ReleaseDate.HasValue)
{
var earliestRelease = editionReleases.Min(x => x.ReleaseDate.Value);
if (earliestRelease < book.ReleaseDate)
var editionReleases = book.Editions.Value
.Where(x => x.ReleaseDate.HasValue && x.ReleaseDate.Value.Month != 1 && x.ReleaseDate.Value.Day != 1)
.ToList();
if (editionReleases.Any())
{
book.ReleaseDate = earliestRelease;
book.ReleaseDate = editionReleases.Min(x => x.ReleaseDate.Value);
}
else
{
editionReleases = book.Editions.Value.Where(x => x.ReleaseDate.HasValue).ToList();
if (editionReleases.Any())
{
book.ReleaseDate = editionReleases.Min(x => x.ReleaseDate.Value);
}
}
}
@@ -322,7 +655,7 @@ namespace NzbDrone.Core.MetadataSource.BookInfo
return edition;
}
private int GetAuthorId(WorkResource b)
private static int GetAuthorId(WorkResource b)
{
return b.Books.First().Contributors.FirstOrDefault()?.ForeignId ?? 0;
}