Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.IO;
- using System.Threading;
- using Microsoft.Office.Interop.Outlook;
- using System.Text.RegularExpressions;
- namespace GoogleIOAlert
- {
- class Program
- {
- private const string url = "http://www.google.com/io";
- private const string urlRedirect = "http://www.google.com/events/io/2011/index-live.html";
- 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";
- private const int minBackoffTime = 10; //5 seconds
- private const string emailSubject = "Google I/O 2012 Site Updated";
- private const string emailBody = "Quick, go to " + url + "\n Maybe they opened registration!";
- private const string emailRegex = @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
- @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$";
- private static HttpWebRequest currentRequest;
- private static HttpWebResponse currentResponse;
- private static StringBuilder output;
- private static int backoffTime = minBackoffTime;
- private static string emailAddress;
- static void Main(string[] args)
- {
- //Get user email address
- Console.Write("Enter a valid email address to which you would like me to alert: ");
- emailAddress = Console.ReadLine();
- //Validate email address
- while (!Regex.IsMatch(emailAddress, emailRegex, RegexOptions.IgnoreCase))
- {
- //Get user email address
- Console.Write("Invalid email address, try again: ");
- emailAddress = Console.ReadLine();
- }
- Console.WriteLine("An email will be sent to " + emailAddress + " as soon as the redirect changes for " + url + "\n");
- int attemptNumber = 1;
- //Listen for changes
- while (true)
- {
- //Set request timeout and user agent
- currentRequest = (HttpWebRequest) HttpWebRequest.Create(url);
- currentRequest.Timeout = backoffTime * 1000;
- currentRequest.UserAgent = userAgent;
- output = new StringBuilder();
- output.Append("Attempt #");
- output.Append(attemptNumber++);
- output.Append(":\n");
- output.Append("Fetching Google IO...\n");
- Console.WriteLine(output.ToString());
- output.Clear();
- try
- {
- currentResponse = (HttpWebResponse)currentRequest.GetResponse();
- output.Append("Response URI: ");
- output.Append(currentResponse.ResponseUri.ToString());
- output.Append("\n");
- if (!urlRedirect.Equals(currentResponse.ResponseUri.ToString()))
- {
- //If the url redirect has changed
- output.Append("The Google IO 2012 Site is up!\n");
- Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
- MailItem newEmail = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
- newEmail.Recipients.Add(emailAddress);
- newEmail.Subject = emailSubject;
- newEmail.Body = emailBody;
- newEmail.Send();
- Console.WriteLine(output.ToString());
- output.Clear();
- break;
- }
- else
- {
- output.Append("Nothing has changed yet.\n");
- }
- currentResponse.Close();
- output.Append(url);
- output.Append(" request successful.");
- if (backoffTime != Math.Max(minBackoffTime, (int)((float)backoffTime * (2.0f / 3.0f))))
- {
- //If the backoff time can be reduced
- output.Append(" Decreasing backoff time from ");
- output.Append(backoffTime);
- output.Append(" seconds to ");
- backoffTime = Math.Max(minBackoffTime, (int)((float)backoffTime * (2.0f / 3.0f)));
- output.Append(backoffTime);
- output.Append(" seconds.");
- }
- output.Append("\n");
- }
- catch (WebException e)
- {
- if (e.Status == WebExceptionStatus.Timeout)
- {
- //Backoff a bit
- output.Append(url);
- output.Append(" timed out. Increasing backoff time from ");
- output.Append(backoffTime);
- output.Append(" seconds to ");
- backoffTime = (int)Math.Ceiling(backoffTime * 1.5);
- output.Append(backoffTime);
- }
- else
- {
- output.Append("Web exception: ");
- output.Append(e.Message);
- }
- output.Append("\n");
- }
- finally
- {
- output.Append("Waiting ");
- output.Append(backoffTime);
- output.Append(" seconds until next request.\n\n");
- Console.Write(output.ToString());
- output.Clear();
- Thread.Sleep(backoffTime * 1000);
- }
- }
- Console.WriteLine("Program ended.");
- Console.WriteLine("Press any key to exit.");
- Console.ReadKey();
- }
- }
- }
Add Comment
Please, Sign In to add comment