PS: Powershell API SkyScanner

PS: Powershell API SkyScanner

Skyscanner has got an API that allows you to pull data such as pricing on flights and here is a quick example on how to do this the powershell way! So to give you a quick idea to how the API works i have put together this script that will pull the latest prices from skyscanner and output them into the scripts running directory as a csv document

$FlightConfig = @{
 
    Country = "UK"
    # Output Currency
    Currency = "GBP"
    locality = "en-US"
    # Airport Code for London Luton
    originPlace = "LTN-sky"
    # Airport Code for Bucharest Airport
    destinationPlace = "OTP-sky"
    # Change the date to something in the future
    outboundDate = "2019-11-10"
    # Number of traveling adults
    adults = "1"
}
 
$headers=@{}
$headers.Add("x-rapidapi-host", "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com")
$headers.Add("x-rapidapi-key", "bd65daf56amshcae9b435bccb095p1449fejsncf2eda31df2b")
$headers.Add("content-type", "application/x-www-form-urlencoded")
$body = @( "country=" + $FlightConfig.Country + '¤cy=' + $FlightConfig.Currency + '&locale=' + $FlightConfig.locality + '&originPlace=' + $FlightConfig.originPlace + '&destinationPlace=' + $FlightConfig.destinationPlace + '&outboundDate=' + $FlightConfig.outboundDate + '&adults=' + $FlightConfig.adults) 
$Session = Invoke-WebRequest -Uri 'https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0' -Method POST -Headers $headers -ContentType 'application/x-www-form-urlencoded' -Body $body
$ID = $Session.Headers.Location | Split-Path -Leaf
$uri = "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/uk2/v1.0/" + $ID + "?pageIndex=0&pageSize=10"
$response = Invoke-WebRequest -Uri $uri -Method GET -Headers $headers
$fresponse = $response | ConvertFrom-Json | Select-Object -ExpandProperty Itineraries | Select-Object -ExpandProperty PricingOptions | Select-Object Price, QuoteAgeInMinutes, DeeplinkUrl | Sort-Object -Property Price
$fresponse | Export-Csv .\export.csv

