예제 #1
0
파일: AppDb.cs 프로젝트: sempers/book-utils
        public void Correct()
        {
            var corrections = BookCommonData.Corrections;

            OnNotify("Starting correction...");
            foreach (var book in db.Books)
            {
                book.Title = book.Title.Replace("’", "'").Replace("–", "-").Replace("&", "&").Replace("&", "&");
                if (!string.IsNullOrEmpty(book.Category))
                {
                    var _category = book.Category;
                    foreach (var correction in corrections)
                    {
                        if (correction.Key.Trim().StartsWith("*"))
                        {
                            continue;
                        }
                        var key   = correction.Key.Trim().Replace("*", "");
                        var value = correction.Value.Trim();
                        _category = _category.Replace(key, value);
                    }
                    if (_category.Contains(";"))
                    {
                        _category = _category.Split(';')[0];
                    }
                    book.SetCategory(_category); //no approving
                    book.Authors = book.Authors.Trim();
                }
                book.Summary = book.Summary?.Replace("…", "...");
            }
            db.SaveChanges();
            LastAction = BookActions.Correct;
            OnNotify("Correction finished.");
        }
예제 #2
0
파일: AppDb.cs 프로젝트: sempers/book-utils
 public void ClearDbFile()
 {
     db.SaveChanges();
     db.Dispose();
     db = null;
     File.Delete(@"..\..\books.db");
     LastAction = BookActions.Clear;
 }
예제 #3
0
        public ActionResult Create([Bind(Include = "GenreId,GenreName")] Genre genre)
        {
            if (ModelState.IsValid)
            {
                int result = BookActions.GenreSave(genre.GenreName);

                return(RedirectToAction("Index"));
            }

            return(View(genre));
        }
예제 #4
0
 public BookWindow(Book model, MainWindow main, BookActions action = BookActions.Add)
 {
     InitializeComponent();
     this.Owner  = main;
     this.model  = model;
     DataContext = this.model;
     db          = main.Model.Db;
     this.action = action;
     if (this.action == BookActions.Add)
     {
         btnRemove.Visibility = Visibility.Hidden;
     }
 }
예제 #5
0
        // GET: Genres/Delete/5
        public ActionResult Delete(int id)
        {
            if (BookActions.BookGenreCheck(id))
            {
                Genre genre = db.Genres.Find(id);
                db.Genres.Remove(genre);
                db.SaveChanges();
            }
            else
            {
                TempData["bookExists"] = "true";
            }

            return(RedirectToAction("Index"));
        }
예제 #6
0
        public ActionResult Search(string search, FormCollection formCollection)
        {
            bool chkSwitch = false;

            if (formCollection["chkSwitch"] == "on")
            {
                chkSwitch = true;
            }
            List <CompleteBook> books = BookActions.Search(search, chkSwitch);

            TempData["books"] = books;


            //i'm passing model to a different controller so I have to use TempData
            //TempData = short lived session used for redirects
            return(RedirectToAction("Index", "Home"));
        }
예제 #7
0
        public ActionResult SaveUpdateBook(int hiddenId, string Title, string AuthorName, string AuthorSurname, int GenreId, HttpPostedFileBase ImageUrl)
        {
            if (ModelState.IsValid)
            {
                //New Book modal has data-id=0
                //Update Book modal has data-id=BookId
                if (hiddenId == 0)
                {
                    BookActions.BookSave(Title, AuthorName, AuthorSurname, GenreId, ImageUrl);
                }
                else
                {
                    int BookId = hiddenId;
                    BookActions.BookUpdate(BookId, Title, AuthorName, AuthorSurname, GenreId, ImageUrl);
                }
            }

            return(RedirectToAction("Index", "Home"));
        }
예제 #8
0
파일: AppDb.cs 프로젝트: sempers/book-utils
        public async Task <int> UpdateEpubsFromWeb()
        {
            OnNotify("Updating epubs...");
            var          web        = new HtmlWeb();
            HtmlDocument detailPage = null;
            var          list       = db.Books.Where(b => b.Extension == "epub").ToList();
            var          counter    = 0;

            foreach (var book in list)
            {
                OnNotify($"Updating epubs {++counter}/{list.Count}...");
                detailPage = await web.LoadFromWebAsync(book.Url);

                var downloadLinks = detailPage.DocumentNode.SelectNodes("//span[@class='download-links']/a");
                foreach (var node in downloadLinks)
                {
                    var href = node.Attributes["href"].Value;
                    if (href.Contains(".pdf"))
                    {
                        book.DownloadUrl = href;
                        book.Extension   = "pdf";
                        break;
                    }
                    else if (href.Contains(".epub"))
                    {
                        book.DownloadUrl = href;
                        book.Extension   = "epub";
                    }
                    else if (href.Contains("file.html"))
                    {
                        book.DownloadUrl = href;
                        book.Extension   = "rar";
                    }
                }
            }
            db.SaveChanges();
            LastAction = BookActions.Update;
            OnNotify("Updating completed...");
            return(counter);
        }
예제 #9
0
 // POST: BookActions/Delete/5
 //[HttpPost]
 public ActionResult Delete(int id, string image)
 {
     try
     {
         string path;
         // delete book from db and image file from server
         if (image != "noimage.jpg")
         {
             path = Request.MapPath("~/Content/Images/" + image);
         }
         else
         {
             path = "";
         }
         BookActions.BookDelete(id, path);
         return(RedirectToAction("Index", "Home"));
     }
     catch
     {
         return(RedirectToAction("Index", "Home"));
     }
 }
예제 #10
0
파일: AppDb.cs 프로젝트: sempers/book-utils
        public async Task <int> UpdateDbFromWeb_ORG()
        {
            var corrections = BookCommonData.Corrections;

            try
            {
                OnNotify("Updating all from web (ORG) ...");
                HtmlDocument pageHtml = null;
                var          web      = new HtmlWeb();
                var          postIDs  = db.Books.Select(b => b.PostId).Distinct();
                var          URL      = "http://www.allitebooks.org/page";
                pageHtml = await web.LoadFromWebAsync("http://www.allitebooks.org");

                var pagination = pageHtml.DocumentNode.SelectNodes("//div[@class='pagination clearfix']/a");
                PAGES_NUM = int.Parse(pagination.Last().InnerText);
                int MAX_THERE           = 10;
                var alreadyThereCounter = 0;
                int booksAdded          = 0;

                for (var page = 0; page <= PAGES_NUM; page++)
                {
                    var pageList    = new List <Book>();
                    var pageErrUrls = new List <string>();
                    pageHtml = await web.LoadFromWebAsync($"{URL}/{page}");

                    var pageBookNodes = pageHtml.DocumentNode.SelectNodes("//article");
                    //цикл по нодам страницы
                    foreach (var bookElement in pageBookNodes)
                    {
                        var book = new Book();
                        try
                        {
                            book.PostId = int.Parse(bookElement.Attributes["id"].Value.Substring(5));
                            if (postIDs.Contains(book.PostId))
                            {
                                alreadyThereCounter++;
                                if (alreadyThereCounter <= MAX_THERE) // продолжаем цикл
                                {
                                    continue;
                                }
                                else
                                {
                                    break; //убрали goto
                                }
                            }
                            //Adding book
                            //Primary properties
                            book.Title   = bookElement.SelectSingleNode("div/header/h2[@class='entry-title']/a")?.InnerText;
                            book.Title   = book.Title?.Trim().Replace("&#8217;", "'").Replace("&#8211;", "-").Replace("&#038;", "&").Replace("&amp;", "&");
                            book.Url     = bookElement.SelectSingleNode("div/header/h2[@class='entry-title']/a")?.Attributes["href"]?.Value?.Trim();
                            book.Summary = bookElement.SelectSingleNode("div/div[@class='entry-summary']/p")?.InnerText;
                            book.Sync    = 0;
                            //Secondary properties
                            book = await UpdateBookFromWeb_ORG(book);

                            OnNotify($"Loading page {page}...");
                            pageList.Add(book);
                        }
                        catch (Exception e)
                        {
                            pageErrUrls.Add(book.Url);
                        }
                    }
                    //сюда уходит goto
                    foreach (var b in pageList)
                    {
                        if (!db.Books.Any(_b => _b.PostId == b.PostId))
                        {
                            db.Books.Add(b);
                            booksAdded++;
                        }
                    }
                    await db.SaveChangesAsync();

                    File.AppendAllLines("log.txt", pageErrUrls.ToArray());
                    OnNotify($"Page {page} saved.");
                    if (alreadyThereCounter > MAX_THERE)
                    {
                        break;
                    }
                    Thread.Sleep(DELAY);
                }
                OnNotify($"Updating all finished. Added {booksAdded} new books.");
                LastAction = BookActions.Update;
                return(booksAdded);
            }
            catch (Exception e)
            {
                OnNotify($"Error while updating from web: {e.Message}");
                return(0);
            }
        }
예제 #11
0
파일: AppDb.cs 프로젝트: sempers/book-utils
 public void RemoveBook(Book model)
 {
     db.Books.Remove(model);
     db.SaveChanges();
     LastAction = BookActions.Remove;
 }
예제 #12
0
파일: AppDb.cs 프로젝트: sempers/book-utils
 public void AddBook(Book model)
 {
     db.Books.Add(model);
     db.SaveChanges();
     LastAction = BookActions.Add;
 }
예제 #13
0
파일: AppDb.cs 프로젝트: sempers/book-utils
 public void Save()
 {
     db.SaveChanges();
     LastAction = BookActions.Save;
 }
예제 #14
0
 // GET: Genres
 public ActionResult Index()
 {
     return(View(BookActions.GetGenres()));
 }
예제 #15
0
        public ActionResult CreateGenre(string genre)
        {
            int genreId = BookActions.GenreSave(genre);

            return(Json(new { genreId = genreId }));
        }
예제 #16
0
파일: AppDb.cs 프로젝트: sempers/book-utils
        public async Task <int> UpdateDbFromWeb_IN()
        {
            var corrections = BookCommonData.Corrections;

            try
            {
                OnNotify("Updating all from web (IN) ...");
                HtmlDocument pageHtml   = null;
                var          web        = new HtmlWeb();
                var          maxPostID  = db.Books.Select(b => b.PostId).Max();
                var          bookTitles = db.Books.Select(b => b.Title);
                pageHtml = await web.LoadFromWebAsync(Settings.Default.WebUrl);

                var pagination = pageHtml.DocumentNode.SelectNodes("//span[@class='pages']");
                var pagText    = pagination.First().InnerText;
                var lastnum    = pagText.Substring(pagText.LastIndexOf(' ') + 1);
                PAGES_NUM = int.Parse(lastnum);
                int MAX_THERE           = 10;
                var alreadyThereCounter = 0;
                int booksAdded          = 0;

                for (var page = 1; page <= PAGES_NUM; page++)
                {
                    var newBooks    = new List <Book>();
                    var pageErrUrls = new List <string>();
                    pageHtml = await web.LoadFromWebAsync($"{Settings.Default.WebUrl}/page/{page}");

                    var pageBookNodes = pageHtml.DocumentNode.SelectNodes("//div[@class='meta-info-container']");  //h3[@class='entry-title td-module-title']/a");
                                                                                                                   //цикл по нодам страницы
                    foreach (var bookNode in pageBookNodes)
                    {
                        var book = new Book();
                        try
                        {
                            var h3    = bookNode.SelectSingleNode("div/div/h3/a");
                            var title = h3.Attributes["title"].Value.Trim().Replace("&#8217;", "'").Replace("&#8211;", "-").Replace("&#038;", "&").Replace("&amp;", "&");

                            if (bookTitles.Contains(title))
                            {
                                alreadyThereCounter++;
                                if (alreadyThereCounter <= MAX_THERE) // продолжаем цикл
                                {
                                    continue;
                                }
                                else
                                {
                                    break; //убрали goto
                                }
                            }
                            //Adding book
                            //Primary properties -> goto Details
                            book.Title = title;
                            book.Url   = bookNode.SelectSingleNode("div/div[@class='td-read-more']/a").Attributes["href"].Value;
                            //Secondary properties
                            book = await UpdateBookFromWeb_IN(book);

                            OnNotify($"Loading page {page}...");
                            newBooks.Add(book);
                        }
                        catch (Exception e)
                        {
                            pageErrUrls.Add(book.Url);
                        }
                    }
                    //сюда уходит goto
                    foreach (var b in newBooks)
                    {
                        if (!db.Books.Any(_b => _b.PostId == b.PostId))
                        {
                            db.Books.Add(b);
                            booksAdded++;
                        }
                    }
                    await db.SaveChangesAsync();

                    File.AppendAllLines("log.txt", pageErrUrls.ToArray());
                    OnNotify($"Page {page} saved.");
                    if (alreadyThereCounter > MAX_THERE)
                    {
                        break;
                    }
                    Thread.Sleep(DELAY);
                }
                OnNotify($"Updating all finished. Added {booksAdded} new books.");
                LastAction = BookActions.Update;
                return(booksAdded);
            }
            catch (Exception e)
            {
                OnNotify($"Error while updating from web: {e.Message}");
                return(0);
            }
        }
예제 #17
0
 public void WhenGetTheTitle()
 {
     _actualTitle = BookActions.GetTitle(api.GetBook(_code));
 }
예제 #18
0
 public ActionResult SideBar()
 {
     //notice that a PartialView is being returned
     return(PartialView(BookActions.GetGenres()));
 }