static internal async Task<List<GuokrPost>> _getPosts(RestClient client, RestRequest req, Dictionary<string, string> xpath, GuokrGroup group = null) { var resp = await RestSharpAsync.RestSharpExecuteAsyncTask(client, req); List<GuokrPost> ress = new List<GuokrPost>(); #if WP8 await Task.Run(() => #else await TaskEx.Run(() => #endif { var html = resp.Content; var doc = new HtmlDocument(); doc.LoadHtml(html); var ul = doc.DocumentNode.SelectSingleNode(xpath["ul"]); if (!(xpath.ContainsKey("title") && xpath.ContainsKey("reply_count") && xpath.ContainsKey("posted_by") && xpath.ContainsKey("replied_dt") && xpath.ContainsKey("group") )) throw new NotImplementedException("xpath expressions not sufficient"); if (ul == null) return; var titles = ul.SelectNodes(ul.XPath + xpath["title"]); var reply_counts = ul.SelectNodes(ul.XPath + xpath["reply_count"]); var posted_bys = ul.SelectNodes(ul.XPath + xpath["posted_by"]); var replied_dts = ul.SelectNodes(ul.XPath + xpath["replied_dt"]); var groups = ul.SelectNodes(ul.XPath + xpath["group"]); if (titles == null || titles.Count <= 1) return; if (titles.Count != reply_counts.Count || titles.Count != posted_bys.Count || titles.Count != replied_dts.Count || titles.Count != groups.Count) throw new ArgumentOutOfRangeException(); for (int i = 0; i < titles.Count; i++) { var p = new GuokrPost(); ress.Add(p); try { if (xpath.ContainsKey("title")) { var title = titles[i]; p.title = HtmlEntity.DeEntitize(title.InnerText); p.wwwurl = GetAttribute(title, "href"); } if (xpath.ContainsKey("reply_count")) p.reply_count = Convert.ToInt32(reply_counts[i].InnerText); if (group != null) p.group = group; else if (xpath.ContainsKey("group")) { p.group = new GuokrGroup(); var grouplink = groups[i]; p.group.name = grouplink.InnerText; p.group.path = grouplink.Attributes["href"].Value; } else p.group = null; if (xpath.ContainsKey("posted_by")) { p.posted_by = new GuokrUser(); var n = posted_bys[i]; p.posted_by.nickname = n.InnerText; p.posted_by.uri = GetAttribute(n, "href"); } if (xpath.ContainsKey("replied_dt")) { var dt = replied_dts[i].InnerText; var match = Regex.Match(dt, @"\d{4}-\d{1,2}-\d{1,2} \d{2}:\d{2}:\d{2}"); p.replied_dt = match.Success && match.Groups.Count > 0 ? match.Groups[1].Value : ""; } } catch (Exception e) { DebugLogging.Append("exception", e.Message, ""); } } }); return ress; }
public static async Task<IEnumerable<GuokrPost>> GetGroupPosts(GuokrGroup g, int page = 0) { var client = new RestClient("http://www.guokr.com"); var req = new RestRequest(); req.Method = Method.GET; req.Resource = g.path; if (page != 0) req.Parameters.Add(new Parameter() { Name = "page", Value = page, Type = ParameterType.GetOrPost }); Dictionary<string, string> kvp = new Dictionary<string, string>(); kvp.Add("ulclass", "titles"); kvp.Add("liname", "h2"); return await _getPosts(client, req, kvp); }