Assignment – Web APIs
The New York Times web site provides a rich set of APIs, as described here: http://developer.nytimes.com/docs
You’ll need to start by signing up for an API key.
response<-GET("https://api.nytimes.com/svc/archive/v1/2018/3.json", query = list(api_key = key, order = "by-title", offset = 20))
response[2]
## $status_code
## [1] 200
getArchive<-function(year, month, rows) {
stopifnot(!any(is.na(c(year, month))))
stopifnot(year >= 1851)
stopifnot(month >= 1 & month <= 12)
stopifnot(rows >= 1)
url<-sprintf("https://api.nytimes.com/svc/archive/v1/%d/%d.json?api-key=%s", year, month, key)
response_df<-fromJSON(url,flatten = TRUE)$response$docs
resp = response_df %>%
select(Headline = headline.main) %>%
mutate(Headline = stri_trans_totitle(Headline))
stopifnot(is.data.frame(resp))
head(resp, rows)
}
getArchive(2015, 4, 24)
## Headline
## 1 Backlash Against Indiana Religious Exceptions
## 2 Clip: ‘Felix & Meira’
## 3 ‘Broadchurch’ Season 2 Episode 5 Recap: The Center Collapses
## 4 ‘The Americans’ Recap: The Moment Finally Arrives
## 5 Denim Jackets That Are Far From Old-Fashioned And More
## 6 What Is Morgellons? Singer Joni Mitchell’S Disputed Diagnosis
## 7 Another Arrangement
## 8 Love And Loss On The Road To Arkansas
## 9 Jeb Bush Seems To Shift Tone In His Praise Of Indiana Law
## 10 The Indictment Of Senator Robert Menendez
## 11 Pictures Of The Day: Iraq And Elsewhere
## 12 Filing On Hsbc Compliance
## 13 Verbatim: Menendez Repeats, ‘I’M Not Going Anywhere’
## 14 Cruz, In Iowa, Commends Indiana For ‘Doing The Right Thing’
## 15 Review: In ‘Furious 7,’ A Franchise Continues To Roar
## 16 California Governor Imposes Water Order
## 17 In A Nod To Past And Future, Clinton Pushes Child Development In Brooklyn
## 18 Liquidity Traps, Local And Global (Somewhat Wonkish)
## 19 Officials On Iran Nuclear Negotiations
## 20 Your Evening Briefing
## 21 A Friendly Pillow Fight, Worldwide
## 22 ‘Scrapbooking’ Our Children Into Facebook
## 23 A New Exhibition Celebrates Le Corbusier, Through The Eyes Of Those He Influenced
## 24 Verbatim: Booker Stands With Menendez