private void UpdateBookRecord(Entities context, Book book, BookItem bookItem) { if (book.Cover == null && bookItem.volumeInfo.imageLinks != null && bookItem.volumeInfo.imageLinks.thumbnail != null) { book.Cover = DownloadCover(bookItem.volumeInfo.imageLinks.thumbnail); book.CoverUpdated = DateTime.Now; } if (string.IsNullOrEmpty(book.Description) && !string.IsNullOrEmpty(bookItem.volumeInfo.description)) { book.Description = bookItem.volumeInfo.description; } if (book.PagesCount.GetValueOrDefault(0) == 0 && bookItem.volumeInfo.pageCount > 0) { book.PagesCount = bookItem.volumeInfo.pageCount; } if (!string.IsNullOrEmpty(bookItem.volumeInfo.publisher)) { book.Publisher = bookItem.volumeInfo.publisher; } if (bookItem.volumeInfo.publishedDate != null) { try { book.PublishDate = Convert.ToDateTime(bookItem.volumeInfo.publishedDate); } catch { } } if (bookItem.volumeInfo.industryIdentifiers != null) { book.ISBN = GetISBN(bookItem.volumeInfo.industryIdentifiers); } UpdateBookRecordAsProcessed(context, book); }
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); }
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; } } }