How to Beat HGTV
workflow python unix

The Problem

One of my wife’s favorite things on TV are the HGTV Dream Home contests. For the uninitiated, HGTV is a cable channel, which airs content about home renovations and interior design. One of my wife’s favorite things on HGTV are the HGTV Dream Home contests.

HGTV Dream Home contests consist of giving away multi-million dollar homes to lucky contestants who register daily on the HGTV web site. The latest Dream Home contest is the Chicago Urban Oasis. While I think it would be fantastic to win one of these homes, I’m lazy and I’m not about to visit the HGTV web site and fill out the entry form each day.

The Solution

To make my wife happy and maintain my laziness, I automated the HGTV contest entry process by turning to Python. For my purposes I used the browser emulator module mechanize to automate the HGTV entry form submission process. For the Chicago Dream Home entry form, I used mechanize to create a simple generator function called mech_hgtv that spawns a browser instance that fills out the Dream Home entry form and submits it. Each time the function is called the output html is written to a file and the time_stamp function logs each event to a log file. Here’s the code:

#!/usr/bin/env python
# encoding: utf-8
"""
Seth Brown
2011-9-25
Python 2.7.1

Dependancies:
    mechanize
"""

from time import localtime
import mechanize

def mech_hgtv(url, form, f_name, l_name, email, address, \
             city, state, zipcode, phone, gender, cable):
    """ mechanize hgtv dream home form 
    """
    br = mechanize.Browser()
    br.open(url)
    br.select_form(name = form)
    br['fvFirstName'] = f_name
    br['fvLastName'] = l_name 
    br['fvEmail'] = email
    br['fvConfirmEmail'] = email
    br['fvAddress1'] = address
    br['fvCity'] = city
    br.form.set_value([state],name='fvState')
    br['fvZip'] = zipcode
    # xxx xxx-xxxx format
    br['fvPhone'] = phone
    # 'm' or 'f'
    br.form.set_value([gender],name='fvGender')
    br.form.set_value([cable],name='fvOptIn33')
    br.submit()
    output =  br.response().read()
    yield output

def time_stamp():
    """local time stamp eg. YYYY:MM:DD:HH:MM"""
    stamp = localtime()
    year, month, day, hour, minute  = stamp[0:5]
    time_stamp_format = "{0}:{1}:{2}:{3}:{4}"\
            .format(year, month, day, hour, minute)
    return time_stamp_format

if __name__ == '__main__':

    sites = ["http://www.hgtv.com/the-url.html",
            "http://www.hgtvremodels.com/the-url.html]

    users = [['Seth', 'Brown', 'seth@drbunsen.org', '122 555-1212', 'm'],
             ['Sue', 'Brown', 'sue@gmail.com', '122 555-1213', 'f']]

    (mech_hgtv(site, 'sweepsEntryForm', u[0], u[1], u[2], '3 Woods Hole', 
        'Seattle', 'WA', '98109', u[3], u[4], 'media') 
        for u in users for site in sites)

    submit_time = time_stamp()

    with open('log.txt', mode="a") as log:
        log.write(submit_time)

The above Python code works, but it requires manually running the program each day to submit the entry form to the HGTV web site. To fix this and make the submission process entirely automated, I used the UNIX job scheduler cron.1 The Chicago Urban Oasis contest runs until October 7th, so I configured my crontab config file like this:2

#min hour    day/mon month   day/week    command`  
00  6   *   9   *   /usr/bin/python /Users/sbrown/Desktop/hgtv.py

00  6   1-7 10  *   /usr/bin/python /Users/sbrown/Desktop/hgtv.py

The end result of using Python, mechanize, and cron is a triumvirate of productivity and daily automated HGTV Dream Home contest entry goodness. cron controls the daily running of the Python script.


  1. Launchd is the preferred to cron on OSX. I'm more familiar with cron, so I opted to use it instead of launchd. ↩

  2. Don't forget to add a shebang with the path to the interpreter binary in your Python script. cron doesn’t have all the environment variables set as in the shell, so you may need to specify the path explicitly. If you've installed mechanize with a specific version of Python, make sure you use this version of Python in your crontab file. ↩