-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
114 lines (91 loc) · 3.01 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
---
title: "R Flights"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
[![CRAN status](https://www.r-pkg.org/badges/version/rflights)](https://CRAN.R-project.org/package=rflights)
[![CRAN logs](https://cranlogs.r-pkg.org/badges/rflights)](https://cran.r-project.org/package=rflights)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://www.tidyverse.org/lifecycle/#stable)
[![Travis build status](https://travis-ci.org/jcrodriguez1989/rflights.svg?branch=master)](https://travis-ci.org/jcrodriguez1989/rflights)
[![Coverage status](https://codecov.io/gh/jcrodriguez1989/rflights/branch/master/graph/badge.svg)](https://codecov.io/github/jcrodriguez1989/rflights?branch=master)
Query plane tickets, from several airlines, using the Kiwi API (similar to Google Flights).
The API is documented at [https://docs.kiwi.com/](https://docs.kiwi.com/).
## Installation
You can install the `rflights` package from CRAN:
```{r eval = FALSE}
install.packages("rflights")
```
Or get the latest version from GitHub:
```{r eval = FALSE}
if (!require("remotes")) {
install.packages("remotes")
}
remotes::install_github("jcrodriguez1989/rflights")
```
## Usage
### Get city or country IDs
```{r, eval = TRUE}
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
arg_id <- arg_id[[1]]
names(arg_id)
arg_id$id
arg_id$continent
arg_id <- arg_id$id
tl_id <- find_location("toulouse")
length(tl_id)
lapply(tl_id, function(x) x$type)
# we are looking for the city
tl_id <- tl_id[[which(sapply(tl_id, function(x) x$type == "city"))]]
tl_id$country
tl_id <- tl_id$id
tl_id
```
### Get flight prices
```{r, eval = FALSE}
# get flights from Argentina to toulouse around 01 July to 09 July
# Maybe I can go to the user2019??
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)
```
## Examples
### Create flight price alarms
I used it to alert through a [Pushbullet](https://www.pushbullet.com/) message.
```{r, eval = FALSE}
my_savings <- 25 # yup, just 25USD
found_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)
}
}
```
### Fly to anywhere
Find plane tickets from my city to anywhere, from today to 2 next weeks.
```{r, eval = TRUE}
# I am a freelancer, let's go anywhere!
flights <- get_flights(
fly_from = "GNV",
date_from = Sys.Date(), date_to = Sys.Date() + 2 * 7
)
length(flights)
head(t(sapply(flights, function(x) c(x$price, x$cityTo))), n = 20)
```