Skip to content

Instantly share code, notes, and snippets.

@rach
Created April 21, 2012 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rach/2437728 to your computer and use it in GitHub Desktop.
Save rach/2437728 to your computer and use it in GitHub Desktop.
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
try:
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 = '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 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))
s.run()
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 = soup.select(".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]
else:
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.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.")
(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__':
main()
#!/usr/bin/env python
from setuptools import setup, find_packages
long_description = """ """
setup(
name='imax',
version='0.1',
description='short description',
long_description=long_description,
author='Sergio Garcez',
author_email='xxx@xxx.com',
url='https://yoururl',
packages=find_packages(),
license = "MIT|BSD|",
entry_points={
'console_scripts': [
'imax = imax.imax:main',
]
},
install_requires=[
'BeautifulSoup4',
]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment