Exemple #1
0
        private GoogleBookJsonResponse GetGoogleBookItem(string jsonGoogleBooksResponse)
        {
            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(GoogleBookJsonResponse));
            GoogleBookJsonResponse     bookItem       = null;

            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonGoogleBooksResponse)))
            {
                bookItem = (GoogleBookJsonResponse)jsonSerializer.ReadObject(ms);
            }

            return(bookItem);
        }
Exemple #2
0
 private BookItem ChooseBookItem(GoogleBookJsonResponse booksDataFromGoogle, string bookName, string authorName)
 {
     BookItem bookItem = null;
     foreach (BookItem item in booksDataFromGoogle.items)
     {
         if (PhrasesAreEqual(bookName, item.volumeInfo.title) && AuthorIsInList(authorName, item.volumeInfo.authors))
         {
             bookItem = item;
             break;
         }
     }
     return bookItem;
 }
Exemple #3
0
        private BookItem ChooseBookItem(GoogleBookJsonResponse booksDataFromGoogle, string bookName, string authorName)
        {
            BookItem bookItem = null;

            foreach (BookItem item in booksDataFromGoogle.items)
            {
                if (PhrasesAreEqual(bookName, item.volumeInfo.title) && AuthorIsInList(authorName, item.volumeInfo.authors))
                {
                    bookItem = item;
                    break;
                }
            }
            return(bookItem);
        }
Exemple #4
0
        private GoogleBookJsonResponse GetBooksDataFromGoogle(string bookName, string author, int iterator)
        {
            string queryString = baseGoogleBooksUrl + " \"" + bookName + "\" " + author;

            KnigoskopWebClient webClient = new KnigoskopWebClient();

            try
            {
                string jsonGoogleBooksResponse  = webClient.DownloadData(queryString);
                GoogleBookJsonResponse bookItem = GetGoogleBookItem(jsonGoogleBooksResponse);
                return(bookItem);
            }
            catch (WebException ex)
            {
                if (ex.Message.Contains("(403)"))
                {
                    string currentTime = Convert.ToString(DateTime.Now);
                    Console.WriteLine(currentTime + " " + ex.Message);
                    Thread.Sleep((iterator + 1) * 5000);
                    if (iterator <= 10)
                    {
                        iterator++;
                        return(GetBooksDataFromGoogle(bookName, author, iterator));
                    }
                    else
                    {
                        throw ex;
                    }
                }
                else
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #5
0
 private void GoThroughBookRecords()
 {
     using (Entities context = new Entities())
     {
         try
         {
             IQueryable <Guid> books = null;
             if (!string.IsNullOrEmpty(orderByClause))
             {
                 books = context.Books.AsNoTracking().Where(x => x.FromGoogleBooks == null || x.FromGoogleBooks == false).OrderBy(x => x.Name).Select(x => x.BookId);
             }
             else
             {
                 books = context.Books.AsNoTracking().Where(x => x.FromGoogleBooks == null || x.FromGoogleBooks == false).OrderBy(x => x.BookId).Select(x => x.BookId);
             }
             foreach (var bookID in books)
             {
                 Book book = context.Books.FirstOrDefault(x => x.BookId == bookID);
                 ApplicationLogger.WriteStringToLog("Start to process book: " + book.BookId.ToString());
                 try
                 {
                     GoogleBookJsonResponse booksDataFromGoogle = GetBooksDataFromGoogle(book.Name, book.Authors.FirstOrDefault().Name, 0);
                     if (booksDataFromGoogle != null && booksDataFromGoogle.totalItems > 0)
                     {
                         ApplicationLogger.WriteStringToLog("Processed book: " + book.BookId.ToString() + " - Success");
                         BookItem bookItem = ChooseBookItem(booksDataFromGoogle, book.Name, book.Authors.FirstOrDefault().Name);
                         if (bookItem != null)
                         {
                             UpdateBookRecord(context, book, bookItem);
                             context.SaveChanges();
                         }
                         else
                         {
                             UpdateBookRecordAsProcessed(context, book);
                             context.SaveChanges();
                         }
                     }
                     else
                     {
                         UpdateBookRecordAsProcessed(context, book);
                         context.SaveChanges();
                     }
                 }
                 catch
                 {
                 }
                 Thread.Sleep(2000);
             }
         }
         catch (SqlException ex)
         {
             if (ex.Message.Contains("provider: TCP Provider, error: 0"))
             {
                 string currentTime = Convert.ToString(DateTime.Now);
                 ApplicationLogger.WriteStringToLog("Error: can't connect to SQL server.");
                 Thread.Sleep(5000);
                 GoThroughBookRecords();
             }
         }
         catch (Exception ex)
         {
             ApplicationLogger.WriteStringToLog(ex.Message);
             throw ex;
         }
     }
 }