예제 #1
0
        private async Task LoadForumDataAsync(CancellationTokenSource cts)
        {
            if (_forumData.Count > 0)
            {
                return;
            }

            // 读取数据
            string url         = "http://www.hi-pda.com/forum/search.php";
            string htmlContent = await _httpClient.GetAsync(url, cts);

            // 实例化 HtmlAgilityPack.HtmlDocument 对象
            HtmlDocument doc = new HtmlDocument();

            // 载入HTML
            doc.LoadHtml(htmlContent);
            var data = doc.DocumentNode;

            var selectNode = data.Descendants().FirstOrDefault(n => n.Name.Equals("select") && n.GetAttributeValue("id", "").Equals("srchfid"));

            if (selectNode == null)
            {
                return;
            }

            var groups = selectNode.ChildNodes.Where(n => n.Name.Equals("optgroup"));

            if (groups == null)
            {
                return;
            }

            foreach (var group in groups)
            {
                string forumGroupName = group.Attributes[0].Value.Replace("--", string.Empty);
                var    forumGroup     = new ForumCategoryModel {
                    ForumGroupName = forumGroupName
                };

                var groupItems = group.ChildNodes.Where(n => n.Name.Equals("option"));
                if (groupItems == null)
                {
                    return;
                }

                foreach (var item in groupItems)
                {
                    int    forumId   = Convert.ToInt32(item.Attributes[0].Value);
                    string forumName = item.NextSibling.InnerText;
                    forumName = forumName.Replace(" ", string.Empty);
                    forumName = forumName.Trim();

                    forumGroup.Forums.Add(new ForumModel {
                        Id = forumId, Name = forumName
                    });
                }

                _forumData.Add(forumGroup);
            }
        }
예제 #2
0
        public async Task <ActionResult> Category(int id)
        {
            using (var context = new ApplicationDbContext())
            {
                var category = await context.ForumCategories.FirstOrDefaultAsync(x => x.Id == id && !x.IsDeleted);

                if (category == null)
                {
                    return(View("ViewMessage", new ViewMessageModel(ViewMessageType.Warning, Resources.Forum.categoryNotFoundErrorTitle, string.Format(Resources.Forum.categoryNotFoundErrorMessage, id))));
                }

                var model = new ForumCategoryModel
                {
                    Id    = category.Id,
                    Name  = category.Name,
                    Icon  = category.Icon,
                    Forum = new ForumModel
                    {
                        Id   = category.Forum.Id,
                        Name = category.Forum.Title,
                        Icon = category.Forum.Icon
                    },
                    Threads = category.Threads.Where(x => !x.IsDeleted).OrderByDescending(x => x.IsPinned).ThenByDescending(x => x.LastUpdate).Select(x => new ForumThreadModel
                    {
                        Id          = x.Id,
                        Name        = x.Title,
                        Description = x.Description,
                        PostCount   = x.Posts.Count(g => !g.IsDeleted),
                        IsPinned    = x.IsPinned,
                        Icon        = x.Icon
                    }).ToList(),
                    LatestPosts = category.Threads.Where(x => !x.IsDeleted).SelectMany(x => x.Posts).Where(x => !x.IsDeleted).OrderByDescending(x => x.Id).Take(20).Select(x => new ForumPostModel
                    {
                        Id          = x.Id,
                        Message     = x.Message,
                        ThreadTitle = x.Thread.Title,
                        ThreadId    = x.ThreadId,
                        ForumName   = x.Thread.Category.Name
                    }).ToList()
                };

                var sanitizer = new HtmlSanitizer(allowedTags: new List <string> {
                    "p"
                });
                foreach (var message in model.LatestPosts)
                {
                    message.Message = sanitizer.Sanitize(message.Message).Replace("<p>", "").Replace("</p>", "").Trim();
                }

                foreach (var threadModel in model.Threads)
                {
                    var thread = category.Threads.Where(x => !x.IsDeleted).FirstOrDefault(x => x.Id == threadModel.Id);
                    if (thread != null)
                    {
                        var lastPost = thread.Posts.Where(x => !x.IsDeleted).OrderByDescending(x => x.Id).FirstOrDefault();
                        if (lastPost != null)
                        {
                            threadModel.LastPost = new ForumPostModel
                            {
                                Id        = lastPost.Id,
                                Timestamp = lastPost.Timestamp,
                                User      = new ForumUser
                                {
                                    Name = lastPost.User.UserName
                                }
                            };
                        }
                    }
                }

                return(View("Category", model));
            }
        }