Ejemplo n.º 1
0
        private static async Task TryAddNewBook(BookContext bookContext, BookLink newBookLink)
        {
            // DbSet 不能使用 IEquatable.Equals,只能使用能轉換成 SQL 的 Linq 語法
            BookLink existingBook = await bookContext.BookLinks.FirstOrDefaultAsync(b => b.IndexPage == newBookLink.IndexPage);

            if (existingBook != null)
            {
                // 檢查書內每一冊,都要加入現有 DbSet
                existingBook.TryAddNewIssues(newBookLink);
                return;
            }
            // 此書不存在,直接加入 DbSet
            bookContext.BookLinks.Add(newBookLink);
        }
Ejemplo n.º 2
0
        private async void BtnGetIssues_OnClick(object sender, RoutedEventArgs e)
        {
            if (bookListbox.SelectedItem is BookshelfLink book)
            {
                btnGetIssues.IsEnabled = false;

                BookLink bookLinkFromWeb = await parser.FindBookIndexPageAsync(book.MainPage, CodePage.Gb2312);

                issueListbox.ItemsSource = bookLinkFromWeb.IssueLinks;
                if (bookLinkFromWeb.IssueLinks.Count > 0)
                {
                    issueListbox.SelectedIndex = 0;
                }
                labelIssue.Content = bookLinkFromWeb.Title;

                btnGetIssues.IsEnabled     = true;
                btnDownloadIssue.IsEnabled = true;
            }
        }
Ejemplo n.º 3
0
        static async Task Main(string[] args)
        {
            Test();
            string       username = File.ReadAllText(@"D:\Test Dir\LoginWebTest\username.txt");
            string       password = File.ReadAllText(@"D:\Test Dir\LoginWebTest\password.txt");
            Wenku8Parser parser   = new Wenku8Parser(username, password, Wenku8Parser.LoginDuration.OneDay);

            parser.Init();
            if (await parser.TryLogin())
            {
                parser.SaveCookie();
                List <BookshelfLink> myBooks = await parser.GetBooksFromBookshelf();

                foreach (var bookshelfLink in myBooks)
                {
                    BookLink bookLink = await parser.FindBookIndexPageAsync(bookshelfLink.MainPage);

                    Book book = await parser.GetBookAsync(bookLink);

                    book.SaveToTxt(AppDomain.CurrentDomain.BaseDirectory);
                }
            }
            else
            {
                Console.WriteLine("Login failed!");
            }

            Console.ReadLine();
            return;

            var baseAddress     = new Uri(LOGIN_PAGE);
            var cookieContainer = new CookieContainer();

            using (var handler = new HttpClientHandler()
            {
                CookieContainer = cookieContainer, AllowAutoRedirect = false
            })
            {
                using (httpClient = new HttpClient(handler)
                {
                    BaseAddress = baseAddress
                })
                {
                    try
                    {
                        /*
                         * //usually i make a standard request without authentication, eg: to the home page.
                         * //by doing this request you store some initial cookie values, that might be used in the subsequent login request and checked by the server
                         * var homePageResult = await httpClient.GetAsync("/");
                         * homePageResult.EnsureSuccessStatusCode();
                         * Console.WriteLine("Get / success.");
                         *
                         * // username=a45312&password=a45312&usecookie=0&action=login&submit=%26nbsp%3B%B5%C7%26nbsp%3B%26nbsp%3B%C2%BC%26nbsp%3B
                         * var content = new FormUrlEncodedContent(new[]
                         * {
                         *  //the name of the form values must be the name of <input /> tags of the login form, in this case the tag is <input type="text" name="username">
                         *  new KeyValuePair<string, string>("username", "a45312"),
                         *  new KeyValuePair<string, string>("password", "a45312"),
                         *  new KeyValuePair<string, string>("usecookie", "0"),
                         *  new KeyValuePair<string, string>("action", "login")
                         * });
                         * var loginResult = await httpClient.PostAsync("https://www.wenku8.net/login.php?do=submit", content);
                         * loginResult.EnsureSuccessStatusCode();
                         * Console.WriteLine("Login success.");
                         *
                         * //make the subsequent web requests using the same HttpClient object
                         * // get index page after login
                         * //string index = "https://www.wenku8.net/index.php";
                         * string bookCase = "https://www.wenku8.net/modules/article/bookcase.php";
                         * string htmlString = await GetPageBodyAsync(bookCase, CodePage.Gb2312);
                         *
                         * var htmlDoc = new HtmlDocument();
                         * htmlDoc.LoadHtml(htmlString);
                         * List<BookshelfLink> myBooks = GetBooksFromBookshelf(htmlDoc);
                         * foreach (var bookshelfLink in myBooks)
                         * {
                         *  await FindBookIndexPageAsync(bookshelfLink.Title);
                         * }*/

                        // select a node from bookcase

                        /*var checkFormNode = htmlDoc.DocumentNode.SelectSingleNode("//form[@name='checkform']");
                         * var tableNode = checkFormNode.SelectSingleNode("//table");
                         * if (tableNode != null)
                         * {
                         *  foreach (HtmlNode row in tableNode.SelectNodes("tr"))
                         *  {
                         *      var columns = row.SelectNodes("th|td");
                         *      if (columns != null && columns.Count >= 7)
                         *      {
                         *          var secondColumn = columns[1];
                         *          var link = secondColumn.Descendants("a").FirstOrDefault();
                         *          if (link != null)
                         *          {
                         *              string bookHref = link.Attributes["href"]?.Value;
                         *
                         *              string bookPage = await GetPageBodyAsync(bookHref, CodePage.Gb2312);
                         *              HtmlDocument bookDocument = new HtmlDocument();
                         *              bookDocument.LoadHtml(bookPage);
                         *              string indexHref = FindTargetHref(bookDocument, "小说目录");
                         *
                         *              bookPage = await GetPageBodyAsync(indexHref, CodePage.Gb2312);
                         *              HtmlDocument bookPageDocument = new HtmlDocument();
                         *              bookPageDocument.LoadHtml(bookPage);
                         *              GetChapterLinks(bookPageDocument, indexHref);
                         *          }
                         *      }
                         *  }
                         * }*/
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        throw;
                    }
                }
            }

            Console.ReadLine();
        }
Ejemplo n.º 4
0
        static async Task Main(string[] args)
        {
            //await Test();
            using (BookContext bookContext = new BookContext(sqliteString))
            {
                bookContext.Database.EnsureCreated();
                bookContext.Database.OpenConnection();

                // test with db
                bookContext.Set <ChapterLink>().Load();
                bookContext.Set <IssueLink>().Load();
                bookContext.Set <BookLink>().Load();
                var testBook0 = bookContext.BookLinks.FirstOrDefault();
                Console.WriteLine(testBook0?.IndexPage);

                string        username = File.ReadAllText(@"D:\Test Dir\LoginWebTest\username.txt");
                string        password = File.ReadAllText(@"D:\Test Dir\LoginWebTest\password.txt");
                Wenku8Parser2 parser   = new Wenku8Parser2(username, password, Wenku8Parser.LoginDuration.OneDay);
                parser.Init();
                if (await parser.TryLogin())
                {
                    parser.SaveCookie();
                    List <BookshelfLink> myBooks = await parser.GetBooksFromBookshelf();

                    foreach (var bookshelfLink in myBooks)
                    {
                        BookLink bookLinkFromWeb = await parser.FindBookIndexPageAsync(bookshelfLink.MainPage, CodePage.Gb2312);

                        // update book to database (update or add new)
                        await TryAddNewBook(bookContext, bookLinkFromWeb);

                        bookContext.SaveChanges();

                        // download book
                        parser.OnlyReturnNew = true;
                        BookLink bookLinkFromDB = await bookContext.BookLinks.FirstOrDefaultAsync(b => b.IndexPage == bookLinkFromWeb.IndexPage);

                        Book book = await parser.GetBookAsync(bookLinkFromDB, CodePage.Gb2312);

                        bookContext.SaveChanges();

                        book.SaveToTxt(Environment.CurrentDirectory);

                        // test with db
                        //bookContext = new BookContext();
                        //bookContext.Database.EnsureCreated();
                        //bookContext.Database.OpenConnection();
                        var testBook = await bookContext.BookLinks.FirstOrDefaultAsync();

                        Console.WriteLine(testBook.IndexPage);
                        //bookContext.Database.CloseConnection();
                        //bookContext.Dispose();
                        return;

                        // download book
                        //Book book = await parser.GetBookAsync(bookLink);

                        // test with db after get book content
                        bookContext.SaveChanges();
                        var testBook2 = await bookContext.BookLinks.FirstOrDefaultAsync();

                        Console.WriteLine(testBook2.IndexPage);
                        bookContext.Database.CloseConnection();

                        // save txt to file
                        //book.SaveToTxt(AppDomain.CurrentDomain.BaseDirectory);
                    }
                }
                else
                {
                    Console.WriteLine("Login failed!");
                }
            }

            Console.ReadLine();
        }