Skip to content

Instantly share code, notes, and snippets.

@palfrey
Created August 12, 2012 11:58
Show Gist options
  • Select an option

  • Save palfrey/3331554 to your computer and use it in GitHub Desktop.

Select an option

Save palfrey/3331554 to your computer and use it in GitHub Desktop.
Price checker for olympic tickets
  1. Download grabber.py and settings.py.example. Rename the latter to settings.py and edit as appropriate for your local system.
  2. git clone https://github.com/palfrey/urlgrab.git urlgrab
  3. Run python grabber.py <event id> <max price> (where event id is from a page like http://www.tickets.london2012.com/eventdetails?id=000046BFAE4D12A7 and is that id field at the end)

You will now get emailed once a ticket becomes available at that price, and every time after that when prices drop further.

#!/usr/bin/python
# vim: set fileencoding=utf-8 :
from urlgrab import Cache
import email.mime, smtplib, email.mime.text
from settings import *
import re
from sys import argv
import time
id = argv[1]
limit = int(argv[2])
cache = Cache()
select = re.compile("<select id=\"price_category\"[^>]+>(.*?)</select>", re.MULTILINE | re.DOTALL)
option = re.compile("<option value=\"\d+\" price=\"(\d+)\"")
url = "http://www.tickets.london2012.com/eventdetails?id="+ id
best = None
while True:
data = cache.get(url, max_age=60).read()
selector = select.search(data)
assert selector != None
options = sorted([int(x) for x in option.findall(selector.groups()[0])])
assert len(options) > 0, selector
print time.asctime(), "cheapest", options[0], "wanted", limit, id
if options[0] <= limit and (best == None or best > options[0]):
msg = email.mime.Multipart.MIMEMultipart()
msg["From"] = srcEmail
msg["To"] = destEmail
msg["Subject"] = "Cheaper olympic ticket"
doc = email.mime.text.MIMEText("Cheaper price £%d at %s" %(options[0], url))
msg.attach(doc)
s = smtplib.SMTP(server, port)
if username!= None:
s.ehlo()
s.starttls()
s.ehlo()
s.login(username, password)
s.sendmail(srcEmail, destEmail, msg.as_string())
s.close()
best = options[0]
time.sleep(300) # 5 minutes
destEmail = "youremail@blah.com"
srcEmail = "an_allowed_email@example.com"
server = "smtp.gmail.com"
port = 587
username = None # set to a string to login
password = None # set to a string to login
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment