예제 #1
0
        public static void ParseUser(HackerItem item, List<HtmlNode> links)
        {
            var userLink = links.Where(x => x.Attributes["href"].Value.Contains("user")).ToList();

            if (userLink.Count >= 1){
                item.User = userLink[0].InnerText;
            }
        }
예제 #2
0
 public static void ParsePoints(HackerItem item, List<HtmlNode> spans)
 {
     var pointSpan = spans.Where(x => x.Attributes["id"] != null && x.Attributes["id"].Value.Contains("score")).ToList();
     if (pointSpan.Count == 1)
     {
         item.Points = Regex.Replace(pointSpan[0].InnerText, " p(.*)", "");
     }
 }
예제 #3
0
 public static void ParseHost(HackerItem item, List<HtmlNode> spans)
 {
     var hostSpan = spans.Where(x => x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("comhead")).ToList();
     if (hostSpan.Count == 1){
         var host = hostSpan[0].InnerText.Trim();
         item.Host = host.Replace("(", "").Replace(")", "");
     }
 }
예제 #4
0
        public static void ParseIDAndCommentCount(HackerItem item, List<HtmlNode> links)
        {
            var itemLinks = links.Where(x => x.Attributes["href"].Value.Contains("item")).ToList();

            if (itemLinks.Count >= 1)
            {
                var itemLink = itemLinks[0];
                item.ID = itemLink.Attributes["href"].Value.Replace("item?id=", "");
                item.CommentCount = itemLink.InnerText == "discuss" ? "0" : itemLink.InnerText.Replace(" c(.*)", "");
            }
        }
예제 #5
0
        public static void ParseUrlTitle(HackerItem item, List<HtmlNode> links)
        {
            HtmlNode link;

            if (links.Count == 2)
            {
                link = links[1];
            }
            else
            {
                link = links[0];//if there is no upvote link
            }

            item.URL = link.Attributes["href"].Value;
            item.Title = link.InnerText;
        }
예제 #6
0
        protected List<HackerItem> ParseList(List<HtmlNode> rows)
        {
            List<HackerItem> items = new List<HackerItem>();
            HackerItem newsItem = null;

            for (int i = 0; i < rows.Count; i++)
            {
                try
                {
                    var row = rows[i];
                    var tableData = row.Descendants("td");
                    Debug.WriteLine(i);

                    if (tableData != null && tableData.Count() > 0)
                    {
                        if (tableData.Count(x => x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("title")) > 0)
                        {
                            if (i == rows.Count - 1)
                            {
                                nextUrl = row.Descendants("a").ToList()[0].Attributes["href"].Value;
                                break;
                            }

                            newsItem = new HackerItem();
                            ParsingHelpers.ParseUrlTitle(newsItem, row.Descendants("a").ToList());
                            ParsingHelpers.ParseHost(newsItem, row.Descendants("span").ToList());
                        }
                        else if (tableData.Count(x => x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("subtext")) > 0)
                        {
                            //points & user
                            var links = row.Descendants("a").ToList();

                            ParsingHelpers.ParsePoints(newsItem, row.Descendants("span").ToList());
                            ParsingHelpers.ParseUser(newsItem, links);
                            ParsingHelpers.ParseIDAndCommentCount(newsItem, links);

                            items.Add(newsItem);
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new HackerException("Could not parse the news feed. See inner exception for details", e);
                }
            }

            return items;
        }