Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Docs
# https://rapidapi.com/skyscanner/api/skyscanner-flight-search?endpoint=5a9b572de4b06ec3937b1296
#
# Using the skyscanner API
# install unirest into virtual env "flight"
#
# activate virtualenv
#
# workon flight
#
# Details
# From Jul 15 - Aug 30
# considering 5 day window
import unirest
import csv
import time
API_KEY = ""
origins = ['SYD', 'ALO', 'ZRH', 'MAD', 'DUB', 'NRT', 'ICN', 'WAW', 'ATL', 'AUS', 'DEN', 'SEA', 'BOS', 'DFW', 'SEA', 'SFO', 'JFK', 'PIT', 'LAX', 'SEA', 'SFO', 'SFO']
dests = ['my_dest1', 'my_dest2']
dates = [("2019-07-15", "....")]
# overwrite CSV with headers
with open('flights.csv', 'w') as fd:
fd.write('origin, dest, outbound, inbound, minPrice\n')
for date in dates:
for dest in dests:
for origin in origins:
# first create a session - this encodes the params
response = unirest.post("https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0",
headers={
"X-RapidAPI-Key": API_KEY,
"Content-Type": "application/x-www-form-urlencoded"
},
params={
"inboundDate": date[1],
"cabinClass": "economy",
"children": 0,
"infants": 0,
"country": "US",
"currency": "USD",
"locale": "en-US",
"originPlace": "{origin}-sky".format(origin=origin),
"destinationPlace": "{dest}-sky".format(dest=dest),
"outboundDate": date[0],
"adults": 1
}
)
# store session key to use in next request
print response.body
# error from response if origin = dest
if origin == dest:
minPrice = 0
else:
session_id = response.headers.items()[3][1].split('v1.0/')[1]
url = "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/uk2/v1.0/{session_id}?sortType=price&sortOrder=asc&pageIndex=0&pageSize=10"
url_params = url.format(session_id=session_id)
response = unirest.get(url_params,
headers={
"X-RapidAPI-Key": API_KEY
}
)
# Origin / Dest / Minimum price
if len(response.body['Itineraries']) > 0:
minPrice = response.body["Itineraries"][0]['PricingOptions'][0]['Price']
else:
minPrice = 0
print "Origin={origin}, Destination={dest}, Outbound={outbound}, Inbound={inbound}, Min Price={minPrice}".format(
origin=origin, dest=dest, outbound=date[1], inbound=date[0], minPrice=minPrice
)
summary = '{origin}, {dest}, {outbound}, {inbound}, {minPrice}\n'.format(
origin=origin, dest=dest, outbound=date[1], inbound=date[0], minPrice=minPrice
)
with open('flights.csv', 'a') as fd:
fd.write(summary)
# wait 5s to ensure API is ok with requests
time.sleep(5)
print 'waiting....'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.