Beispiel #1
0
 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);
 }
Beispiel #2
0
 private void LoadAuthorsDataFromWiki(Entities context, System.Collections.Generic.ICollection<Author> authors)
 {
     foreach (Author author in authors)
     {
         new AuthorsDataFromWiki(context, author);
     }
 }
Beispiel #3
0
 private void UpdateBookRecordAsProcessed(Entities context, Book book)
 {
     book.FromGoogleBooks = true;
     if (book.CoverUpdated == null)
     {
         book.CoverUpdated = DateTime.Now;
     }
 }
Beispiel #4
0
 public InpBookLoader(InpBookRecord bookRecord, AuthorLoader authorLoader, GenresLoader genresLoader, SeriesLoader seriesLoader, Entities context)
 {
     this.bookRecord = bookRecord;
     this.authorLoader = authorLoader;
     this.genresLoader = genresLoader;
     this.seriesLoader = seriesLoader;
     this.context = context;
     ProcessBook();
 }
Beispiel #5
0
 private static void UploadDataFromWiki()
 {
     using (Entities context = new Entities())
     {
         foreach (Author author in context.Authors.Where(x => !x.FromWiki))
         {
             new AuthorsDataFromWiki(context, author);
             context.SaveChanges();
             ApplicationLogger.WriteStringToLog("Processed \"" + author.AuthorId.ToString() + "\": " + author.Name);
         }
     }
 }
Beispiel #6
0
 private void UploadBookAndEntities()
 {
     using (Entities context = new Entities())
     {
         try
         {
             AuthorLoader authorLoader = new AuthorLoader(bookSources.BookRecord, context);
             if (authorLoader.Authors != null)
             {
                 GenresLoader genresLoader = new GenresLoader(bookSources.BookRecord, context);
                 SeriesLoader seriesLoader = new SeriesLoader(bookSources.BookRecord, context);
                 InpBookLoader bookLoader = new InpBookLoader(bookSources.BookRecord, authorLoader, genresLoader, seriesLoader, context);
                 if (bookLoader.Book != null)
                 {
                     try
                     {
                         FB2BookUploader fb2Loader = new FB2BookUploader(bookLoader.Book, bookSources.BookFileName, context);
                         try
                         {
                             LoadAuthorsDataFromWiki(context, bookLoader.Book.Authors);
                         }
                         catch { }
                         context.SaveChanges();
                         ApplicationLogger.WriteStringToLog("Book \"" + bookLoader.Book.Name + "\" has been uploaded successfully.");
                     }
                     catch (Exception ex)
                     {
                         ApplicationLogger.WriteStringToLog("Book from file: \"" + Path.GetFileName(bookSources.BookFileName) + "\" has not been uploaded because: \r\n" + ex.Message);
                     }
                 }
                 else
                 {
                     ApplicationLogger.WriteStringToLog("Book from file: \"" + Path.GetFileName(bookSources.BookFileName) + "\" has not been uploaded because exist in the database.");
                 }
             }
             else
             {
                 ApplicationLogger.WriteStringToLog("Book from file: \"" + Path.GetFileName(bookSources.BookFileName) + "\" has not been uploaded because couldn't parse authors.");
             }
         }
         catch (Exception ex)
         {
             //transaction.Rollback();
             throw new Exception("UploadBookAndEntities:\r\n" + ex.Message);
         }
     }
 }
Beispiel #7
0
 static void Main(string[] args)
 {
     string updateSql = string.Empty;
     using (Entities context = new Entities())
     {
         foreach (Author author in context.Authors)
         {
             string newAuthorName = ProcessAuthorName(author.Name);
             if (!author.Name.Equals(newAuthorName))
             {
                 updateSql += "Update Authors set Name='" + newAuthorName + "' where AuthorId='" +
                     author.AuthorId + "';\r\n";
                 Console.WriteLine(author.AuthorId + ":" + author.Name + "->" + newAuthorName);
             }
         }
     }
     WriteStringToFile("UpdateAuthors.sql", updateSql);
 }
Beispiel #8
0
 public GenresLoader(InpBookRecord bookRecord, Entities context)
 {
     this.bookRecord = bookRecord;
     this.context = context;
     ProcessGenres(bookRecord.Genres);
     if (genres == null || genres.Count == 0)
     {
         Genre defaultGenre;
         if (IsGenreExist(DEFAULT_GENRE_NAME, out defaultGenre))
         {
             if (genres == null)
             {
                 genres = new List<Genre>();
             }
             genres.Add(defaultGenre);
         }
     }
 }
