-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_queries.Rmd
89 lines (67 loc) · 2.6 KB
/
api_queries.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
---
title: "Homework10 Make API queries"
author: "Minzhi Liao"
date: "December 6, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Set up
Again, we need `dplyr` and `tibble` to convert data to a `data.frame`. We are using [`RCurl`](http://www.rdocumentation.org/packages/RCurl) this time for html request. We also need `jsonlite` to deal with json.
```{r}
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(RCurl))
suppressPackageStartupMessages(library(jsonlite))
```
Then we need API key here. I read it from a local file. If you need to run this code, you should type in your API key from yelp which can be applied [here](https://www.yelp.com/developers/v3/manage_app).
```{r}
yelp_api_key <- readLines(file("yelp_key.private"))
github_api_key <- readLines(file("github_key.private"))
```
## Scraping data by API queries
We can do the exact same thing as the previous section, searching for ramen near UBC, but in a more elegent way.
```{r}
url <- "https://api.yelp.com/v3/businesses/search"
header = c(
"Authorization" = paste("Bearer", yelp_api_key)
)
params = c(
"term" = "ramen",
"location" = "UBC, Vancouver, BC"
)
yelp_ramen_new <- fromJSON(getForm(url,
.opts = list(httpheader = header),
.params = params)
) %>%
as.data.frame() %>%
select(
name = businesses.name,
is_closed = businesses.is_closed,
reviews = businesses.review_count,
rating = businesses.rating,
price = businesses.price,
phone = businesses.phone,
distance = businesses.distance,
longi = region.center.longitude,
lati = region.center.latitude
)
```
Now we can see the result. In this way it is much more easier to get the infomation, and there are more things to get like number of reviews, closed or not, or the price and phone number. But there are also some disadvantages, like the APIs are mostly used in apps, so it contains coordinates instead of address, which is not friendly for human.
```{r}
knitr::kable(head(yelp_ramen_new, 10))
```
And we can save the new data to the disk.
```{r}
write.csv(yelp_ramen_new, "yelp_ramen_new.csv")
```
## Try more than `GET` with github API
We can try adding a issue by `POST`. It is easier to use `httr` here.
```{r}
library(httr)
url <- "https://api.github.com/repos/STAT545-UBC-students/hw10-liao02x/issues"
POST(url,
add_headers("Accept" = "application/vnd.github.squirrel-girl-preview"),
add_headers("Authorization" = paste("token", github_api_key)),
body = "{\"title\":\"R API test\",\"body\":\"This issue is for R API test for github.\"}"
)