Example #1
0
        public async Task <BookInfo[]> SearchSecondarySourceAsync(ISecondarySource dataSource, Parameters parameters, CancellationToken cancellationToken = default)
        {
            var books = new BookInfo[0];

            // If ASIN is available, use it to search first before falling back to author/title
            if (!string.IsNullOrEmpty(parameters.Asin))
            {
                books = (await dataSource.SearchBookByAsinAsync(parameters.Asin, cancellationToken)).ToArray();
            }

            if (books.Length <= 0)
            {
                books = (await dataSource.SearchBookAsync(parameters.Author, parameters.Title, cancellationToken)).ToArray();
            }

            return(books.OrderByDescending(book => book.Reviews)
                   .ThenByDescending(book => book.Editions)
                   .ToArray());
        }
Example #2
0
        private async Task btnSearchGoodreads_Run()
        {
            if (!File.Exists(txtMobi.Text))
            {
                MessageBox.Show("Specified book was not found.", "Book Not Found");
                return;
            }
            if (!Directory.Exists(_settings.outDir))
            {
                MessageBox.Show("Specified output directory does not exist. Please review the settings page.",
                                "Output Directory Not found");
                return;
            }

            //this.TopMost = true;
            using var metadata = await Task.Run(() => UIFunctions.GetAndValidateMetadata(txtMobi.Text, false, _logger));

            if (metadata == null)
            {
                return;
            }

            try
            {
                var books = new BookInfo[0];
                if (_settings.searchByAsin)
                {
                    books = (await _dataSource.SearchBookByAsinAsync(metadata.Asin)).ToArray();
                }

                if (books.Length <= 0)
                {
                    books = (await _dataSource.SearchBookAsync(metadata.Author, metadata.Title)).ToArray();
                    if (books.Length <= 0)
                    {
                        _logger.Log($"Unable to find this book on {_dataSource.Name}!\nEnsure the book's title ({metadata.Title}) is accurate!");
                        return;
                    }
                }

                string bookUrl;
                if (books.Length == 1)
                {
                    bookUrl = books[0].DataUrl;
                }
                else
                {
                    books = books.OrderByDescending(book => book.Reviews)
                            .ThenByDescending(book => book.Editions)
                            .ToArray();

                    // Pre-load cover images
                    foreach (var book in books.Where(book => !string.IsNullOrEmpty(book.ImageUrl)))
                    {
                        try
                        {
                            book.CoverImage = await _httpClient.GetImageAsync(book.ImageUrl, cancellationToken : _cancelTokens.Token);
                        }
                        catch (Exception ex)
                        {
                            _logger.Log($"Failed to download cover image: {ex.Message}");
                        }
                    }

                    _logger.Log($"Warning: Multiple results returned from {_dataSource.Name}...");
                    var frmG = new frmGR(books);
                    frmG.ShowDialog();
                    bookUrl = books[frmG.cbResults.SelectedIndex].DataUrl;
                }

                if (!string.IsNullOrEmpty(bookUrl))
                {
                    txtGoodreads.Text = bookUrl;
                    txtGoodreads.Refresh();
                    _logger.Log(
                        $"Book found on {_dataSource.Name}!\r\n{metadata.Title} by {metadata.Author}\r\n{_dataSource.Name} URL: {bookUrl}\r\n"
                        + "You may want to visit the URL to ensure it is correct.");
                }
            }
            catch (Exception ex)
            {
                _logger.Log($"An error occurred while searching: {ex.Message}\r\n{ex.StackTrace}");
            }
        }