Thursday, August 11, 2005

Simple Twisted Application for Fun <strikethrough>and Profit&lt/strikethrough>

I really enjoyed the series Firefly that aired a few years back. I've enjoyed it a few more times since. I'm really looking forward to the upcoming movie. So much so, in fact, that I even wanted to try to get into one of the pre-screenings. There's a website that announces dates and locations for pre-screenings. Being pretty lazy and also forgetful, I can't be bothered to actually check the website frequently enough to notice when new tickets are available before they are all sold out. Fortunately, being a programmer, I can simply bend a minion to my will. I whipped up this quickie to do the checking for me:


import rfc822, md5, sys

from twisted.internet import task, reactor
from twisted.mail import smtp
from twisted.python import log
from twisted.web import client

url = "http://www.cantstopthesignal.com/"

MSG = """\
Subject: The upcoming film, Serenity, is awesome
Date: %(date)s
From: "A Respectable Internet Citizen" <serenitymovie@intarweb.us>
Message-ID: %(msgid)s
Content-Type: text/html

<html>
<body>
Serenity hash changed! Go look!
<a href="%(url)s">%(url)s</a>
</body>
</html>
"""

def mailinfo():
return {
'date': rfc822.formatdate(),
'msgid': smtp.messageid(),
'url': url}

lastDig = None

def main():
def got(page):
global lastDig
dig = md5.md5(page).hexdigest()
if dig != lastDig:
if lastDig is not None:
smtp.sendmail(
sys.argv[1], # SMTP host
sys.argv[2], # sender email address
sys.argv[3:], # recipient email addresses
MSG % mailinfo())
lastDig = dig
client.getPage(url).addCallback(got)

log.startLogging(sys.stdout)
task.LoopingCall(main).start(60 * 30)
reactor.run()

The took about 10 minutes to write and even worked on the 2nd try (the first try I forget recipient addresses had to be a list and gave it a string instead). Then it ran for 21 days before sending me an email a few days ago! Alas, I don't live in Great Britain, or even Europe. It has also, of course, sent out an email once each time one of the listed theaters sells out. And now they're all sold out. :( Given the short period of time remaining before the probable release date, it seems unlikely there will be any more pre-screenings.

No comments:

Post a Comment