Python Woot! Off Notifier

On January 18, 2012, in Programming, Python, by LuCuS

woot! LogoIf you’re like me, you enjoy the occasional Woot! Off. If you aren’t like me, there’s a good chance you don’t even know what a Woot! Off is. Hell, you probably don’t even know what Woot! is. If you aren’t familiar with Woot!, you should checkout an article I wrote a while back called “woot! woot! woot! woot!“. Catchy title, huh? Anyways, a Woot! Off is basically an event that happens periodically where the guys at Woot! sell off their inventory one item after the other until everything is gone. Unlike their normal routine of only having one deal a day, a Woot! Off can have several items in a short amount of time. It’s their way of cleaning house. But, you’ve gotta be quick if you want to land some of the good stuff as it goes quick! You have to constantly refresh your browser to see when something new has arrived. That’s what lead me to writing this article.

Instead of constantly hitting the refresh button in your browser, waiting for something new to arrive, wouldn’t it just be easier to write a quick tool that would poll Woot! for you? Yep. I thought so too. So, being the geek that I am, I fired up my Python Idle GUI and started hammering out some code to that will check with Woot! every 60 seconds and print out each item. This tool is a simple screen scraper that downloads the HTML code from Woot! and uses regular expressions to search for certain pieces of code, the pieces that contain things like item name and price.

I’ve written other articles showing how to create screen scrapers using Python. For this tool, I basically used the same thing, but added one extra class that provides me with a timer function which will be used to repeat the Woot! check every minute. I’m sure there are other ways of creating timers and I know there are all kinds of modules out there that do this for me. But, this was a small chunk of code that I used in another project and decided it would be an easy fit here as well. So, without further ado, here is the Python code I wrote for scraping Woot! and printing out the current item.

import urllib, sgmllib
import re

from threading import Event, Thread

class RepeatTimer(Thread):
    def __init__(self, interval, function, iterations=0, args=[], kwargs={}):
        Thread.__init__(self)
        self.interval = interval
        self.function = function
        self.iterations = iterations
        self.args = args
        self.kwargs = kwargs
        self.finished = Event()
 
    def run(self):
        count = 0
        while not self.finished.is_set() and (self.iterations <= 0 or count < self.iterations):
            self.finished.wait(self.interval)
            if not self.finished.is_set():
                self.function(*self.args, **self.kwargs)
                count += 1
 
    def cancel(self):
        self.finished.set()

class Woot(sgmllib.SGMLParser):
    def __init__(self, verbose = 0):
        sgmllib.SGMLParser.__init__(self, verbose)
        self.url = "http://www.woot.com"
        self.content = ""

    def get_content(self):
        self.content = urllib.urlopen(self.url).read()
        self.close()
        return self.content

    def get_item_title(self):
        s = self.get_content()
        m = re.search('<meta property="og:title" content="(.*?)" />', s)
        if m:
            return m.group(1)
        else:
            return ""

    def get_item_price(self):
        s = self.get_content()
        m = re.search('<span class="amount">(.*?)</span>', s)
        if m:
            return "${0}".format(m.group(1))
        else:
            return "N/A"

woot = Woot()

def check_woot():
    print "n"
    print woot.get_item_title()
    print woot.get_item_price()
    woot.close()

r = RepeatTimer(60.0, check_woot)
r.start()

If you run this code from your Idle GUI, you should see something that looks like this:

Python Woot! Off Notifier Example

You can change the wait time for the check to run by changing the first parameter passed to the RepeatTimer class at line 62 above. This parameter is defaulted to 60 seconds.

Feel free to extend this code by using IronPython or something similar to fire alerts when a new item is available. I was going to do that in this article, but decided against it as I’ve also created a C# version which I’ll be explaining in my next article. As always, please leave any questions, comments, and / or suggestions in the comments area below.

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.
$2.00
$5.00
Other

Related Posts

Tagged with:  

Leave a Reply