private List<MatchItem> FilterMatchItemsSuperliga(string httpResult, int noOfItemsToGet, string teamToSearchFor, bool useProperNames) { HtmlDocument htmlSnippet = new HtmlDocument(); Encoding enc = new CustomEncoder(); htmlSnippet.LoadHtml(httpResult); List<MatchItem> listOfNewsToReturn = new List<MatchItem>(); List<HtmlNode> newsHolder = htmlSnippet.DocumentNode.Descendants("div"). FirstOrDefault(div => div.GetAttributeValue("class", "").Contains("col2")) .Descendants() .Where(html => html.OriginalName == "div" && html.GetAttributeValue("class", "").Contains("note") || html.OriginalName == "table" && html.GetAttributeValue("class", "").Contains("matchprogramtable")) .ToList(); int i = 0; while (i <= newsHolder.Count() - 2) { if (i % 2 == 0) { try { //get node HtmlNode note = newsHolder[i]; HtmlNode matchprogramtable = newsHolder[i + 1]; //Harvest needed information: date, title, url, id string dateRaw = note.FirstChild.InnerText.Trim(); DateTime date = BoldHelpers.FormatDateTime(dateRaw.ToLower().Replace("den ", "").Replace(" i", string.Empty).Trim(), true); string isSuperliga = note.Descendants("a").FirstOrDefault().InnerText.Trim(); string titleRaw = matchprogramtable.Descendants("td").Where(td => td.GetAttributeValue("class", "").Contains("col1")).FirstOrDefault().Descendants("a").FirstOrDefault().InnerText.Replace("Vs.", " - ").RemoveNewLineTag().RemoveTabTag(); string score = string.Empty; if (matchprogramtable.Descendants("td").Where(td => td.GetAttributeValue("class", "").Contains("col2")).FirstOrDefault().Descendants("a").FirstOrDefault() != null) { score = matchprogramtable.Descendants("td").Where(td => td.GetAttributeValue("class", "").Contains("col2")).FirstOrDefault().Descendants("a").FirstOrDefault().InnerText.RemoveNewLineTag().RemoveTabTag(); } string lol = score; string homeTeam = BoldHelpers.FormatHomeTeam(titleRaw); string awayTeam = BoldHelpers.FormatAwayTeam(titleRaw); homeTeam = BoldHelpers.FormatClubName(homeTeam, useProperNames); awayTeam = BoldHelpers.FormatClubName(awayTeam, useProperNames); bool isHomeMatch = BoldHelpers.isHomeTeam(teamToSearchFor, homeTeam); //string urlRaw = matchprogramtable.Descendants("a").FirstOrDefault().GetAttributeValue("href", "123456"); //string url = HoldNytHelpers.FormatUrl(urlRaw); string idRaw = matchprogramtable.Descendants("a").FirstOrDefault().GetAttributeValue("href", "123456"); ////string sourceRaw = node.Descendants("div").Where(div => div.GetAttributeValue("class", "").Contains("news_media")).FirstOrDefault().Descendants("span").FirstOrDefault().GetAttributeValue("class", string.Empty); ////string source = HoldNytHelpers.SourceFormat(sourceRaw); ////MatchItem item = new MatchItem(title, url, date, id, source); MatchItem item = new MatchItem(homeTeam, awayTeam, date, isHomeMatch, idRaw, score); listOfNewsToReturn.Add(item); } catch(Exception e) { Debug.WriteLine("FilterMatchItemsSuperliga: " + e); } } i = i + 2; } return listOfNewsToReturn; }
private List<MatchItem> FilterMatchItems(string httpResult, int noOfItemsToGet, string teamToSearchFor="OB") { HtmlDocument htmlSnippet = new HtmlDocument(); htmlSnippet.LoadHtml(httpResult); List<MatchItem> listOfNewsToReturn = new List<MatchItem>(); HtmlNode newsHolder = htmlSnippet.DocumentNode.Descendants("div").FirstOrDefault(div => div.GetAttributeValue("class", "").Contains("col1")).Descendants("table").FirstOrDefault(); foreach (HtmlNode node in newsHolder.Descendants("tr").Where(div => div.InnerHtml.Contains(teamToSearchFor))) { if (listOfNewsToReturn.Count < noOfItemsToGet) { try { //Harvest needed information: date, title, url, id string dateRaw = node.Descendants("td").Where(td => !string.IsNullOrEmpty(td.InnerText.ToString())).FirstOrDefault().InnerText; DateTime date = BoldHelpers.FormatDateTime(dateRaw, false); string titleRaw = node.Descendants("a").Where(a => a.GetAttributeValue("class", "").Contains("tekst")).Where(a => a.InnerText.Contains(teamToSearchFor)).FirstOrDefault().InnerText; string homeTeam = BoldHelpers.FormatHomeTeam(titleRaw); string awayTeam = BoldHelpers.FormatAwayTeam(titleRaw); bool isHomeMatch = BoldHelpers.isHomeTeam(teamToSearchFor, homeTeam); string matchRaw = node.Descendants("a").Where(a => a.GetAttributeValue("class", "").Contains("tekstsmall")).FirstOrDefault().InnerText; //string url = HoldNytHelpers.FormatUrl(urlRaw); string idRaw = node.Descendants("a").Where(a => a.GetAttributeValue("class", "").Contains("tekst")).FirstOrDefault().GetAttributeValue("href", ""); //string sourceRaw = node.Descendants("div").Where(div => div.GetAttributeValue("class", "").Contains("news_media")).FirstOrDefault().Descendants("span").FirstOrDefault().GetAttributeValue("class", string.Empty); //string source = HoldNytHelpers.SourceFormat(sourceRaw); //MatchItem item = new MatchItem(title, url, date, id, source); MatchItem item = new MatchItem(homeTeam, awayTeam, date, isHomeMatch, idRaw, string.Empty); listOfNewsToReturn.Add(item); } catch { //Fail silently.... :'( Debug.WriteLine("FilterMatchItems"); } } } return listOfNewsToReturn; }