Guest User

Google I/O 2012 Redirect Checker w/ Gmail Support

a guest
Feb 28th, 2012
674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.IO;
  7. using System.Threading;
  8. using Microsoft.Office.Interop.Outlook;
  9. using System.Text.RegularExpressions;
  10. using System.Net.Mail;
  11.  
  12. namespace GoogleIOAlert
  13. {
  14.     class Program
  15.     {
  16.         private const string url = "http://www.google.com/io";
  17.         private const string urlRedirect = "http://www.google.com/events/io/2011/index-live.html";
  18.         private const string userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11";
  19.         private const int minBackoffTime = 10;               //10 seconds
  20.         private const string emailSubject = "Google I/O 2012 Site Updated";
  21.         private const string emailBody = "Quick, go to " + url + "\n Maybe they opened registration!";
  22.  
  23.         private const string emailRegex = @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
  24.         @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$";
  25.  
  26.         private static HttpWebRequest currentRequest;
  27.         private static HttpWebResponse currentResponse;
  28.         private static StringBuilder output;
  29.         private static int backoffTime = minBackoffTime;
  30.         private static string emailAddress;
  31.  
  32.         private static string sendFromEmailAddress;
  33.         private static string sendFromPassword;
  34.  
  35.         private enum AlertType
  36.         {
  37.             Outlook = 0,
  38.             Gmail = 1,
  39.             None = 2
  40.         }
  41.  
  42.         private static string[] alertTypeStrings = {"Outlook", "Gmail", "None"};
  43.  
  44.         private static AlertType selectedAlertType;
  45.         private static bool validAlertType;
  46.         private static string selectedAlertTypeString;
  47.  
  48.         static void Main(string[] args)
  49.         {
  50.             Console.Write("How would you like me to alert you? (" + String.Join(", ", alertTypeStrings) + "): ");
  51.             while (!validAlertType)
  52.             {
  53.                 selectedAlertTypeString = Console.ReadLine();
  54.                 for (int i = 0; i < alertTypeStrings.Length; i++)
  55.                 {
  56.                     if (selectedAlertTypeString.Equals(alertTypeStrings[i], StringComparison.OrdinalIgnoreCase))
  57.                     {
  58.                         selectedAlertType = (AlertType)i;
  59.                         validAlertType = true;
  60.                         break;
  61.                     }
  62.                 }
  63.  
  64.                 if (validAlertType)
  65.                 {
  66.                     break;
  67.                 }
  68.  
  69.                 Console.Write("Invalid alert type, try again: ");
  70.             }
  71.  
  72.             if (selectedAlertType == AlertType.Gmail)
  73.             {
  74.                 Console.Write("Enter your gmail address: ");
  75.                 sendFromEmailAddress = Console.ReadLine();
  76.  
  77.                 //Validate email address
  78.                 while (!Regex.IsMatch(sendFromEmailAddress, emailRegex, RegexOptions.IgnoreCase))
  79.                 {
  80.                     //Get user email address
  81.                     Console.Write("Invalid email address, try again: ");
  82.                     sendFromEmailAddress = Console.ReadLine();
  83.                 }
  84.  
  85.                 //Gather password
  86.                 Console.Write("Enter your gmail password: ");
  87.                 sendFromPassword = Console.ReadLine();
  88.             }
  89.  
  90.             if(selectedAlertType != AlertType.None)
  91.             {
  92.                 //Get user email address
  93.                 Console.Write("Enter a valid email address to which you would like me to alert: ");
  94.                 emailAddress = Console.ReadLine();
  95.  
  96.                 //Validate email address
  97.                 while (!Regex.IsMatch(emailAddress, emailRegex, RegexOptions.IgnoreCase))
  98.                 {
  99.                     //Get user email address
  100.                     Console.Write("Invalid email address, try again: ");
  101.                     emailAddress = Console.ReadLine();
  102.                 }
  103.  
  104.                 Console.Write("An alert will be sent from " + alertTypeStrings[(int) selectedAlertType]);
  105.  
  106.                 if (selectedAlertType == AlertType.Gmail)
  107.                 {
  108.                     Console.Write(" using your gmail account: " + sendFromEmailAddress);
  109.                 }
  110.  
  111.                 Console.WriteLine("\nAn email will be sent to " + emailAddress + " as soon as the redirect changes for " + url + "\n");
  112.             }
  113.  
  114.             int attemptNumber = 1;
  115.  
  116.             //Listen for changes
  117.             while (true)
  118.             {
  119.                 //Set request timeout and user agent
  120.                 currentRequest = (HttpWebRequest) HttpWebRequest.Create(url);
  121.                 currentRequest.Timeout = backoffTime * 1000;
  122.                 currentRequest.UserAgent = userAgent;
  123.  
  124.                 output = new StringBuilder();
  125.                 output.Append("Attempt #");
  126.                 output.Append(attemptNumber++);
  127.                 output.Append(":\n");
  128.                 output.Append("Fetching Google IO...\n");
  129.  
  130.                 Console.WriteLine(output.ToString());
  131.                 output.Clear();
  132.  
  133.                 try
  134.                 {
  135.                     currentResponse = (HttpWebResponse)currentRequest.GetResponse();
  136.                     output.Append("Response URI: ");
  137.                     output.Append(currentResponse.ResponseUri.ToString());
  138.                     output.Append("\n");
  139.  
  140.                     if (!urlRedirect.Equals(currentResponse.ResponseUri.ToString()))
  141.                     {
  142.                         //If the url redirect has changed
  143.                         output.Append("The Google IO 2012 Site is up!\n");
  144.                         alert();
  145.  
  146.                         Console.WriteLine(output.ToString());
  147.                         output.Clear();
  148.  
  149.                         break;
  150.                     }
  151.                     else
  152.                     {
  153.                         output.Append("Nothing has changed yet.\n");
  154.                     }
  155.  
  156.                     currentResponse.Close();
  157.  
  158.                     output.Append(url);
  159.                     output.Append(" request successful.");
  160.  
  161.                     if (backoffTime != Math.Max(minBackoffTime, (int)((float)backoffTime * (2.0f / 3.0f))))
  162.                     {
  163.                         //If the backoff time can be reduced
  164.                         output.Append(" Decreasing backoff time from ");
  165.                         output.Append(backoffTime);
  166.                         output.Append(" seconds to ");
  167.  
  168.                         backoffTime = Math.Max(minBackoffTime, (int)((float)backoffTime * (2.0f / 3.0f)));
  169.  
  170.                         output.Append(backoffTime);
  171.                         output.Append(" seconds.");
  172.                     }
  173.  
  174.                     output.Append("\n");
  175.                 }
  176.                 catch (WebException e)
  177.                 {
  178.                     if (e.Status == WebExceptionStatus.Timeout)
  179.                     {
  180.                         //Backoff a bit
  181.                         output.Append(url);
  182.                         output.Append(" timed out. Increasing backoff time from ");
  183.                         output.Append(backoffTime);
  184.                         output.Append(" seconds to ");
  185.  
  186.                         backoffTime = (int)Math.Ceiling(backoffTime * 1.5);
  187.  
  188.                         output.Append(backoffTime);
  189.                     }
  190.                     else
  191.                     {
  192.                         output.Append("Web exception: ");
  193.                         output.Append(e.Message);
  194.                     }
  195.  
  196.                     output.Append("\n");
  197.                 }
  198.                 finally
  199.                 {
  200.                     output.Append("Waiting ");
  201.                     output.Append(backoffTime);
  202.                     output.Append(" seconds until next request.\n\n");
  203.  
  204.                     Console.Write(output.ToString());
  205.                     output.Clear();
  206.                     Thread.Sleep(backoffTime * 1000);
  207.                 }
  208.                
  209.             }
  210.             Console.WriteLine("Program ended.");
  211.             Console.WriteLine("Press any key to exit.");
  212.             Console.ReadKey();
  213.         }
  214.  
  215.         private static void alert()
  216.         {
  217.             switch (selectedAlertType)
  218.             {
  219.                 case AlertType.Gmail:
  220.                     //@see http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail
  221.                     var fromAddress = new MailAddress(sendFromEmailAddress, "Google I/O Alert");
  222.                     var toAddress = new MailAddress(emailAddress, "You");
  223.  
  224.                     var smtp = new SmtpClient
  225.                     {
  226.                         Host = "smtp.gmail.com",
  227.                         Port = 587,
  228.                         EnableSsl = true,
  229.                         DeliveryMethod = SmtpDeliveryMethod.Network,
  230.                         UseDefaultCredentials = false,
  231.                         Credentials = new NetworkCredential(fromAddress.Address, sendFromPassword)
  232.                     };
  233.  
  234.                     using (var message = new MailMessage(fromAddress, toAddress)
  235.                     {
  236.                         Subject = emailSubject,
  237.                         Body = emailBody
  238.                     })
  239.                     {
  240.                         smtp.Send(message);
  241.                     }
  242.                     break;
  243.                 case AlertType.Outlook:
  244.                     Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
  245.                     MailItem newEmail = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
  246.                     newEmail.Recipients.Add(emailAddress);
  247.                     newEmail.Subject = emailSubject;
  248.                     newEmail.Body = emailBody;
  249.                     newEmail.Send();
  250.                     break;
  251.                 case AlertType.None:
  252.                 default:
  253.                     break;
  254.             }
  255.         }
  256.     }
  257. }
Add Comment
Please, Sign In to add comment