Skip to content

Instantly share code, notes, and snippets.

@rach
Created Apr 21, 2012
Embed
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
try:
from bs4 import BeautifulSoup
except ImportError:
exit('This script requires BeautifulSoup4. Please install it via pip or easyinstall')
_SENDER = 'xx@gmail.com'
_SMTP_SERVER_PWD = 'xx'
_SMTP_SERVER_URL = 'smtp.gmail.com:587'
BFI_URL = 'http://www.bfi.org.uk/whatson/bfi_imax/coming_soon/now_booking/'
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)
_s.run()
def checkBFI(films, emails=None):
if not films:
raise ValueError("film list must exist")
f = urllib.urlopen(BFI_URL)
soup = BeautifulSoup(f)
tags = soup.select(".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]
else:
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.starttls()
server.login(_SENDER,_SMTP_SERVER_PWD)
server.sendmail(_SENDER, receivers, msg)
server.quit()
def main():
from optparse import OptionParser
from string import strip
parser = OptionParser("usage: %prog -f 'Titanic, Lady From Shanghai' [-d 24 -e 'me@me.com, you@me.com']")
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()
consoleLogger.setFormatter(formatter)
logging.getLogger('imax').addHandler(consoleLogger)
logging.getLogger('imax').setLevel(logging.DEBUG)
delayedCall(checkBFI, options.delay, films, emails )
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment