Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Mail;
  6. using System.Net;
  7. using System.Configuration;
  8. using System.Threading.Tasks;
  9. using HtmlAgilityPack;
  10.  
  11. namespace WebscrapeMailer
  12. {
  13.     class Program
  14.     {
  15.  
  16.  
  17.  
  18.         public static void Main()
  19.         {
  20.             // Store configurable values from App.config.
  21.  
  22.             string mailTo = ConfigurationManager.AppSettings["config.MailTo"];
  23.             string mailFrom = ConfigurationManager.AppSettings["config.MailFrom"];
  24.             string mailSubject = ConfigurationManager.AppSettings["config.MailSubject"];
  25.             string mailServer = ConfigurationManager.AppSettings["config.MailServer"];
  26.             //Create a dictionary which is like a dictionary in the real world.. except that instead of
  27.             //you looking up a word and then its associated definitions, you can store any lookup value, and associate it with anything you want
  28.             //"word" = webURL, "definition" = URLnode.
  29.  
  30.             Dictionary<string, string> webURLnodePairs = new Dictionary<string, string>();
  31.  
  32.             webURLnodePairs.Add(ConfigurationManager.AppSettings["config.webURL1"], ConfigurationManager.AppSettings["config.URL1Node"]);
  33.             webURLnodePairs.Add(ConfigurationManager.AppSettings["config.webURL2"], ConfigurationManager.AppSettings["config.URL2Node"]);
  34.             webURLnodePairs.Add(ConfigurationManager.AppSettings["config.webURL3"], ConfigurationManager.AppSettings["config.URL3Node"]);
  35.  
  36.             //Now imagine being able to spawn 3 separate requests for each screen scrape that execute at roughly the same time
  37.             //It should be 3x as fast!
  38.  
  39.             var task = GetAllScrapes(webURLnodePairs);  // Im calling an asynchronous method and telling it to wait
  40.             task.Wait();
  41.             List<string> scrapeResults = task.Result; //grab the result from the async call
  42.            
  43.             SendMail(scrapeResults, mailTo, mailFrom, mailSubject, mailServer);
  44.  
  45.         }
  46.  
  47.         static async Task<List<string>> GetAllScrapes(Dictionary<string, string> webURLnodePairs)
  48.         {
  49.             // This is a neat feature.. create a task that waits for all the taks you spawn off using
  50.             //the select method and lambda expression to pick out the url and urlnodes from the dictionary.
  51.             var results = await Task.WhenAll(webURLnodePairs.Select((url, index) => Scrape(url.Key, url.Value)));
  52.  
  53.             return results.ToList();
  54.         }
  55.  
  56.  
  57.  
  58.         static async Task<string> Scrape(string url, string urlnode)
  59.         {
  60.             //each scrape is on its own thread
  61.             WebClient client = new WebClient();
  62.  
  63.             //this is relatively unchanged from your code
  64.             string html = client.DownloadString(url);
  65.             HtmlAgilityPack.HtmlDocument doc3 = new HtmlAgilityPack.HtmlDocument();
  66.             doc3.LoadHtml(html);
  67.             HtmlNode node = doc3.DocumentNode.SelectSingleNode(urlnode);
  68.  
  69.             return node.InnerText;
  70.         }
  71.  
  72.  
  73.         static async void SendMail(List<string> scrapeValues, string mailTo, string mailFrom, string mailSubject, string mailServer)
  74.         {
  75.             //sending mail is also on its own thread..
  76.  
  77.             MailAddress to = new MailAddress(mailTo);
  78.             MailAddress from = new MailAddress(mailFrom);
  79.             MailMessage mail = new MailMessage(from, to);
  80.             mail.Subject = mailSubject;
  81.  
  82.             mail.Body = "Scrapes: " + Environment.NewLine;
  83.             foreach (string scrape in scrapeValues)
  84.             {
  85.                 mail.Body += "Start of new scrape: ";
  86.                 mail.Body += scrape;
  87.                 mail.Body += Environment.NewLine;
  88.             }
  89.  
  90.             // Create the smtp object.
  91.             SmtpClient smtp = new SmtpClient();
  92.  
  93.             // Get relay details, then send.
  94.             smtp.Host = mailServer;
  95.             smtp.Port = 25;
  96.  
  97.             smtp.Send(mail);
  98.         }
  99.     }
  100.  
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement