Ejemplo n.º 1
0
        private static void ProcessHeaders()
        {
            var count = 0;

            foreach (var recipeHeader in Headers)
            {
                // Check if we have this id in the database
                var recipe = ScraperRepository.GetRecipeHeader(recipeHeader);
                if (recipe != null)
                {
                    continue;
                }

                // This is a new Recipe... Add it to the database.
                ScraperRepository.AddRecipeHeader(recipeHeader);
                count++;
            }

            if (!Context.ChangeTracker.HasChanges())
            {
                return;
            }

            ScraperRepository.SaveChanges();
            Console.WriteLine($"\tAdded {count} recipes");
        }
Ejemplo n.º 2
0
        private static async void GetRecipeDetailsAsync()
        {
            //var recipeHeader = Context.RecipeHeaders.Where(x => x.Id >= 541).ToList();
            var recipeHeader = Context.RecipeHeaders.ToList();

            foreach (var header in recipeHeader)
            {
                Console.WriteLine($"Getting recipe from {header.RecipeUri}");
                var httpClient = new HttpClient();
                var html       = await httpClient.GetStringAsync(header.RecipeUri);

                var htmlDocument = new HtmlDocument();
                htmlDocument.LoadHtml(html);

                var ingredientsList = GetIngredientsList(htmlDocument);

                var ingredients = new List <HtmlNode>();

                if (ingredientsList.Count == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"No Ingredients Found for {header.RecipeUri} attempting 2nd approach");
                    Console.ForegroundColor = ConsoleColor.White;

                    // Use 2nd Approach
                    ingredientsList = GetIngredientsList2(htmlDocument);

                    GetDetails2(ingredientsList, ingredients);
                }
                else
                {
                    GetDetails(ingredientsList, ingredients);
                }


                if (ingredients.Count == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("No Ingredients Found");
                    Console.ForegroundColor = ConsoleColor.White;
                }

                ScraperRepository.AddIngredientsToDb(ingredients, header);

                ScraperRepository.SaveChanges();

                Console.WriteLine($"\tAdded {ingredients.Count} ingredients");
            }
        }