Query plane tickets, from several airlines, using the Kiwi API (similar to Google Flights).
The API is documented at https://docs.kiwi.com/.
library("rflights")
# get Argentina and toulouse IDs
arg_id <- find_location("Argentina", "country")
length(arg_id) # only one result, so it might be the one
## [1] 1
arg_id <- arg_id[[1]]
names(arg_id)
## [1] "id" "active" "code"
## [4] "name" "slug" "alternative_names"
## [7] "rank" "global_rank_dst" "neighbours"
## [10] "organizations" "currency" "region"
## [13] "continent" "location" "type"
arg_id$id
## [1] "AR"
arg_id$continent
## $id
## [1] "south-america"
##
## $code
## [1] "SA"
##
## $name
## [1] "South America"
##
## $slug
## [1] "south-america"
arg_id <- arg_id$id
tl_id <- find_location("toulouse")
length(tl_id)
## [1] 5
lapply(tl_id, function(x) x$type)
## [[1]]
## [1] "city"
##
## [[2]]
## [1] "airport"
##
## [[3]]
## [1] "bus_station"
##
## [[4]]
## [1] "tourist_region"
##
## [[5]]
## [1] "bus_station"
# we are looking for the city
tl_id <- tl_id[[which(sapply(tl_id, function(x) x$type == "city"))]]
tl_id$country
## $id
## [1] "FR"
##
## $name
## [1] "France"
##
## $slug
## [1] "france"
##
## $code
## [1] "FR"
tl_id <- tl_id$id
tl_id
## [1] "toulouse_fr"
# get flights from Argentina to toulouse around 01 July to 09 July
# Maybe I can go to the useR! 2019?
flights <- get_flights(
fly_from = "AR", fly_to = "toulouse_fr",
date_from = "01/09/2019", date_to = "09/09/2019"
)
length(flights)
names(flights[[1]])
sapply(flights, function(x) x$price)
I used it to alert through a Pushbullet message.
my_savings <- 25 # yup, just 25USD ound_ticket <- FALSE
while (!found_ticket) {
flights <- get_flights(
fly_from = "AR", fly_to = "toulouse_fr",
date_from = "01/09/2019", date_to = "09/09/2019"
)
flights <- flights[sapply(flights, function(x) x$price) <= my_savings]
if (length(flights) > 0) {
send_alert(paste0(
"There is a plane ticket you can afford!\n",
"Check it out at Kiwi.com"
))
# user-defined alert function (not in rflights)
}
}
Find plane tickets from my city to anywhere, from today to 2 next weeks.
# I am a freelancer, let's go anywhere!
flights <- get_flights(
fly_from = "COR",
date_from = Sys.Date(), date_to = Sys.Date() + 2 * 7
)
length(flights)
## [1] 234
head(t(sapply(flights, function(x) c(x$price, x$cityTo))), n = 20)
## [,1] [,2]
## [1,] "24" "Buenos Aires"
## [2,] "24" "Buenos Aires"
## [3,] "24" "Salta"
## [4,] "29" "San Miguel de Tucumán"
## [5,] "29" "San Miguel de Tucumán"
## [6,] "29" "Buenos Aires"
## [7,] "29" "Buenos Aires"
## [8,] "30" "Buenos Aires"
## [9,] "30" "Buenos Aires"
## [10,] "30" "Neuquén"
## [11,] "31" "Salta"
## [12,] "31" "Salta"
## [13,] "31" "Salta"
## [14,] "35" "Buenos Aires"
## [15,] "35" "Buenos Aires"
## [16,] "35" "Buenos Aires"
## [17,] "35" "Buenos Aires"
## [18,] "35" "Neuquén"
## [19,] "37" "Buenos Aires"
## [20,] "36" "Buenos Aires"