Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google Flight API - Unlimited Key Queries
#!/usr/bin/python
import json,os,subprocess,requests,sys,datetime,smtplib
from datetime import timedelta
from pprint import pprint
SOURCES = ["BOM","BDQ","DEL"]
DESTINATIONS = ["BOM","BDQ","DEL","HYD","SFO","IAD","DXB","SIN","LHR","IXZ"]
WHEN = str(datetime.datetime.now() + datetime.timedelta(days=10)).split(" ")[0]
KEYS=["KEY1","KEY2","KEY3","KEY4","KEY5","KEY6","KEY7","KEY8","KEY9","KEY10","KEY11","KEY12","KEY13","KEY14","KEY15"]
def getapikey():
for KEY in KEYS:
REQUEST_JSON = '{ "request": { "passengers": { "adultCount": 1 }, "slice": [ { "origin": "BOM", "destination": "BDQ", "date": "2017-12-30" } ] } }'
url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=" + KEY
headers = {'content-type': 'application/json'}
response = requests.post(url, data=REQUEST_JSON, headers=headers)
data = response.json()
try:
test = data['trips']
return KEY
except :
pass
def getflightdetails(src,dst,WHEN):
REQUEST_JSON = '{\
"request": {\
"passengers": {\
"adultCount": 1\
},\
"slice": [\
{\
"origin": "FROM",\
"destination": "TO",\
"date": "WHEN"\
}\
]\
}\
}'
REQUEST_JSON =REQUEST_JSON.replace("FROM",src)
REQUEST_JSON =REQUEST_JSON.replace("TO",dst)
REQUEST_JSON =REQUEST_JSON.replace("WHEN",WHEN)
api_key = getapikey()
url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=" + api_key
headers = {'content-type': 'application/json'}
response = requests.post(url, data=REQUEST_JSON, headers=headers)
data = response.json()
try:
return src, dst, data["trips"]["tripOption"][0]["saleTotal"], WHEN , data["trips"]["data"]["carrier"][0]["name"]
except:
getflightdetails(src,dst,WHEN)
content = []
for src in SOURCES:
for dst in DESTINATIONS:
if src != dst:
content.append(str(getflightdetails(src,dst,WHEN)))
print content
file_op = open("content",'w')
file_op.write(('\n').join(content))
file_op.close()
subprocess.check_output("""cat content | awk -F"'" '{print $2 " " $4" " $6 " " $8" " $10}' | sed 's/INR/INR /g' | sort -n -k4 > mailer """,shell=True)
mail = subprocess.check_output("swaks --to hello@oswalpalash.com --from flights@oswalpalash.com --header 'Subject: Rates of Flights 10 Days from Today' --body mailer --helo google.com",shell = True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment