Exemple #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);
            }
        }