Exemplo n.º 1
0
        public async void CanSearchByAuthor()
        {
            // Arrange
            var service = new VolumeService();

            // Act
            var searchResult = await service.SearchByAuthorAsync("Isaac Asimov");

            // Assert
            Assert.NotNull(searchResult);
            Assert.NotEqual(0, searchResult.TotalItems);
            Assert.Equal("Isaac Asimov", searchResult.Items.First().VolumeInfo.Authors.First());
        }
Exemplo n.º 2
0
        public async void CanGetMoreThan40Books()
        {
            // Arrange
            var       service  = new VolumeService();
            const int maxBooks = 120;

            // Act
            var searchResult = await service.SearchByAuthorAsync("Terry Pratchett", maxBooks);

            // Assert
            Assert.NotNull(searchResult);
            Assert.NotEqual(0, searchResult.TotalItems);
            Assert.InRange(searchResult.Items.Count(), 41, maxBooks);
        }
Exemplo n.º 3
0
        private async Task <List <Book> > GenerateBooksAsync()
        {
            var authors    = _context.Author.ToList();
            var languages  = _context.BookLanguage.ToList();
            var publishers = _context.Publisher.ToList();
            var categories = _context.Category.ToList();
            var books      = new List <Book>();

            var service = new VolumeService();

            Console.WriteLine("Downloading data from the Google Books...");
            foreach (var author in authors)
            {
                var authorBooks = new List <Book>();

                Console.WriteLine($"Searching for the books by {author.Name}...");
                var searchResult = await service.SearchByAuthorAsync(author.Name, 80);

                Console.WriteLine($"{searchResult.Items.Count()} books have been found and downloaded.");

                Console.WriteLine("Processing book data...");
                foreach (var item in searchResult.Items)
                {
                    // Skip Google Books which are not for sale or which do not match the database
                    if (item.SaleInfo.Saleability != "FOR_SALE" ||
                        author.Name != item.VolumeInfo.Authors.First() ||
                        !publishers.Any(p => p.Name == item.VolumeInfo.Publisher) ||
                        !categories.Any(c => c.Name == item.VolumeInfo.Categories.First()) ||
                        !languages.Any(l => l.Code == item.VolumeInfo.Language) ||
                        !item.VolumeInfo.IndustryIdentifiers.Any(ii => ii.Type == "ISBN_13")
                        )
                    {
                        continue;
                    }

                    var volumeData = item.VolumeInfo;
                    var saleData   = item.SaleInfo;

                    var book = new Book()
                    {
                        Title          = volumeData.Title,
                        Subtitle       = volumeData.Subtitle,
                        AuthorId       = author.AuthorId,
                        PublisherId    = publishers.First(p => p.Name == volumeData.Publisher).PublisherId,
                        Description    = volumeData.Description,
                        IsMature       = volumeData.MaturityRating != "NOT_MATURE",
                        Isbn10         = volumeData.IndustryIdentifiers.First(ii => ii.Type == "ISBN_10").Identifier,
                        Isbn13         = volumeData.IndustryIdentifiers.First(ii => ii.Type == "ISBN_13").Identifier,
                        TotalPages     = volumeData.PageCount,
                        CategoryId     = categories.First(c => c.Name == volumeData.Categories.First()).CategoryId,
                        CoverImage     = await ImageHelper.DownloadImageAsync(volumeData.ImageLinks.Thumbnail),
                        CoverImageType = "jpeg",
                        BookLanguageId = languages.First(l => l.Code == volumeData.Language).BookLanguageId,
                        Price          = saleData.RetailPrice.Amount,
                        DownloadLink   = volumeData.CanonicalVolumeLink
                    };

                    string publicationDate = volumeData.PublishedDate;
                    if (publicationDate.Length == 4) // If there is only year in the publication date
                    {
                        publicationDate += "-01-01"; // add month and day to be able to store the date
                    }
                    book.PublicationDate = DateTime.Parse(publicationDate);

                    authorBooks.Add(book);
                }
                books.AddRange(authorBooks);
                Console.WriteLine($"{authorBooks.Count()} have been saved.");
            }

            return(books);
        }