C# Woot! Off Notifier

On January 19, 2012, in C#, Programming, by LuCuS

woot! LogoYesterday, I showed you how to create a Woot! Off notifier using Python. As promised, I’m now going to show you how to create a Woot! Off notifier using C#. In case you didn’t read yesterday’s article, it basically told how I love Woot! Off’s, but hate having to constantly refresh my browser to check for new items. Instead, I decided to write a tool that does that for me. In the Python version I wrote, it only displays the item name and price. In today’s article, I’m taking it a bit further by showing you how to create a Windows Form that displays the item description along with the item name and price. And, I even show you how to display the item image and the progress bar that shows how many items are left just like on the Woot! site. So, let’s get started.

The first thing you need to do is to fire up Visual C# and create yourself a new Windows Form application. Once you have your application built, you will need to add a few controls to your form. So, go ahead and add a picture box control for displaying the item image, a rich text box control for displaying the item information such as name, price, and description, a progress bar for showing how many items are remaining, a numeric up / down control for setting the wait interval, and a button control for starting and stopping the thread that we’ll use for our timer. You can layout your form however you want. Here’s what my layout looks like:

C# Woot! Off Form LayoutFor this demo, I’ll be working with System.Threading.Thread to give our application a way to repeat the check for new items. But, you don’t have to use threads. If you’d prefer, you can use the Background Worker Control found in your toolbox. The reason you need to use either a Thread or a Background Worker is because your app will freeze, preventing you from changing your wait interval or clicking the Stop button while the application waits. Here are the dependencies I’m working with and the thread I’m using to do the work. You’ll also see that I’ve declared the Woot! url as a global variable as well. This isn’t really needed, but I did it anyways.

using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;

private Thread _workerThread;
private string _url = http://www.woot.com;

Typically, when working with threads, you will need to use delegates when accessing form components to prevent cross thread exceptions. But, to keep things simple, we’re just going to tell our app to not check for illegal cross thread calls. To do that, we only need to add one line to our form’s constructor.

CheckForIllegalCrossThreadCalls = false;

Next, you will need to add a click event listener to the button you added earlier. This event handler is where we will create and start our worker thread. When you build your new thread, you will need to pass it a function which will be where the work actually takes place. I called my thread function “Run” as shown below, but you can call yours whatever you want.

_workerThread = new Thread(new ThreadStart(Run));

After you’ve setup your thread, make sure you have a function created with the same name as the name you passed to your ThreadStart above. That’s where we’ll be doing all of the heavy lifting. Instead of getting into all of the details about how to make your call to Woot!, I’m just going to go ahead and show you the code.

        private void Run()
            WebClient wc = new WebClient();
            wc.UseDefaultCredentials = true;
            wc.Proxy = WebRequest.DefaultWebProxy;
            wc.Proxy.Credentials = CredentialCache.DefaultCredentials;

            while (true)
                txtDescription.Text = "";
                pctImage.Image = null;
                status.Value = 0;

                string html = wc.DownloadString(_url);
                Match m = Regex.Match(html, "<meta property=\"og:title\" content=\"(.*?)\" />");
                if (m.Success)
                    string title = m.Groups[1].Value;
                    txtDescription.AppendText(title + "\n\n");
                m = Regex.Match(html, "<span class=\"amount\">(.*?)</span>");
                if (m.Success)
                    string amount = m.Groups[1].Value;
                    txtDescription.AppendText("$" + amount + "\n\n");
                m = Regex.Match(html, "<meta property=\"og:image\" content=\"(.*?)\" />");
                if (m.Success)
                    string imgUrl = m.Groups[1].Value;
                    pctImage.ImageLocation = imgUrl;
                m = Regex.Match(html, "<div class='wootOffProgressBarValue' style='width:(.*?)%'></div>");
                if (m.Success)
                    int s = int.Parse(m.Groups[1].Value);
                    status.Value = s;
                m = Regex.Match(html.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace("  ", ""), "<dt>Product:</dt><dd>(.*?)</dd>");
                if (m.Success)
                    string description = m.Groups[1].Value.Trim();
                    txtDescription.AppendText(description + "\n");

                Thread.Sleep(Convert.ToInt32(interval.Value) * 1000);

The most important piece to note here is that I’m using System.Net.WebClient to make the call to Woot! to download the HTML which we’ll be scraping to find the important pieces. You’ll also notice that I’ve wrapped a good portion of this code with a “while” loop that never ends. That’s because I want this app to continuously check for new items either until I close the app or click the Stop button. I’m using System.Text.RegularExpressions.Regex to search for the item name, price, description, and image URL. Once found, that information gets appended to the rich text box we created earlier. If we hadn’t added the “CheckForIllegalCrossThreadCalls = false” line earlier, this is where we would’ve hit the Illegal Cross Thread Exception.

At the end of the function, you’ll see that I’m also System.Threading.Thread to tell the application to “wait” for a given amount of time. The Sleep method expects a time interval in milliseconds. Since our numeric up / down control only has the time in seconds, we simply multiply that value times 1000 to get the time into milliseconds.

Well, that’s pretty much it. If everything worked accordingly, you should be able to run your app and should see something like shown below. Keep in mind that this is only intended to be used on days that there’s a Woot! Off happening. Even though it’ll probably work on other days, it hasn’t been tested and might not work as expected. If you are interested, I have provided the entire application solution available for download from http://www.prodigyproductionsllc.com/downloads/CSharpWootOff.zip. It contains a couple of extra things not mentioned here that make the app complete such as checking if the worker thread is not null and is active and killing it if it is when closing the app.

C# Woot! Off Notifier ExampleAs always, please leave your comments, questions, and suggestions in the comments section below and I’ll answer you as soon as possible. Until next time, HAPPY CODING!

Thank you for your interest in my site. If you find the information provided on this site useful, please consider making a donation to help continue development!

PayPal will open in a new tab.

Related Posts

Tagged with:  

Leave a Reply