//This method works by checking whether the GoogleBookModel object and GoodreadsModel object (both passed into the method's argument) is not null. If it is not null then it will check whether the GoodreadsModel object's page count attribute is
        //null. If it is not null then assign this data value to the public BookModel object's data attribute that holds the page count. If it is null then check whether the GoogleBooksModel object's page count attribute value is equal to 0.
        //If it does not equal 0 then assign this data value to the public BookModel object's data attribute that holds the page count.

        //However, if one or both of the objects(passed into the method's arguments) is not null then it first
        //checks whether the GoodreadsModel object is null. If it is not null then it will check whether the GoodreadsModel object's page count attribute is
        //null. If it is not null then assign this data value to the public BookModel object's data attribute that holds the page count. If it is null then it will check whether the GoogleBooksModel object is null. If it is not null then it will check whether
        //the GoogleBooksModel object's page count attribute value is equal to 0. If it does not equal 0 then assign this data value to the public BookModel object's data attribute that holds the page count.
        private void SetPageCount(GoodreadsModel goodreadsBookData, GoogleBookModel googleBooksBookData)
        {
            if (goodreadsBookData != null && googleBooksBookData != null)
            {
                if (goodreadsBookData.PageCount != null)
                {
                    bookModel.PageCount = goodreadsBookData.PageCount;
                }
                else if (googleBooksBookData.PageCount != 0)
                {
                    bookModel.PageCount = googleBooksBookData.PageCount;
                }
            }
            else
            {
                if (goodreadsBookData != null)
                {
                    if (goodreadsBookData.PageCount != null)
                    {
                        bookModel.PageCount = goodreadsBookData.PageCount;
                    }
                }
                else if (googleBooksBookData != null)
                {
                    if (googleBooksBookData.PageCount != 0)
                    {
                        bookModel.PageCount = googleBooksBookData.PageCount;
                    }
                }
            }
        }
        //Tnis method works by checking with the GoogleBookModel and GoodreadsModel(passed into the method's arguments) is not null. If both are not null then it check whether the title attribute held by the GoodreadsModel object is not null or empty. If it is
        //not null or empty then this data value will be assigned to the public BookModel object's data attribute which holds the title. If the object's title attribute value is null or empty then check whether the title attribute held by the GoodreadsModel object
        //is not null or empty. If it is not null or empty then this data value will be assigned to the public BookModel object's data attribute which holds the title.

        //However if one or both of the objects are null then check whether the GoogleBookModel object is null. If it is not null then check whether the title attribute held by the GoogleBookModel object is not null or empty. If it is
        //not null or empty then this data value will be assigned to the public BookModel object's data attribute which holds the title. If the GoogleBookModel object is null then check whether the GoodreadsModel object is null. If it is not null then check whether
        //the title attribute held by the GoodreadsModel object is not null or empty. If it is not null or empty then this data value will be assigned to the public BookModel object's data attribute which holds the title.
        private void SetBookTitle(GoodreadsModel goodreadsBookData, GoogleBookModel googleBooksBookData)
        {
            if (goodreadsBookData != null && googleBooksBookData != null)
            {
                if (!String.IsNullOrEmpty(goodreadsBookData.Title))
                {
                    bookModel.Title = goodreadsBookData.Title;
                }

                else if (!String.IsNullOrEmpty(googleBooksBookData.Title))
                {
                    bookModel.Title = googleBooksBookData.Title;
                }
            }
            else
            {
                if (googleBooksBookData != null)
                {
                    if (!String.IsNullOrEmpty(googleBooksBookData.Title))
                    {
                        bookModel.Title = googleBooksBookData.Title;
                    }
                }
                else if (goodreadsBookData != null)
                {
                    if (!String.IsNullOrEmpty(goodreadsBookData.Title))
                    {
                        bookModel.Title = goodreadsBookData.Title;
                    }
                }
            }
        }
Example #3
0
        //This method works by querying the Google book api to get book information using the isbn(passed into the method's arguments). It then checks to see if the query returned data. If it did then get the first book
        //from the results and return it. If the query returning null then it will return a GoogleBookModel with empty attributes.
        public async Task <GoogleBookModel> CollectDataForBook(string isbn)
        {
            var bookResults = SearchBook(isbn, 0, 1);

            if (bookResults != null)
            {
                googleBookModel = bookResults.Item2.First();
            }
            else
            {
                googleBookModel = new GoogleBookModel()
                {
                    Title                    = "",
                    Description              = "",
                    Subtitle                 = "",
                    AverageRating            = 0,
                    Authors                  = new List <string>(),
                    Genres                   = new List <string>(),
                    ThumbnailUrl             = "",
                    PageCount                = 0,
                    IndustryIdentifiersDatas = new List <IndustryIdentifiersData>()
                };
            }

            return(googleBookModel);
        }
 //This method works by checking whether the GoogleBookModel object(passed into the method's argument) is not null. If it is not null then it will check whether the Subtitle attribute is null or empty. If it is
 //not then this Subtitle data value will be assigned to the public BookModel object's data attribute that holds the subtitle.
 private void SetSubtitle(GoogleBookModel googleBooksBookData)
 {
     if (googleBooksBookData != null)
     {
         if (!String.IsNullOrEmpty(googleBooksBookData.Subtitle))
         {
             bookModel.Subtitle = googleBooksBookData.Subtitle;
         }
     }
 }
 //This method works by checking whether the GoogleBookModel object(passed into the method's argument) is not null. If it is not null then it will check whether the Thumbnail url attribute is null or empty. If it is
 //not then this thumbnail url will be assigned to the public BookModel object data attribute that holds the thumbnail url.
 public void SetThumbnailUrl(GoogleBookModel googleBooksBookData)
 {
     if (googleBooksBookData != null)
     {
         if (!String.IsNullOrEmpty(googleBooksBookData.ThumbnailUrl))
         {
             bookModel.ThumbnailUrl = googleBooksBookData.ThumbnailUrl;
         }
     }
 }
 //This method works by checking whether the GoogleBookModel object(passed into the method's argument) is not null. If it is not null then it will check whether the Genres attribute is null. If it is
 //not then this will be assigned to the public BookModel object's data attribute that holds the genres. If the object is null then an empty list of strings will be assigned to the public BookModel object data attribute that holds the genres.
 private void SetGenres(GoogleBookModel googleBooksBookData)
 {
     if (googleBooksBookData != null)
     {
         bookModel.Genres = googleBooksBookData.Genres;
     }
     else
     {
         bookModel.Genres = new List <string>();
     }
 }
        //This method by first checking whether both the GoodreadsModel and GoogleBooksModel objects(passed into the method's arguments) is not null. If both are not null then it will check whether the Authors attribute value
        //held by GoogleBookModel model actually contains one or more author. If this check is passed then it will assign these authors to the public BookMode object's data attribute which holds the authors. If the check was not met then
        //it will check whether the Authors attribute value held by GoodreadsModel model actually contains one or more author. If this check is passed then it will iterate through each author and add it to the initialised list of authors and then will assign this list
        //to the public BookModel object's data attribute which holds the authors. If neither objects contained authors then assign an empty string list to the public BookModel object's data attribute which holds the authors.

        //However, if one or both objects are null then it will check if the GoogleBookModel object is null. If it isn't then it will check whether the Authors attribute value held by GoogleBookModel model actually contains one or more author.
        //If this check is passed then it will assign these authors to the public BookMode object's data attribute which holds the authors. If the GoogleBookModel object is null then it will check whether the GoodreadModel is not null and if it is not null then
        //it will check whether the Authors attribute value held by GoodreadsModel model actually contains one or more author. If this check is passed then it will iterate through each author and add it to the initialised list of authors and then will assign this list
        //to the public BookModel object's data attribute which holds the authors. If neither objects are null then it will assign an empty string list to the public BookModel object's data attribute which holds the authors.

        private void SetAuthorsOfBook(GoodreadsModel goodreadsBookData, GoogleBookModel googleBooksBookData)
        {
            if (goodreadsBookData != null && googleBooksBookData != null)
            {
                if (googleBooksBookData.Authors.Count != 0)
                {
                    bookModel.Authors = googleBooksBookData.Authors;
                }
                else if (goodreadsBookData.Authors.Count != 0)
                {
                    List <string> authors = new List <string>();
                    foreach (var author in goodreadsBookData.Authors)
                    {
                        authors.Add(author.Name);
                    }
                    bookModel.Authors = authors;
                }
                else
                {
                    bookModel.Authors = new List <string>();
                }
            }
            else
            {
                if (googleBooksBookData != null)
                {
                    if (googleBooksBookData.Authors.Count != 0)
                    {
                        bookModel.Authors = googleBooksBookData.Authors;
                    }
                }
                else if (goodreadsBookData != null)
                {
                    if (goodreadsBookData.Authors.Count != 0)
                    {
                        List <string> authors = new List <string>();
                        foreach (var author in goodreadsBookData.Authors)
                        {
                            authors.Add(author.Name);
                        }
                        bookModel.Authors = authors;
                    }
                    else
                    {
                        bookModel.Authors = new List <string>();
                    }
                }
            }
        }
        //This method works by checking whether the GoogleBookModel object and GoodreadsModel object (both passed into the method's argument) is not null. If it is not null then it will check whether the GoodreadsModel object's Isbn attribute is
        //null or empty. If it is not then this isbn value url will be assigned to the public BookModel object data attribute that holds the isbn. If it is null or empty then it check whether the GoogleBooksModel object's Isbn attribute is
        //null or empty. If it is then this isbn value url will be assigned to the public BookModel object's data attribute that holds the isbn. If it is null or empty then the isbn(passed into the method's arguments) will be checked to see
        //if it contains numbers to confirm that it is a valid isbn. If this check passes then this isbn will be assigned to the public BookModel object's data attribute that holds the isbn.

        //However, if the one or both of the GoogleBookModel and GoodreadsModel object is null, it will check each model object individually to see if it is null and if it isn't then it will proceed to check if it's Isbn data attribute is not null nor empty.
        //Similarly, once that passes then it will be assigned to the public BookModel object data attribute that holds the isbn. If it neither object is not null then it repeats the same process as for checking the isbn(passed into the method;s arguments)
        //to see if it contains number and if it does, it will assign that value to the public BookModel object's data attribute that holds the isbn.
        private void SetIsbn(GoodreadsModel goodreadsBookData, GoogleBookModel googleBookModel, string retrievedIsbn)
        {
            if (googleBookModel != null && goodreadsBookData != null)
            {
                if (!String.IsNullOrEmpty(goodreadsBookData.Isbn))
                {
                    bookModel.Isbn = goodreadsBookData.Isbn;
                }
                else if (!String.IsNullOrEmpty(GetIsbn(googleBookModel)))
                {
                    bookModel.Isbn = GetIsbn(googleBookModel);
                }
                else
                {
                    if (retrievedIsbn.All(char.IsDigit).Equals(true))
                    {
                        bookModel.Isbn = retrievedIsbn;
                    }
                }
            }
            else
            {
                if (googleBookModel != null)
                {
                    if (!String.IsNullOrEmpty(GetIsbn(googleBookModel)))
                    {
                        bookModel.Isbn = GetIsbn(googleBookModel);
                    }
                }
                else if (goodreadsBookData != null)
                {
                    if (!String.IsNullOrEmpty(goodreadsBookData.Isbn))
                    {
                        bookModel.Isbn = goodreadsBookData.Isbn;
                    }
                }
                else
                {
                    if (retrievedIsbn.All(char.IsDigit).Equals(true))
                    {
                        bookModel.Isbn = retrievedIsbn;
                    }
                }
            }
        }
Example #9
0
        //This method will filter out the book results such that only the books where all the needed fields are non-null are returned such that it can be used to create a book list without encoutering any errors
        //This method works by interating through each book from the results(passed into the method's arguments) and creating a new GoogleBookModel instance where it populates its attributes(properties) with the book data
        //retrieved from the Google book api. If no exception were thrown while populating the attributes then it is added to the list of GoogleBookModels. If an exception is thrown then don't add to that list. After
        //iterating through each book, the method returns the list of GoogleBooksModel which may be empty.
        private List <GoogleBookModel> GetBookListFromResults(Volumes results)
        {
            List <GoogleBookModel> books = new List <GoogleBookModel>();

            foreach (var book in results.Items)
            {
                GoogleBookModel googleBookModel = new GoogleBookModel();
                try
                {
                    googleBookModel.Title                    = (string)HandleNull(book.VolumeInfo.Title, DataType.String);
                    googleBookModel.Authors                  = (List <string>)HandleNull(book.VolumeInfo.Authors, DataType.StringList);
                    googleBookModel.ThumbnailUrl             = (string)HandleNull(book.VolumeInfo.ImageLinks.SmallThumbnail, DataType.String);
                    googleBookModel.IndustryIdentifiersDatas = (List <IndustryIdentifiersData>)HandleNull(book.VolumeInfo.IndustryIdentifiers, DataType.IndustryIdentifiersDataList);
                    books.Add(googleBookModel);
                }
                catch (Exception e) {
                    //Don't add that book to the list cause on the data attributes were null
                }
            }

            return(books);
        }
        //This method works by first checking with the data(passed into the method's argument) is an Isbn. If it is not an isbn number it will first collect book data from the Google books api and uses the output to retrieve the
        //isbn number and this is then used to get book data from the Goodreads api. If the data is an isbn number then get the book data from the Google books and Goodreads apis. Afterwards, it then uses the data retrieved from
        //the Goodreads api to initalise the GoodreadsModel. This model along with the GoogleBookModel is used to set the description to the BookModel object which involves working the Python text summariser to receive a
        //summarised description of the book which is what is assigned to the BookModel object. It then proceeds use the AmazonWebScraper to scrape the relevent book data from the relevent product page and this returns
        //and AmazonModel object. All three models( GoodreadsModel, GoogleBooksModel and AmazonModel) were used to initialise the attributes for the BookModel object.
        public async void CollectDataFromSources(string data)
        {
            bookModel = new BookModel();
            server    = new TCPServer();
            dominantColorWebScraper = new DominantColorWebScraper();
            amazonWebScraper        = new AmazonWebScraper();
            AmazonModel amazonBookData = null;

            IWebDriver driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

            //If the data doesnt contain the isbn then the isbn will be initially retrieved
            //from the google books api which is then passed into the good reads api solution
            var dataContainsIsbn = data.All(char.IsDigit);

            if (dataContainsIsbn.Equals(false))
            {
                googleBookTask = googleBookApi.CollectDataForBook(data);
                amazonTask     = amazonWebScraper.CollectDataForBook(driver, data);
                var isbn = GetIsbn(googleBookModel);
                goodreadsTask = goodreadsApi.SearchByIsbn(isbn);
            }
            else
            {
                googleBookTask = googleBookApi.CollectDataForBook(data);
                amazonTask     = amazonWebScraper.CollectDataForBook(driver, data);
                goodreadsTask  = goodreadsApi.SearchByIsbn(data);
            }

            Task.WhenAll(amazonTask, googleBookTask, goodreadsTask);

            googleBookModel = googleBookTask.Result;

            //Check if model has a valid thumbnail url
            if (googleBookModel.ThumbnailUrl.Count() > 2)
            {
                DominantColorWebScraper dominantColorWebScraper = new DominantColorWebScraper();
                var dominantColour = dominantColorWebScraper.GetDominantColor(driver, googleBookModel.ThumbnailUrl);
                dominantColorString = dominantColour;
            }

            var            book           = goodreadsTask.Result;
            GoodreadsModel goodreadsModel = null;

            if (book != null)
            {
                goodreadsModel = goodreadsApi.CollectDataForBook(book);
            }

            amazonBookData = amazonTask.Result;
            SetDescription(goodreadsModel, googleBookModel);

            SetBookTitle(goodreadsModel, googleBookModel);
            SetAuthorsOfBook(goodreadsModel, googleBookModel);

            SetAverageRatings(goodreadsModel, googleBookModel, amazonBookData);
            SetPageCount(goodreadsModel, googleBookModel);
            SetAmazonRatings(amazonBookData);
            SetAmazonReviews(amazonBookData);
            SetReviewCount(amazonBookData);
            SetGenres(googleBookModel);
            SetSubtitle(googleBookModel);
            SetIsbn(goodreadsModel, googleBookModel, data);
            SetThumbnailUrl(googleBookModel);
            SetDominantColor(dominantColorString);
        }
 //This method works by using LINQ to get the Isbn number from the GoogleBookModel(passed into the method's argument) and return it.
 private static string GetIsbn(GoogleBookModel book)
 {
     return(book.IndustryIdentifiersDatas.Where(iid => iid.Type.Equals("ISBN_13")).Select(d => d.Identifier).SingleOrDefault());
 }