public async Task <IActionResult> Result(string search) { var amazon = new AmazonScraper(); var ebay = new EbayScraper(); var jLewis = new JohnLewisScraper(); Hashtable amazonResults; Hashtable ebayResults; Hashtable jLewisResults; ViewBag.ProductDescription = search; // scrape retailers try { jLewisResults = await jLewis.ScrapePricesForProduct(search); ebayResults = await ebay.ScrapePricesForProduct(search); amazonResults = await amazon.ScrapePricesForProduct(search); } catch (Exception e) { Console.WriteLine(e); throw; } // create new result var result = new Result() { Date = DateTime.Now, AmazonPrice = Convert.ToDouble(amazonResults["Formatted Price"]), EbayPrice = Convert.ToDouble(ebayResults["Formatted Price"]), JohnLewisPrice = Convert.ToDouble(jLewisResults["Formatted Price"]), AmazonLink = amazonResults["Product Link"].ToString(), AmazonHeading = amazonResults["Product Heading"].ToString(), EbayLink = ebayResults["Product Link"].ToString(), EbayHeading = ebayResults["Product Heading"].ToString(), JohnLewisHeading = jLewisResults["Product Heading"].ToString(), JohnLewisLink = jLewisResults["Product Link"].ToString(), }; //create Saved Search var savedSearch = new SavedSearch() { CreatedDate = DateTime.Now, Description = search, }; // create save search view model var viewModel = new SaveSearchViewModel() { Result = result, SavedSearch = savedSearch }; return(View(viewModel)); }
public static async Task UpdateSearches() { Console.WriteLine("task started successfully"); using (var _context = new PriceTrackerContext()) { var amazon = new AmazonScraper(); var ebay = new EbayScraper(); var jLewis = new JohnLewisScraper(); //get the saved searches var savedSearches = _context.SavedSearch.OrderByDescending(x => x.CreatedDate).Include(x => x.Results).ToList(); foreach (var search in savedSearches) { Hashtable amazonResults; Hashtable ebayResults; Hashtable jLewisResults; try { jLewisResults = await jLewis.ScrapePricesForProduct(search.Description); ebayResults = await ebay.ScrapePricesForProduct(search.Description); amazonResults = await amazon.ScrapePricesForProduct(search.Description); } catch (Exception e) { Console.WriteLine(e); throw; } var result = new Result() { Date = DateTime.Now, AmazonPrice = Convert.ToDouble(amazonResults["Formatted Price"]), EbayPrice = Convert.ToDouble(ebayResults["Formatted Price"]), JohnLewisPrice = Convert.ToDouble(jLewisResults["Formatted Price"]), AmazonLink = amazonResults["Product Link"].ToString(), AmazonHeading = amazonResults["Product Heading"].ToString(), EbayLink = ebayResults["Product Link"].ToString(), EbayHeading = ebayResults["Product Heading"].ToString(), JohnLewisHeading = jLewisResults["Product Heading"].ToString(), JohnLewisLink = jLewisResults["Product Link"].ToString(), }; search.Results.Add(result); await _context.SaveChangesAsync(); } } }
public async Task UpdateResults() { Console.WriteLine($"Update Task started at {DateTime.Now}"); using (var _context = new PriceTrackerContext()) { var amazon = new AmazonScraper(); var ebay = new EbayScraper(); var jLewis = new JohnLewisScraper(); //get the saved searches var savedSearches = _context.SavedSearch.OrderByDescending(x => x.CreatedDate).Include(x => x.Results).ToList(); if (savedSearches.Count != 0) { foreach (var search in savedSearches) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"scraping prices for search: {search.Description} , id {search.SavedSearchId}, created on: {search.CreatedDate}"); Console.ForegroundColor = ConsoleColor.Gray; Hashtable amazonResults; Hashtable ebayResults; Hashtable jLewisResults; try { jLewisResults = await jLewis.ScrapePricesForProduct(search.Description); ebayResults = await ebay.ScrapePricesForProduct(search.Description); amazonResults = await amazon.ScrapePricesForProduct(search.Description); } catch (Exception e) { Console.WriteLine(e); throw; } var result = new Result() { Date = DateTime.Now, AmazonPrice = Convert.ToDouble(amazonResults["Formatted Price"]), EbayPrice = Convert.ToDouble(ebayResults["Formatted Price"]), JohnLewisPrice = Convert.ToDouble(jLewisResults["Formatted Price"]), AmazonLink = amazonResults["Product Link"].ToString(), AmazonHeading = amazonResults["Product Heading"].ToString(), EbayLink = ebayResults["Product Link"].ToString(), EbayHeading = ebayResults["Product Heading"].ToString(), JohnLewisHeading = jLewisResults["Product Heading"].ToString(), JohnLewisLink = jLewisResults["Product Link"].ToString(), }; //get the latest result for this saved search var currentSearchResult = search.Results.OrderByDescending(x => x.Date).FirstOrDefault(); search.Results.Add(result); await _context.SaveChangesAsync(); // compare the prices here var emailBody = ComparePrices(result, currentSearchResult); if (!string.IsNullOrEmpty(emailBody)) { Console.WriteLine(SendNotificationEmail(emailBody)); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("completed "); Console.ForegroundColor = ConsoleColor.Gray; } } else { Console.WriteLine("No Saved Searches to update"); } } Console.WriteLine($"Update Task completed successfully at {DateTime.Now}"); }