public List <Book> SearchByGoodreadsId(int id) { try { var remote = GetBookInfo(id.ToString()); var book = _bookService.FindById(remote.Item2.ForeignBookId); var result = book ?? remote.Item2; // at this point, book could have the wrong edition. // Check if we already have the correct edition. var remoteEdition = remote.Item2.Editions.Value.Single(x => x.Monitored); var localEdition = _editionService.GetEditionByForeignEditionId(remoteEdition.ForeignEditionId); if (localEdition != null) { result.Editions = new List <Edition> { localEdition }; } // If we don't have the correct edition in the response, add it in. if (!result.Editions.Value.Any(x => x.ForeignEditionId == remoteEdition.ForeignEditionId)) { result.Editions.Value.ForEach(x => x.Monitored = false); result.Editions.Value.Add(remoteEdition); } var author = _authorService.FindById(remote.Item1); if (author == null) { author = new Author { CleanName = Parser.Parser.CleanAuthorName(remote.Item2.AuthorMetadata.Value.Name), Metadata = remote.Item2.AuthorMetadata.Value }; } result.Author = author; return(new List <Book> { result }); } catch (BookNotFoundException) { return(new List <Book>()); } }
private Edition EnsureEditionAdded(List <ImportDecision <LocalBook> > decisions) { var book = decisions.First().Item.Book; var edition = decisions.First().Item.Edition; if (edition.Id == 0) { var dbEdition = _editionService.GetEditionByForeignEditionId(edition.ForeignEditionId); if (dbEdition == null) { _logger.Debug($"Adding remote edition {edition}"); try { edition.BookId = book.Id; edition.Monitored = false; _editionService.InsertMany(new List <Edition> { edition }); dbEdition = _editionService.GetEditionByForeignEditionId(edition.ForeignEditionId); } catch (Exception e) { _logger.Error(e, "Failed to add edition {0}", edition); RejectBook(decisions); return(null); } // Populate the new DB book foreach (var decision in decisions) { decision.Item.Edition = dbEdition; } edition = dbEdition; } } return(edition); }
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(); } }
public List <Book> SearchByGoodreadsId(int id) { try { var remote = GetBookInfo(id.ToString()); var book = _bookService.FindById(remote.Item2.ForeignBookId); var result = book ?? remote.Item2; var edition = _editionService.GetEditionByForeignEditionId(remote.Item2.Editions.Value.Single(x => x.Monitored).ForeignEditionId); if (edition != null) { result.Editions = new List <Edition> { edition }; } var author = _authorService.FindById(remote.Item1); if (author == null) { author = new Author { CleanName = Parser.Parser.CleanAuthorName(remote.Item2.AuthorMetadata.Value.Name), Metadata = remote.Item2.AuthorMetadata.Value }; } result.Author = author; return(new List <Book> { result }); } catch (BookNotFoundException) { return(new List <Book>()); } }
public void Execute(ManualImportCommand message) { _logger.ProgressTrace("Manually importing {0} files using mode {1}", message.Files.Count, message.ImportMode); var imported = new List <ImportResult>(); var importedTrackedDownload = new List <ManuallyImportedFile>(); var bookIds = message.Files.GroupBy(e => e.BookId).ToList(); var fileCount = 0; foreach (var importBookId in bookIds) { var bookImportDecisions = new List <ImportDecision <LocalBook> >(); // turn off anyReleaseOk if specified if (importBookId.First().DisableReleaseSwitching) { var book = _bookService.GetBook(importBookId.First().BookId); book.AnyEditionOk = false; _bookService.UpdateBook(book); } foreach (var file in importBookId) { _logger.ProgressTrace("Processing file {0} of {1}", fileCount + 1, message.Files.Count); var author = _authorService.GetAuthor(file.AuthorId); var book = _bookService.GetBook(file.BookId); var edition = _editionService.GetEditionByForeignEditionId(file.ForeignEditionId); if (edition == null) { var tuple = _bookInfo.GetBookInfo(book.ForeignBookId); edition = tuple.Item2.Editions.Value.SingleOrDefault(x => x.ForeignEditionId == file.ForeignEditionId); } var fileRootFolder = _rootFolderService.GetBestRootFolder(file.Path); var fileInfo = _diskProvider.GetFileInfo(file.Path); var fileTrackInfo = _metadataTagService.ReadTags(fileInfo) ?? new ParsedTrackInfo(); var localTrack = new LocalBook { ExistingFile = fileRootFolder != null, FileTrackInfo = fileTrackInfo, Path = file.Path, Part = fileTrackInfo.TrackNumbers.Any() ? fileTrackInfo.TrackNumbers.First() : 1, PartCount = importBookId.Count(), Size = fileInfo.Length, Modified = fileInfo.LastWriteTimeUtc, Quality = file.Quality, Author = author, Book = book, Edition = edition }; var importDecision = new ImportDecision <LocalBook>(localTrack); if (_rootFolderService.GetBestRootFolder(author.Path) == null) { _logger.Warn($"Destination author folder {author.Path} not in a Root Folder, skipping import"); importDecision.Reject(new Rejection($"Destination author folder {author.Path} is not in a Root Folder")); } bookImportDecisions.Add(importDecision); fileCount += 1; } var downloadId = importBookId.Select(x => x.DownloadId).FirstOrDefault(x => x.IsNotNullOrWhiteSpace()); if (downloadId.IsNullOrWhiteSpace()) { imported.AddRange(_importApprovedBooks.Import(bookImportDecisions, message.ReplaceExistingFiles, null, message.ImportMode)); } else { var trackedDownload = _trackedDownloadService.Find(downloadId); var importResults = _importApprovedBooks.Import(bookImportDecisions, message.ReplaceExistingFiles, trackedDownload.DownloadItem, message.ImportMode); imported.AddRange(importResults); foreach (var importResult in importResults) { importedTrackedDownload.Add(new ManuallyImportedFile { TrackedDownload = trackedDownload, ImportResult = importResult }); } } } _logger.ProgressTrace("Manually imported {0} files", imported.Count); foreach (var groupedTrackedDownload in importedTrackedDownload.GroupBy(i => i.TrackedDownload.DownloadItem.DownloadId).ToList()) { var trackedDownload = groupedTrackedDownload.First().TrackedDownload; var outputPath = trackedDownload.ImportItem.OutputPath.FullPath; if (_diskProvider.FolderExists(outputPath)) { if (_downloadedTracksImportService.ShouldDeleteFolder(_diskProvider.GetDirectoryInfo(outputPath)) && trackedDownload.DownloadItem.CanMoveFiles) { _diskProvider.DeleteFolder(outputPath, true); } } var importedCount = groupedTrackedDownload.Select(c => c.ImportResult) .Count(c => c.Result == ImportResultType.Imported); var downloadItemCount = Math.Max(1, trackedDownload.RemoteBook?.Books.Count ?? 1); var allItemsImported = importedCount >= downloadItemCount; if (allItemsImported) { trackedDownload.State = TrackedDownloadState.Imported; _eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload, imported.First().ImportDecision.Item.Author.Id)); } } }
private List <ManualImportResource> UpdateImportItems(List <ManualImportResource> resources) { var items = new List <ManualImportItem>(); foreach (var resource in resources) { items.Add(new ManualImportItem { Id = resource.Id, Path = resource.Path, Name = resource.Name, Size = resource.Size, Author = resource.Author == null ? null : _authorService.GetAuthor(resource.Author.Id), Book = resource.Book == null ? null : _bookService.GetBook(resource.Book.Id), Edition = resource.ForeignEditionId == null ? null : _editionService.GetEditionByForeignEditionId(resource.ForeignEditionId), Quality = resource.Quality, DownloadId = resource.DownloadId, AdditionalFile = resource.AdditionalFile, ReplaceExistingFiles = resource.ReplaceExistingFiles, DisableReleaseSwitching = resource.DisableReleaseSwitching }); } return(_manualImportService.UpdateItems(items).Select(x => x.ToResource()).ToList()); }