Beispiel #9
0
 public AuthorsDataFromWiki(Entities context, Author author)
 {
     if (AuthorNameIsValidated(author.Name))
     {
         AuthorPage authorPage = new AuthorPage(author.Name);
         ProcessAuthorPage processAuthorPage = new ProcessAuthorPage(authorPage.AuthorPageUrl, authorPage.AuthorPageHtmlDocument);
         if (processAuthorPage.AuthorData != null)
         {
             UpdateAuthorResources(context, author, processAuthorPage.AuthorData);
         }
         else
         {
             UpdateAuthorState(context, author);
         }
     }
     else
     {
         UpdateAuthorState(context, author);
     }
 }
Beispiel #10
0
 private void UpdateAuthorResources(Entities context, Author author, AuthorData authorData)
 {
     string sql = string.Empty;
     if (!string.IsNullOrEmpty(authorData.Biography))
     {
         author.Biography = authorData.Biography;
         author.NameFromWiki = authorData.NameFromWiki;
         author.Photo = authorData.AuthorImage;
         author.PhotoUpdated = DateTime.Now;
         if (authorData.BornDate > new DateTime(1753, 1, 1))
         {
             author.BirthDate = authorData.BornDate;
         }
         if (authorData.DeathDate > new DateTime(1753, 1, 1))
         {
             author.DeathDate = authorData.DeathDate;
         }
         author.SourceUrl = authorData.OriginalUrl;
         author.FromWiki = true;
     }
 }
Beispiel #11
0
 private void UpdateAuthorState(Entities context, Author author)
 {
     author.PhotoUpdated = DateTime.Now;
     author.FromWiki = true;
 }
Beispiel #12
0
 public AuthorLoader(string authorsRecord, Entities context)
 {
     this.context = context;
     string[] authors = authorsRecord.Split(':');
     ProcessAuthorsArray(authors);
 }
Beispiel #13
0
 public AuthorLoader(InpBookRecord bookRecord, Entities context)
 {
     this.bookRecord = bookRecord;
     this.context = context;
     ProcessAuthorsArray(bookRecord.Authors);
 }
Beispiel #14
0
 private void GetLastBookIdFromDatabase()
 {
     using (Entities context = new Entities())
     {
         int maxFileId = context.BookContents.Select(s => s.SourceFileName.Replace(".fb2", "")).ToList().Max(n=>int.Parse(n));
         lastBookIdFromDatabase = maxFileId;
         ApplicationLogger.WriteStringToLog("Last uploaded book Id = " + lastBookIdFromDatabase.ToString());
     }
 }
Beispiel #15
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;
         }
     }
 }
Beispiel #16
0
 private static void ProcessBooks()
 {
     List<Guid> books = null;
     using (Entities context = new Entities())
     {
         books = context.Flags.Where(x => x.Name.ToLower().Equals("sources uploaded") && x.State == 0).Select(x => (Guid)x.BookId).ToList();
     }
     foreach (Guid bookId in books)
     {
         ProcessBook(bookId, 1);
     }
 }
Beispiel #17
0
 private static void ProcessBook(Guid bookID, int iterator)
 {
     try
     {
         using (Entities context = new Entities())
         {
             Book book = context.Books.FirstOrDefault(x => x.BookId == bookID);
             if (book.BookContents.Count > 0)
             {
                 ApplicationLogger.WriteStringToLog("Process book: " + book.BookId.ToString());
                 Flag flag = context.Flags.FirstOrDefault(x => x.Name.ToLower().Equals("sources uploaded") && x.BookId == bookID);
                 UploadBookSources(book, flag);
                 context.SaveChanges();
             }
         }
     }
     catch (Exception e)
     {
         if (iterator <= 10)
         {
             ApplicationLogger.WriteStringToError(e.Message);
             Thread.Sleep(2000);
             iterator = iterator++;
             ProcessBook(bookID, iterator);
         }
         else
         {
             ApplicationLogger.WriteStringToLog("Application can't commit cahnges to the database because:\r\n");
             throw e;
         }
     }
 }
Beispiel #18
0
 public SeriesLoader(InpBookRecord bookRecord, Entities context)
 {
     this.bookRecord = bookRecord;
     this.context = context;
     ProcesSeries();
 }
Beispiel #19
0
 protected Entities GetContext()
 {
     var result = new Entities();
     return result;
 }