private void SetImageData(HtmlDocument document, ScrappedCardModel scrappedCard) { var image = document.DocumentNode.SelectSingleNode("//*[@id='mw-content-text']/p/img") ?? document.DocumentNode.SelectSingleNode("//*[@id='mw-content-text']/img"); scrappedCard.ImageUrl = image.GetAttributeValue("data-src", null) ?? image.GetAttributeValue("src", string.Empty); scrappedCard.ImageName = image.GetAttributeValue("data-image-key", string.Empty); }
private static void SetLoyalty(ScrappedCardModel scrappedCard, HtmlNodeCollection table) { var row = table.FirstOrDefault(x => x.ChildNodes[1].InnerText.Contains("Loyalty")); if (row != null) { scrappedCard.Loyalty = row.ChildNodes[2].InnerText.Trim(); } }
private static void SetTypes(ScrappedCardModel scrappedCard, HtmlNodeCollection table) { var row = table.FirstOrDefault(x => x.ChildNodes[1].InnerText.Contains("Types")); if (row != null) { scrappedCard.Types = row.ChildNodes[2].InnerText.Trim().Replace(" —", "").Split(' ').ToList(); } }
private static void SetConvertedManaCost(ScrappedCardModel scrappedCard, HtmlNodeCollection table) { var row = table.FirstOrDefault(x => x.ChildNodes[1].InnerText.Contains("Converted Mana Cost")); if (row != null) { scrappedCard.ConvertedManaCost = int.Parse(row.ChildNodes[2].InnerText.Trim()); } }
private static void SetPowerAndThoughness(ScrappedCardModel scrappedCard, HtmlNodeCollection table) { var row = table.FirstOrDefault(x => x.ChildNodes[1].InnerText.Contains("P/T")); if (row != null) { var pt = row.ChildNodes[2].InnerText.Trim().Trim('(', ')').Split('/'); scrappedCard.Power = pt[0]; scrappedCard.Thoughness = pt[1]; } }
private void ExtractDataFromTable(ScrappedCardModel scrappedCard, HtmlNodeCollection table) { SetName(scrappedCard, table); SetManaCost(scrappedCard, table); SetConvertedManaCost(scrappedCard, table); SetTypes(scrappedCard, table); SetCardText(scrappedCard, table); SetFlavorText(scrappedCard, table); SetPowerAndThoughness(scrappedCard, table); SetExpansion(scrappedCard, table); SetRarity(scrappedCard, table); SetLoyalty(scrappedCard, table); }
private static void AddTags(HtmlDocument document, ScrappedCardModel scrappedCard) { var tags = document.DocumentNode.SelectSingleNode("//*[@id='articleCategories']/div[1]/ul"); var filteredTags = tags.ChildNodes.Where(x => !string.IsNullOrWhiteSpace(x.InnerText)); foreach (var tag in filteredTags) { var trimmedText = tag.InnerText.Trim(); if (trimmedText == "Cards" || trimmedText == "Add category") { continue; } scrappedCard.Tags.Add(trimmedText); } }
private static void SetManaCost(ScrappedCardModel scrappedCard, HtmlNodeCollection table) { var row = table.FirstOrDefault(x => x.ChildNodes[1].InnerText.Contains("Mana Cost")); if (row != null) { foreach (var childNode in row.ChildNodes[2].ChildNodes) { if (childNode.Name == "#text") { continue; } var alt = childNode.GetAttributeValue("alt", ""); if (alt.Contains("CMC")) { if (alt.Length > 4) { scrappedCard.ColorlessMana = alt[3].ToString() + alt[4]; } else { scrappedCard.ColorlessMana = alt[3].ToString(); } } else if (alt.Contains("Color R")) { scrappedCard.RedMana++; } else if (alt.Contains("Color U")) { scrappedCard.BlueMana++; } else if (alt.Contains("Color G")) { scrappedCard.GreenMana++; } else if (alt.Contains("Color W")) { scrappedCard.WhiteMana++; } else if (alt.Contains("Color B")) { scrappedCard.BlackMana++; } } } }
private ScrappedCardModel ScrapCard(string page) { HtmlDocument document = new HtmlDocument(); document.LoadHtml(page); var scrappedCard = new ScrappedCardModel(); var table = document.DocumentNode.SelectNodes("//*[@id='mw-content-text']/table/tr"); ExtractDataFromTable(scrappedCard, table); SetImageData(document, scrappedCard); AddTags(document, scrappedCard); return(scrappedCard); }