Example #1
0
        private async Task <Book> GetBookFromDangdang(string isbn)
        {
            var   keywords     = _configuration["Dangdang:TitleKeywords"];
            Regex oldBookRegex = new Regex(keywords);

            try
            {
                string dangdangSearchUrl = @"http://search.dangdang.com/?key={0}&act=input";
                var    url = string.Format(dangdangSearchUrl, isbn);

                var htmlStr = await _htmlService.GetHtmlString(url, Encoding.GetEncoding(936), Encoding.Default);

                var dom = await _parser.ParseAsync(htmlStr);

                List <IHtmlListItemElement> links = new List <IHtmlListItemElement>();
                _htmlService.FindAllHtmlElement(links, dom.Body);

                var  allLines = links.Where(c => !c.ClassName.IsNullOrEmpty() && c.ClassName.StartsWith("line"));
                Book book     = null;
                foreach (var line in allLines)
                {
                    if (!(line.Children[0] is IHtmlAnchorElement firstLink))
                    {
                        continue;
                    }

                    if (oldBookRegex.Match(firstLink.Title).Success)
                    {
                        continue;
                    }

                    var href      = firstLink.Href;
                    var productId = _htmlService.GetNumber(href);

                    var bookDetailHtmlStr = await _htmlService.GetHtmlString(href, Encoding.GetEncoding(936), Encoding.Default);

                    if (!bookDetailHtmlStr.Contains(isbn))
                    {
                        continue;
                    }

                    book = await _bookAppService.SaveDangdangBookAsync(isbn, productId, bookDetailHtmlStr);

                    if (book != null)
                    {
                        break;
                    }
                }

                return(book);
            }
            catch (System.Exception ex)
            {
                Logger.Warn($"获取{isbn}的书籍信息时出现错误:{ex}");
                return(null);
            }
        }
Example #2
0
        private async Task InsertBookAsync(int doubanBookId, string isbn, IHtmlDocument dom)
        {
            List <IHtmlHeadingElement> headingElements = new List <IHtmlHeadingElement>();

            _htmlService.FindAllHtmlElement(headingElements, dom.Body);
            var bookTitleElement = headingElements.FirstOrDefault(c => c.TagName == "H1");

            if (bookTitleElement == null)
            {
                return;
            }

            var header = bookTitleElement.Children.OfType <IHtmlSpanElement>().ToList();
            var title  = header[0].InnerText;

            var doubanBook = new Book();

            doubanBook.DoubanBookId = doubanBookId;
            doubanBook.Name         = title;
            doubanBook.Isbn         = isbn;

            if (!_bookRepository.GetAll().Any(c => c.DoubanBookId == doubanBookId))
            {
                await _bookRepository.InsertAsync(doubanBook);
            }
        }