async Task <UserMessageDataModel> LoadUserMessageDataAsync(int userId, int limitCount, CancellationTokenSource cts) { var listData = new ObservableCollection <UserMessageItemModel>(); int total = 0; // 读取数据 string url = string.Format("http://www.hi-pda.com/forum/pm.php?uid={0}&filter=privatepm&daterange=5&_={1}", userId, DateTime.Now.Ticks.ToString("x")); string htmlContent = await _httpClient.GetAsync(url, cts); // 实例化 HtmlAgilityPack.HtmlDocument 对象 HtmlDocument doc = new HtmlDocument(); // 载入HTML doc.LoadHtml(htmlContent); // 最先读取提醒数据 var promptContentNode = doc.DocumentNode.Descendants().FirstOrDefault(n => n.Name.Equals("div") && n.GetAttributeValue("class", "").Equals("promptcontent")); PromptService.GetPromptData(promptContentNode); var messageListNode = doc.DocumentNode.Descendants().FirstOrDefault(n => n.GetAttributeValue("class", "").Equals("pm_list")); if (messageListNode != null) { var nodeList = messageListNode.Descendants().Where(n => n.GetAttributeValue("id", "").StartsWith("pm_")); if (nodeList != null) { total = nodeList.Count(); if (limitCount != -1 && total > limitCount) { nodeList = nodeList.Skip(total - limitCount); } foreach (var item in nodeList) { listData.Add(GetUserMessageItem(item)); } } } // 清除此用户的私信标识为“NEW”状态的 toast temp data ToastService.ClearPmToastTempData(userId); return(new UserMessageDataModel { ListData = listData, Total = total }); }
public static void GetPromptData(HtmlNode promptContentNode) { try { if (promptContentNode != null) { var promtpViewModel = MainPageViewModel.GetInstance(); var ulNode = promptContentNode.ChildNodes[1]; promtpViewModel.PromptPm = Convert.ToInt32(ulNode.ChildNodes[0].InnerText.Trim().Substring("私人消息 (".Length).Replace(")", string.Empty)); promtpViewModel.PromptAnnouncePm = Convert.ToInt32(ulNode.ChildNodes[1].InnerText.Trim().Substring("公共消息 (".Length).Replace(")", string.Empty)); promtpViewModel.PromptSystemPm = Convert.ToInt32(ulNode.ChildNodes[2].InnerText.Trim().Substring("系统消息 (".Length).Replace(")", string.Empty)); promtpViewModel.PromptFriend = Convert.ToInt32(ulNode.ChildNodes[3].InnerText.Trim().Substring("好友消息 (".Length).Replace(")", string.Empty)); promtpViewModel.PromptThreads = Convert.ToInt32(ulNode.ChildNodes[4].InnerText.Trim().Substring("帖子消息 (".Length).Replace(")", string.Empty)); promtpViewModel.PromptNoticeCountInToastTempData = ToastService.GetNoticeCountFromNoticeToastTempData(); ToastService.UpdateBadge(promtpViewModel.PromptAllWithoutPromptPm + promtpViewModel.PromptPm); } } catch (Exception e) { string errorDetails = string.Format("{0}", e.Message); CommonService.PostErrorEmailToDeveloper("提醒数据解析出现异常", errorDetails); } }
async Task <List <NoticeItemViewModel> > LoadNoticeDataAsync(CancellationTokenSource cts) { var data = new List <NoticeItemViewModel>(); string url = string.Format("http://www.hi-pda.com/forum/notice.php?_={0}", DateTime.Now.Ticks.ToString("x")); string htmlStr = await _httpClient.GetAsync(url, cts); // 实例化 HtmlAgilityPack.HtmlDocument 对象 HtmlDocument doc = new HtmlDocument(); // 载入HTML doc.LoadHtml(htmlStr); var items = doc.DocumentNode.Descendants().FirstOrDefault(n => n.Name.Equals("ul") && n.GetAttributeValue("class", "").Equals("feed"))?.ChildNodes; if (items == null) { return(null); } foreach (var item in items) { NoticeType noticeType; bool isNew = false; string userId = string.Empty; string username = string.Empty; string actionTime = string.Empty; string threadId = string.Empty; string threadTitle = string.Empty; string originalText = string.Empty; string actionText = string.Empty; string repostStr = string.Empty; string postId = string.Empty; HtmlNode userLinkNode; HtmlNode threadLinkNode; var divNode = item.ChildNodes[0]; string nodeClass = divNode.Attributes[0].Value.Trim().ToLower(); switch (nodeClass) { case "f_quote": case "f_reply": noticeType = NoticeType.QuoteOrReply; userLinkNode = divNode.ChildNodes[0]; userId = userLinkNode.Attributes[0].Value.Substring("http://www.hi-pda.com/forum/space.php?from=notice&uid=".Length).Split('&')[0]; username = userLinkNode.InnerText.Trim(); threadLinkNode = divNode.ChildNodes[2]; threadId = threadLinkNode.Attributes[0].Value.Substring("http://www.hi-pda.com/forum/viewthread.php?from=notice&tid=".Length).Split('&')[0]; threadTitle = threadLinkNode.InnerText.Trim(); actionTime = divNode.ChildNodes[4].InnerText.Trim(); HtmlNode actionContentNode; HtmlNode buttonsNode; if (divNode.ChildNodes[5].Name.Equals("img")) { isNew = true; actionContentNode = divNode.ChildNodes[7]; buttonsNode = divNode.ChildNodes[9]; } else { actionContentNode = divNode.ChildNodes[6]; buttonsNode = divNode.ChildNodes[8]; } originalText = actionContentNode.ChildNodes[0].ChildNodes[1].ChildNodes[0] .InnerText.Trim() .Replace("\r", " ") .Replace("\n", " "); actionText = actionContentNode.ChildNodes[0].ChildNodes[1].ChildNodes[2] .InnerText.Trim() .Replace("\r", " ") .Replace("\n", " "); var replyLinkNode = buttonsNode.ChildNodes[0]; repostStr = replyLinkNode.Attributes[0].Value.Substring("http://www.hi-pda.com/forum/post.php?from=notice&action=reply&fid=2&tid=1778684&reppost=".Length).Split('&')[0]; var viewLinkNode = buttonsNode.ChildNodes[2]; postId = viewLinkNode.Attributes[0].Value.Substring("http://www.hi-pda.com/forum/redirect.php?from=notice&goto=findpost&pid=".Length).Split('&')[0]; data.Add(new NoticeItemViewModel(noticeType, isNew, username, actionTime, new string[] { userId, // 0 threadId, // 1 threadTitle, // 2 originalText, // 3 actionText, // 4 repostStr, // 5 postId // 6 })); break; case "f_thread": noticeType = NoticeType.Thread; var nodes = divNode.ChildNodes; var usernames = new List <string>(); var usernameNodes = nodes.Where(n => n.Name.Equals("a") && n.Attributes[0].Value.StartsWith("space.php?username="******",", usernames); threadLinkNode = nodes.FirstOrDefault(n => n.Name.Equals("a") && n.Attributes[0].Value.StartsWith("http://www.hi-pda.com/forum/redirect.php?from=notice&goto=findpost&pid=")); string linkUrlStr = threadLinkNode.Attributes[0].Value.Substring("http://www.hi-pda.com/forum/redirect.php?from=notice&goto=findpost&pid=".Length).Replace("ptid=", string.Empty); string[] idsAry = linkUrlStr.Split('&'); postId = idsAry[0]; threadId = idsAry[1]; threadTitle = threadLinkNode.InnerText.Trim(); actionTime = nodes.FirstOrDefault(n => n.Name.Equals("em")).InnerText.Trim(); isNew = nodes.Count(n => n.Name.Equals("img") && n.GetAttributeValue("alt", "").Equals("NEW")) == 1; data.Add(new NoticeItemViewModel(noticeType, isNew, username, actionTime, new string[] { threadId, threadTitle, postId })); break; case "f_buddy": noticeType = NoticeType.Buddy; userLinkNode = divNode.ChildNodes[0]; userId = userLinkNode.Attributes[0].Value.Substring("http://www.hi-pda.com/forum/space.php?from=notice&uid=".Length); username = userLinkNode.InnerText.Trim(); actionTime = divNode.ChildNodes[2].InnerText.Trim(); isNew = divNode.ChildNodes[3].Name.Equals("img"); data.Add(new NoticeItemViewModel(noticeType, isNew, username, actionTime, new string[] { userId })); break; } } #region 还原通知数据的“NEW”状态后,清除 toast notice data var noticeToastTempData = ToastService.GetNoticeToastTempData(); if (noticeToastTempData != null) { foreach (var i in noticeToastTempData) { foreach (var j in data) { string key = string.Format("{0}#{1}", (int)j.NoticeType, j.ActionTime); if (key.Equals(i) && j.IsNew == false) { j.IsNew = true; break; } } } } ToastService.ClearNoticeToastTempData(); #endregion return(data); }