3 thoughts on “PS: Powershell API SkyScanner

  1. Hello.
    I worked on your idea a little bit just because I know some people tend to want more info.
    Maybe some switches and parameter can be added to just default on the $FlightConfig part in case none are set.

    $FlightConfig = @{

    Country = “UK”
    # Output Currency
    Currency = “EUR”
    locality = “en-US”
    # Airport Code for Eindhoven
    originPlace = “EIN-sky”
    # Airport Code for Bucharest Airport
    destinationPlace = “OTP-sky”
    # Change the date to something in the future
    outboundDate = “2019-11-15”
    # Number of traveling adults
    adults = “1”
    # Number of traveling children
    children = “0”
    }

    $headers=@{}
    $headers.Add(“x-rapidapi-host”, “skyscanner-skyscanner-flight-search-v1.p.rapidapi.com”)
    $headers.Add(“x-rapidapi-key”, “bd65daf56amshcae9b435bccb095p1449fejsncf2eda31df2b”)
    $headers.Add(“content-type”, “application/x-www-form-urlencoded”)
    $body = @( “country=” + $FlightConfig.Country + ‘&currency=’ + $FlightConfig.Currency + ‘&locale=’ + $FlightConfig.locality + ‘&originPlace=’ + $FlightConfig.originPlace + ‘&destinationPlace=’ + $FlightConfig.destinationPlace + ‘&outboundDate=’ + $FlightConfig.outboundDate + ‘&adults=’ + $FlightConfig.adults)
    $Session = Invoke-WebRequest -Uri ‘https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0’ -Method POST -Headers $headers -ContentType ‘application/x-www-form-urlencoded’ -Body $body
    $ID = $Session.Headers.Location | Split-Path -Leaf
    $uri = “https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/uk2/v1.0/” + $ID + “?pageIndex=0&pageSize=200”
    $response = Invoke-WebRequest -Uri $uri -Method GET -Headers $headers

    $x = $response.Content | ConvertFrom-Json

    $count = ($x.Itineraries).count

    $tabName = “SampleTable”
    #Create Table object
    $Flights = New-Object system.Data.DataTable “$tabName”
    #Define Columns
    $colFlight = New-Object system.Data.DataColumn Flight,([string])
    $colCarrier = New-Object system.Data.DataColumn Carrier,([string])
    $colAgent = New-Object system.Data.DataColumn Agent,([string])
    $colPrice = New-Object system.Data.DataColumn Price,([string])
    $colDepartureTime = New-Object system.Data.DataColumn DepartureTime,([string])
    $colArrivalTime = New-Object system.Data.DataColumn ArrivalTime,([string])
    $colDuration = New-Object system.Data.DataColumn Duration,([string])
    $colCode = New-Object system.Data.DataColumn Code,([string])
    $colQuoteAge = New-Object system.Data.DataColumn QuoteAge,([string])
    $colDeepLink = New-Object system.Data.DataColumn DeepLink,([string])

    #Add the Columns
    $Flights.columns.add($colCode)
    $Flights.columns.add($colFlight)
    $Flights.columns.add($colCarrier)
    $Flights.columns.add($colAgent)
    $Flights.columns.add($colPrice)
    $Flights.columns.add($colDepartureTime)
    $Flights.columns.add($colArrivalTime)
    $Flights.columns.add($colDuration)
    $Flights.columns.add($colQuoteAge)
    $Flights.columns.add($colDeepLink)
    $inc = 0

    for ($inc = 0; $inc -lt $count; $inc++) {

    write-host “=========== $inc ===========”
    #Initiate row
    $row = $Flights.NewRow()

    #Add flight number
    $FlightNumber = $x.Segments[$inc].FlightNumber
    $row.Flight = $FlightNumber
    write-host -NoNewline “Flight number:`t”;write-host -ForegroundColor Cyan $FlightNumber

    #Add carrier (air company)
    $Carrier = $x.Carriers[$inc].name
    $row.Carrier = $Carrier
    write-host -NoNewline “Carrier:`t”;write-host -ForegroundColor Cyan $Carrier

    #Add agent
    $Agent = $x.Agents[$inc].name
    $row.Agent = $Agent
    write-host -NoNewline “Agent:`t`t”;write-host -ForegroundColor Cyan $Agent

    #Add flight code
    $Code = $x.Carriers[$inc].code
    $row.Code = $Code
    write-host -NoNewline “Code:`t`t”;write-host -ForegroundColor Cyan $Code

    #Add departure time
    $DepartureTime = $x.Segments[$inc].DepartureDateTime
    $row.DepartureTime = $DepartureTime
    write-host -NoNewline “Departure Time: “;write-host -ForegroundColor Cyan $DepartureTime

    #Add arrival time
    $ArrivalTime = $x.Segments[$inc].ArrivalDateTime
    $row.ArrivalTime = $ArrivalTime
    write-host -NoNewline “Arrival Time:`t”;write-host -ForegroundColor Cyan $ArrivalTime

    #Add flight duration in minutes
    $Duration = $x.Segments[$inc].Duration
    $row.Duration = $Duration
    write-host -NoNewline “Duration:`t”;write-host -ForegroundColor Cyan $Duration

    #Add price
    [int]$Price = $x.Itineraries[$inc].PricingOptions.Price[0]
    $row.Price = $Price
    write-host -NoNewline “Flight Price:`t”;write-host -ForegroundColor Cyan $Price

    #Add quote age (how old the quote is)
    $QuoteAge = $x.Itineraries[$inc].PricingOptions.QuoteAgeInMinutes[0]
    $row.QuoteAge = $QuoteAge
    write-host -NoNewline “Quote age:`t”;write-host -ForegroundColor Cyan $QuoteAge

    #Add link to the flight
    $DeepLink = “$($x.Itineraries[$inc].PricingOptions.DeeplinkUrl)”
    $row.DeepLink = $DeepLink

    #Commit row to table
    $Flights.Rows.Add($row)

    }

    $Flights | Export-Csv -NoTypeInformation flights.csv

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s