Skip to content

Instantly share code, notes, and snippets.

Created April 21, 2012 15:27
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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 IMAX.
You can search for multiple films and notify different email addresses.
__author__ = 'Sergio Garcez'
import sys, urllib, re, sched, time, smtplib
from email.mime.text import MIMEText
from bs4 import BeautifulSoup
except ImportError:
exit('This script require BeautifulSoup4. Please intall it via pip or easyinstall')
#make inside variable 'private' via underscore ... this matter when somebody do a from script import *
_SENDER = ''
BFI_URL = ''
def delay(function,delay=24,**kwargs):
result, kwargs = function(*args,**kwargs)
if delay > 0 and not result :
_s = sched.scheduler(time.time, time.sleep)
s.enter(delay, 1, delay, (function,delay,**kwargs))
def checkBFI(films, emails=None):
#no parenthesis
if not films:
#check the doc if Attribute Error is maybe better
raise ValueError("films list must exist")
f = urllib.urlopen(BFI_URL)
soup = BeautifulSoup(f)
tags =".view-content.view-content-taxonomy-term > a")
#debug or verbose mode
print('\nSearching for: \n %s' % ('\n '.join(films)) )
films_found = [film for film in films for tag in tags if film.lower() in tag.text.lower()]
#removed the line about removing the film found in the list.. doesnt bring much and restrict some
# optimistation
if films_found:
print('\nFound %s!' % ", ".joing(films_found))
#digest email, i hate receive loads email
if emails :
print('\nEmailing:\n %s' % '\n '.join(emails))
send_email(emails, 'Tickets are now available for: %s !' % films_found,
'Go get them: \n %s' % BFI_URL)
#use loggin.Debug for this type of print maybe ?
#print('Email sent.')
#no check on length needed and no parathese
if films :
return False, [set(films)-set(films_found), emails, delay]
print('\nFound all films, stopping.')
return True, [[], emails, delay]
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.")
(options, args) = parser.parse_args()
#if you are using logic base on iterator avoid a default value as None , otherwise all loop will fail
emails = map(strip, options.emails.split(',')) if options.emails else []
films = map(strip, options.films.split(',')) if options.films else []
delay = options.delay * 60 * 60
# splitted delay and checkfilms to be reusable
delay(checkBFIfilms,delay, films=films, emails=emails )
if __name__ == '__main__':
#!/usr/bin/env python
from setuptools import setup, find_packages
long_description = """ """
description='short description',
author='Sergio Garcez',
license = "MIT|BSD|",
'console_scripts': [
'imax = imax.imax:main',
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment