/
rest-kiva.r
198 lines (156 loc) · 5.39 KB
/
rest-kiva.r
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
######################################################################
## Filename: rest-kiva.r
## Description:
## Author: Helge Liebert
## Created: Do Jan 4 19:13:53 2018
## Last-Updated: Mi Jan 9 19:15:12 2019
######################################################################
## Libraries
library(jsonlite)
library(httr)
library(rvest)
## Kiva Examples
## Get the 20 most recent loans
newloans <- fromJSON("https://api.kivaws.org/v1/loans/newest.json")
## All lenders for a particular loan id
loans <- fromJSON("http://api.kivaws.org/v1/loans/38239/lenders.json")
str(loans)
toJSON(loans, pretty = TRUE)
## Simplify structure
loans <- fromJSON("http://api.kivaws.org/v1/loans/38239/lenders.json",
flatten = TRUE)
str(loans)
toJSON(loans, pretty = TRUE)
loans <- as.data.frame(loans)
str(loans)
toJSON(loans, pretty = TRUE)
## List of all API methods
methods <- fromJSON("https://api.kivaws.org/v1/methods.json", flatten = TRUE)
str(methods)
methods <- as.data.frame(methods$methods)
methods[grep("search", methods$id), ]
## Request specific info from KIVA API
## Examples
## https://build.kiva.org/api
## https://build.kiva.org/docs/getting_started
## https://api.kivaws.org/v1/loans/search.html?sector=Agriculture
## https://api.kivaws.org/v1/loans/search.json?sector=Agriculture&country=VN
## Parameters
baseurl <- "https://api.kivaws.org/v1/"
method <- "loans/search.json?"
## method <- "loans/search.xml?"
## method <- "loans/search.html?"
country <- "VN,KH"
sector <- "Agriculture"
type <- "individuals"
status <- "funded"
sortby <- "newest"
## Construct URL
query <- paste0("country_code=", country, "&",
"sector=", sector, "&",
"borrower_type=", type, "&",
"status=", status, "&",
"sort_by=", sortby)
uri <- paste0(baseurl, method, query )
uri
## Send HTTP GET request, handle response content, library(httr)
response <- GET(uri)
response
if (response$status_code == 200) {
jsontable <- content(response, as = "text")
} else {
stop("HTTP response not OK!")
}
jsontable
## Parse json data
data <- fromJSON(jsontable, flatten = TRUE)
str(data)
names(data)
data$paging
data <- data$loans
head(data)
dim(data)
## Even more simple, pass URI directly
data <- fromJSON(uri, flatten = TRUE)
data <- data$loans
str(data)
head(data)
dim(data)
## Nested elements need to be flattened
## data$tags <- sapply(data$tags, function(x) paste(unlist(x), collapse = ", "))
## data$themes <- sapply(data$themes, function(x) paste(unlist(x), collapse = ", "))
## data$description.languages <- sapply(data$description.languages, function(x) paste(unlist(x), collapse = ", "))
## str(data)
## Get all data, multiple requests, iterate over pages
## Note: very simple proof of concept
## (should check http response for error and have better tests)
## (more efficient to large queries to file immediately)
## Parameters
baseurl <- "https://api.kivaws.org/v1/"
method <- "loans/search.json?"
country <- "VN"
sector <- "Agriculture"
type <- "individuals"
status <- "funded"
sortby <- "oldest" # (o/w duplicates may occur when new entries are added)
pagelength <- 20 # max page length allowed is 500
## Construct URL
query <- paste0("country_code=", country, "&",
"sector=", sector, "&",
"borrower_type=", type, "&",
"status=", status, "&",
## "per_page=", pagelength, "&"
"sort_by=", sortby)
uri <- paste0(baseurl, method, query)
## Get maxpagenumber and other information for iteration
response <- fromJSON(uri, flatten = TRUE)
response$paging
maxpages <- response$paging$pages
records <- response$paging$total
columns <- ncol(response$loans)
## Open csv, write header
header <- names(response$loans)
write.table(t(header), file = "Data/kiva.csv", sep = ";",
col.names = FALSE, row.names = FALSE)
# Or collect in data frame (don't do this for large jobs)
## data <- data.frame(matrix(nrow = 0, ncol = columns))
## names(data) <- header
## Simple helper function to flatten columns
unnest <- function(col) paste(unlist(col), collapse = ", ")
## Iterate over pages, limit to first three
for (p in seq(1, maxpages, by = 1)[1:3]) {
## Info
print(paste0(p, "/", maxpages))
## Append page to uri
pquery <- paste0(uri, "&page=", p)
## Get data, assert completeness
loans <- fromJSON(pquery, flatten = TRUE)$loans
stopifnot(nrow(loans) == pagelength)
stopifnot(ncol(loans) == columns)
## Fix nested list columns ... or just use data.table::fwrite()
## str(loans)
loans$tags <- sapply(loans$tags, unnest)
## loans$themes <- sapply(loans$themes, unnest) # missing for older records
loans$description.languages <- sapply(loans$description.languages, unnest)
## str(loans)
## Collect loans in data frame
## data <- rbind(data, loans)
## Append to file
write.table(loans, "Data/kiva.csv", sep = ";", append = TRUE,
col.names = FALSE, row.names = FALSE)
}
## head(data)
## dim(data)
## TheyWorkForYou.com Example
apikey <- "G3WVqtBtKAbdGVqrd8BKajm8"
base <- "https://www.theyworkforyou.com/api/"
format <- "js"
func <- "getMPs?"
query <- paste0("&", "key=", apikey, "&", "output=", format)
uri <- paste0(base, func, query)
uri
## listofmps <- fromJSON(uri) # problem with encoding, maybe xml is better
response <- GET(uri)
response <- content(response, as = "raw")
listofmps <- fromJSON(rawToChar(response))
str(listofmps)