private static void getRecipes(string url, Collection parentCollection) { var webGet = new HtmlWeb(); var document = webGet.Load(url); var itemWrapper = from node in document.DocumentNode.Descendants() where node.Name == "div" && node.Attributes["id"] != null && node.Attributes["id"].Value == "divGridItemWrapper" select node; foreach (HtmlNode wrapper in itemWrapper) { var items = from node in wrapper.Descendants() where node.Name == "a" && node.Attributes["class"] != null && node.Attributes["class"].Value == "title" select node; foreach (HtmlNode item in items) { Recipe recipe; String recipeUrl = item.Attributes["href"].Value; if (recipesByUrl.ContainsKey(recipeUrl)) recipe = recipesByUrl[recipeUrl]; else { recipe = new Recipe(item.InnerText, recipeUrl); getRecipeContents(recipe); recipesByUrl.Add(recipeUrl, recipe); } recipe.Collections.Add(parentCollection); } } }
private static void getRecipeContents(Recipe recipe) { var webGet = new HtmlWeb(); var document = webGet.Load(recipe.Url); var ratingDiv = from node in document.DocumentNode.Descendants() where node.Name == "div" && node.Attributes["itemprop"] != null && node.Attributes["itemprop"].Value == "aggregateRating" select node; foreach (HtmlNode wrapper in ratingDiv) { var ratingMeta = from node in wrapper.Descendants() where node.Name == "meta" && node.Attributes["itemprop"] != null && node.Attributes["itemprop"].Value == "ratingValue" select node; foreach (HtmlNode rating in ratingMeta) { recipe.Rating = Double.Parse(rating.Attributes["content"].Value); } } var ingredients = from node in document.DocumentNode.Descendants() where node.Name == "li" && node.Attributes["id"] != null && node.Attributes["id"].Value == "liIngredient" select node; foreach (HtmlNode ingredientNode in ingredients) { Ingredient ingredient = new Ingredient(); ingredient.Id = int.Parse(ingredientNode.Attributes["data-ingredientid"].Value); ingredient.Grams = Double.Parse(ingredientNode.Attributes["data-grams"].Value); var spans = from node in ingredientNode.Descendants() where node.Name == "span" && node.Attributes["class"] != null && node.Attributes["class"].Value.Contains("ingredient-") select node; foreach (HtmlNode span in spans) { if (span.Attributes["class"].Value == "ingredient-name") ingredient.Name = span.InnerText; if (span.Attributes["class"].Value == "ingredient-amount") ingredient.Amount = span.InnerText; } recipe.Ingredients.Add(ingredient); } var instructions = from node in document.DocumentNode.Descendants() where node.Name == "div" && node.Attributes["itemprop"] != null && node.Attributes["itemprop"].Value == "recipeInstructions" select node; foreach (HtmlNode instructionNode in instructions) { var instructionSpan = from node in instructionNode.Descendants() where node.Name == "span" && node.Attributes["class"] != null && node.Attributes["class"].Value == "plaincharacterwrap break" select node; foreach (HtmlNode span in instructionSpan) { recipe.Instructions.Add(span.InnerText); } } var timeSpans = from node in document.DocumentNode.Descendants() where node.Name == "span" && node.Attributes["id"] != null && (node.Attributes["id"].Value.Contains("HoursSpan") || node.Attributes["id"].Value.Contains("MinsSpan")) select node; int prepTime = 0; int cookTime = 0; int readyTime = 0; foreach (HtmlNode span in timeSpans) { if (span.Attributes["id"].Value == "prepHoursSpan") { string txt = span.InnerText; prepTime += int.Parse(txt.Substring(0, txt.IndexOf("hr") - 1)) * 60; } if (span.Attributes["id"].Value == "prepMinsSpan") { string txt = span.InnerText; prepTime += int.Parse(txt.Substring(0, txt.IndexOf("min") - 1)); } if (span.Attributes["id"].Value == "cookHoursSpan") { string txt = span.InnerText; cookTime += int.Parse(txt.Substring(0, txt.IndexOf("hr") - 1)) * 60; } if (span.Attributes["id"].Value == "cookMinsSpan") { string txt = span.InnerText; cookTime += int.Parse(txt.Substring(0, txt.IndexOf("min") - 1)); } if (span.Attributes["id"].Value == "totalHoursSpan") { string txt = span.InnerText; readyTime += int.Parse(txt.Substring(0, txt.IndexOf("hr") - 1)) * 60; } if (span.Attributes["id"].Value == "totalMinsSpan") { string txt = span.InnerText; readyTime += int.Parse(txt.Substring(0, txt.IndexOf("min") - 1)); } } recipe.PrepMinutes = prepTime; recipe.CookMinutes = cookTime; recipe.ReadyMinutes = readyTime; var nutrients = from node in document.DocumentNode.Descendants() where node.Name == "ul" && node.Attributes["id"] != null && node.Attributes["id"].Value == "ulNutrient" select node; foreach (HtmlNode nutrientItem in nutrients) { var nutrientNodes = from node in nutrientItem.Descendants() where node.Name == "li" select node; Nutrient nutrient = new Nutrient(); foreach (HtmlNode nutrientNode in nutrientNodes) { if (nutrientNode.Attributes["class"] != null && nutrientNode.Attributes["class"].Value == "categories") nutrient.Name = nutrientNode.InnerText; if (nutrientNode.Attributes["class"] != null && nutrientNode.Attributes["class"].Value == "units") nutrient.Value = nutrientNode.InnerText; if (nutrientNode.Attributes["class"] != null && nutrientNode.Attributes["class"].Value == "percentages" && nutrientNode.InnerText.Contains('%')) nutrient.Percent = int.Parse(nutrientNode.InnerText.Substring(0, nutrientNode.InnerText.IndexOf('%'))); } recipe.Nutrients.Add(nutrient); } recipe.Complete = true; }