Skip to content

Instantly share code, notes, and snippets.

@scollinson
Created July 17, 2012 03:21
Show Gist options
  • Save scollinson/3126804 to your computer and use it in GitHub Desktop.
Save scollinson/3126804 to your computer and use it in GitHub Desktop.
kiwicon grabaseat script
#!/usr/bin/env python
"""
Script to poll grabaseat.co.nz for cheap flights to and from conferences
Written by Sam Collinson (smc@affinity.net.nz) on 19/08/10
** Updated 17/07/12 with new URL
"""
import urllib, urllib2
from datetime import datetime, date
from sys import version_info
if version_info < (2, 6):
from json import read as loads
else:
from json import loads
def getSpecials():
url = "http://grabaseat.co.nz/getSpecials.json"
fields = urllib.urlencode({"specialTypes[0]": "LUDICROUS", "specialTypes[1]": "DJ_SPECIAL"})
request = urllib2.Request(url, fields, headers={'User-Agent': ' Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0.1'})
response = urllib2.urlopen(request)
jsondata = response.read()
data = loads(jsondata)
return data['special']
def grabaseat(specials, fromLocation, toLocation, travelDates):
dateformat = "%Y-%m-%d"
for s in specials:
if s['@status'] == "ForSale":
specialStart = datetime.strptime(s['@displayFromDate'], dateformat).date()
specialEnd = datetime.strptime(s['@displayToDate'], dateformat).date()
specialFrom = s['@originIATACode']
specialTo = s['@destinationIATACode']
if specialFrom == fromLocation and specialTo == toLocation and canTravel(specialStart, specialEnd, travelDates):
print "Flight found from %(@originName)s - %(@destinationName)s %(@flightType)s for $%(@price)s, %(@seatCount)s seats left. Goto http://grabaseat.co.nz to book." % s
def canTravel(specialStart, specialEnd, travelDates):
for td in travelDates:
if td.toordinal() <= specialEnd.toordinal() and td.toordinal() >= specialStart.toordinal():
return True
return False
if __name__ == '__main__':
home = "AKL"
kiwicon = "WLG"
departdates = [date(2012, 11, 16)]
returndates = [date(2012, 11, 19)]
specials = getSpecials()
grabaseat(specials, home, kiwicon, departdates)
grabaseat(specials, kiwicon, home, returndates)
@captainpete
Copy link

Nice :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment