Пример #1
0
        private void MapBookReport(ImportListItemInfo report)
        {
            if (report.EditionGoodreadsId.IsNotNullOrWhiteSpace() && int.TryParse(report.EditionGoodreadsId, out var goodreadsId))
            {
                // check the local DB
                var edition = _editionService.GetEditionByForeignEditionId(report.EditionGoodreadsId);

                if (edition != null)
                {
                    var book = edition.Book.Value;
                    report.BookGoodreadsId = book.ForeignBookId;
                    report.Book            = edition.Title;
                    report.Author ??= book.AuthorMetadata.Value.Name;
                    report.AuthorGoodreadsId ??= book.AuthorMetadata.Value.ForeignAuthorId;
                    return;
                }

                try
                {
                    var remoteBook = _goodreadsProxy.GetBookInfo(report.EditionGoodreadsId);

                    _logger.Trace($"Mapped {report.EditionGoodreadsId} to [{remoteBook.ForeignBookId}] {remoteBook.Title}");

                    report.BookGoodreadsId = remoteBook.ForeignBookId;
                    report.Book            = remoteBook.Title;
                    report.Author ??= remoteBook.AuthorMetadata.Value.Name;
                    report.AuthorGoodreadsId ??= remoteBook.AuthorMetadata.Value.Name;
                }
                catch (BookNotFoundException)
                {
                    _logger.Debug($"Nothing found for edition [{report.EditionGoodreadsId}]");
                    report.EditionGoodreadsId = null;
                }
            }
            else
            {
                var mappedBook = _goodreadsSearchProxy.Search($"{report.Book} {report.Author}").FirstOrDefault();

                if (mappedBook == null)
                {
                    _logger.Trace($"Nothing found for {report.Author} - {report.Book}");
                    return;
                }

                _logger.Trace($"Mapped {report.EditionGoodreadsId} to [{mappedBook.WorkId}] {mappedBook.BookTitleBare}");

                report.BookGoodreadsId = mappedBook.WorkId.ToString();
                report.Book            = mappedBook.BookTitleBare;
                report.Author ??= mappedBook.Author.Name;
                report.AuthorGoodreadsId ??= mappedBook.Author.Id.ToString();
            }
        }
Пример #2
0
        private List <Book> Search(string query, bool getAllEditions)
        {
            List <SearchJsonResource> result;

            try
            {
                result = _goodreadsSearchProxy.Search(query);
            }
            catch (Exception e)
            {
                _logger.Warn(e, "Error searching for {0}", query);
                return(new List <Book>());
            }

            var books = new List <Book>();

            if (getAllEditions)
            {
                // Slower but more exhaustive, less intensive on metadata API
                var bookIds = result.Select(x => x.WorkId).ToList();

                var idMap = result.Select(x => new { AuthorId = x.Author.Id, BookId = x.WorkId })
                            .GroupBy(x => x.AuthorId)
                            .ToDictionary(x => x.Key, x => x.Select(i => i.BookId.ToString()).ToList());

                List <Book> authorBooks;
                foreach (var author in idMap.Keys)
                {
                    authorBooks = SearchByGoodreadsAuthorId(author);
                    books.AddRange(authorBooks.Where(b => idMap[author].Contains(b.ForeignBookId)));
                }

                var missingBooks = bookIds.ExceptBy(x => x.ToString(), books, x => x.ForeignBookId, StringComparer.Ordinal).ToList();
                foreach (var book in missingBooks)
                {
                    books.AddRange(SearchByGoodreadsWorkId(book));
                }

                return(books);
            }
            else
            {
                // Use sparingly, hits metadata API quite hard
                var ids = result.Select(x => x.BookId).ToList();

                if (ids.Count == 0)
                {
                    return(new List <Book>());
                }

                if (ids.Count == 1)
                {
                    return(SearchByGoodreadsBookId(ids[0], false));
                }

                try
                {
                    return(MapSearchResult(ids));
                }
                catch (Exception e)
                {
                    _logger.Warn(e, "Error mapping search results");

                    return(new List <Book>());
                }
            }
        }