Guest User

Google I/O 2012 Redirect Checker

a guest
Feb 28th, 2012
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.19 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.  
  11. namespace GoogleIOAlert
  12. {
  13.     class Program
  14.     {
  15.         private const string url = "http://www.google.com/io";
  16.         private const string urlRedirect = "http://www.google.com/events/io/2011/index-live.html";
  17.         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";
  18.         private const int minBackoffTime = 10;               //5 seconds
  19.         private const string emailSubject = "Google I/O 2012 Site Updated";
  20.         private const string emailBody = "Quick, go to " + url + "\n Maybe they opened registration!";
  21.  
  22.         private const string emailRegex = @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
  23.         @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$";
  24.  
  25.         private static HttpWebRequest currentRequest;
  26.         private static HttpWebResponse currentResponse;
  27.         private static StringBuilder output;
  28.         private static int backoffTime = minBackoffTime;
  29.         private static string emailAddress;
  30.  
  31.         static void Main(string[] args)
  32.         {
  33.             //Get user email address
  34.             Console.Write("Enter a valid email address to which you would like me to alert: ");
  35.             emailAddress = Console.ReadLine();
  36.  
  37.             //Validate email address
  38.             while (!Regex.IsMatch(emailAddress, emailRegex, RegexOptions.IgnoreCase))
  39.             {
  40.                 //Get user email address
  41.                 Console.Write("Invalid email address, try again: ");
  42.                 emailAddress = Console.ReadLine();
  43.             }
  44.  
  45.             Console.WriteLine("An email will be sent to " + emailAddress + " as soon as the redirect changes for " + url + "\n");
  46.  
  47.             int attemptNumber = 1;
  48.  
  49.             //Listen for changes
  50.             while (true)
  51.             {
  52.                 //Set request timeout and user agent
  53.                 currentRequest = (HttpWebRequest) HttpWebRequest.Create(url);
  54.                 currentRequest.Timeout = backoffTime * 1000;
  55.                 currentRequest.UserAgent = userAgent;
  56.  
  57.                 output = new StringBuilder();
  58.                 output.Append("Attempt #");
  59.                 output.Append(attemptNumber++);
  60.                 output.Append(":\n");
  61.                 output.Append("Fetching Google IO...\n");
  62.  
  63.                 Console.WriteLine(output.ToString());
  64.                 output.Clear();
  65.  
  66.                 try
  67.                 {
  68.                     currentResponse = (HttpWebResponse)currentRequest.GetResponse();
  69.                     output.Append("Response URI: ");
  70.                     output.Append(currentResponse.ResponseUri.ToString());
  71.                     output.Append("\n");
  72.  
  73.                     if (!urlRedirect.Equals(currentResponse.ResponseUri.ToString()))
  74.                     {
  75.                         //If the url redirect has changed
  76.                         output.Append("The Google IO 2012 Site is up!\n");
  77.                         Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
  78.                         MailItem newEmail = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
  79.                         newEmail.Recipients.Add(emailAddress);
  80.                         newEmail.Subject = emailSubject;
  81.                         newEmail.Body = emailBody;
  82.                         newEmail.Send();
  83.  
  84.                         Console.WriteLine(output.ToString());
  85.                         output.Clear();
  86.  
  87.                         break;
  88.                     }
  89.                     else
  90.                     {
  91.                         output.Append("Nothing has changed yet.\n");
  92.                     }
  93.  
  94.                     currentResponse.Close();
  95.  
  96.                     output.Append(url);
  97.                     output.Append(" request successful.");
  98.  
  99.                     if (backoffTime != Math.Max(minBackoffTime, (int)((float)backoffTime * (2.0f / 3.0f))))
  100.                     {
  101.                         //If the backoff time can be reduced
  102.                         output.Append(" Decreasing backoff time from ");
  103.                         output.Append(backoffTime);
  104.                         output.Append(" seconds to ");
  105.  
  106.                         backoffTime = Math.Max(minBackoffTime, (int)((float)backoffTime * (2.0f / 3.0f)));
  107.  
  108.                         output.Append(backoffTime);
  109.                         output.Append(" seconds.");
  110.                     }
  111.  
  112.                     output.Append("\n");
  113.                 }
  114.                 catch (WebException e)
  115.                 {
  116.                     if (e.Status == WebExceptionStatus.Timeout)
  117.                     {
  118.                         //Backoff a bit
  119.                         output.Append(url);
  120.                         output.Append(" timed out. Increasing backoff time from ");
  121.                         output.Append(backoffTime);
  122.                         output.Append(" seconds to ");
  123.  
  124.                         backoffTime = (int)Math.Ceiling(backoffTime * 1.5);
  125.  
  126.                         output.Append(backoffTime);
  127.                     }
  128.                     else
  129.                     {
  130.                         output.Append("Web exception: ");
  131.                         output.Append(e.Message);
  132.                     }
  133.  
  134.                     output.Append("\n");
  135.                 }
  136.                 finally
  137.                 {
  138.                     output.Append("Waiting ");
  139.                     output.Append(backoffTime);
  140.                     output.Append(" seconds until next request.\n\n");
  141.  
  142.                     Console.Write(output.ToString());
  143.                     output.Clear();
  144.                     Thread.Sleep(backoffTime * 1000);
  145.                 }
  146.                
  147.             }
  148.             Console.WriteLine("Program ended.");
  149.             Console.WriteLine("Press any key to exit.");
  150.             Console.ReadKey();
  151.         }
  152.     }
  153. }
Add Comment
Please, Sign In to add comment