Hello Developers,

I like to travel, and I've always wanted to get the flight ticket prices for some particular destinations. Being lazy, I had recently written a cron to fetch the details from qpxExpress API provided by Google.

The Requirement:

I wanted a solution, which would send me email updates about Flight prices for my favourite destinations and check if there's a price drop.

The Problem:

Google allows only limited number of queries per API key generated per APP for free.

The Solution:

Create lots of new Apps in Google Developer Console, and generate new API keys per app. Put those keys in an array and circulate the queries through them.

The Code:

#!/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)  

The Result: