Skip to content

Instantly share code, notes, and snippets.

Created Apr 21, 2012
What would you like to do?
Simple script to check when tickets go on sale at the IMAX. You can search for multiple films and notify different email addresses.
#!/usr/bin/env python
Simple script to check when tickets go on sale at the London IMAX.
You can search for multiple films and notify different email addresses.
__author__ = 'Sergio Garcez'
import sys, urllib, sched, time, smtplib, logging
from email.mime.text import MIMEText
from bs4 import BeautifulSoup
except ImportError:
exit('This script requires BeautifulSoup4. Please install it via pip or easyinstall')
_SENDER = ''
BFI_URL = ''
def delayedCall(function,delay=24,*args):
result, retargs = function(*args)
if delay > 0 and not result :
logging.getLogger('imax').info( "Waiting %s hours to search again." % delay )
_s = sched.scheduler(time.time, time.sleep)
_s.enter(delay*60*60, 1, delayedCall, [function,delay]+retargs)
def checkBFI(films, emails=None):
if not films:
raise ValueError("film list must exist")
f = urllib.urlopen(BFI_URL)
soup = BeautifulSoup(f)
tags =".view-content.view-content-taxonomy-term > a")
logging.getLogger('imax').info('Searching for: %s' % (', '.join(films)) )
films_found = [film for film in films for tag in tags if film.lower() in tag.text.lower()]
if films_found:
logging.getLogger('imax').info('Found %s!' % ", ".join(films_found))
if emails :
logging.getLogger('imax').info('Emailing: %s' % ', '.join(emails))
send_email(emails, 'Tickets are now available for: %s !' % ', '.join(films_found),
'Here:\n %s' % BFI_URL)
if films :
return False, [set(films)-set(films_found), emails]
logging.getLogger('imax').info('Found all films, stopping.')
return True
def send_email(receivers, subject, body):
msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s"
%(_SENDER, receivers, subject, body))
server = smtplib.SMTP(_SMTP_SERVER_URL)
server.sendmail(_SENDER, receivers, msg)
def main():
from optparse import OptionParser
from string import strip
parser = OptionParser("usage: %prog -f 'Titanic, Lady From Shanghai' [-d 24 -e ',']")
parser.add_option("-e", "--emails",
dest="emails", type="string",
help="email addresses to be notified.")
parser.add_option("-f", "--films",
dest="films", type="string",
help="film names to search for.")
parser.add_option("-d", "--delay",
dest="delay", type="int", default=12,
help="time interval for searches. In hours.")
parser.add_option("-q", "--quiet",
action="store_true", dest="quiet", default=False,
help="don't print status messages to stdout")
(options, args) = parser.parse_args()
emails = map(strip, options.emails.split(',')) if options.emails else None
films = map(strip, options.films.split(',')) if options.films else None
formatter = logging.Formatter(
'%(asctime)-6s: %(name)s - %(levelname)s - %(message)s')
if not options.quiet:
consoleLogger = logging.StreamHandler()
delayedCall(checkBFI, options.delay, films, emails )
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment