library(httr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(stringi)
library(stringr)
library(jsonlite)

Assignment – Web APIs

The New York Times web site provides a rich set of APIs, as described here: http://developer.nytimes.com/docs

chose one of the New York Times APIs, constructed an interface in R to read in the JSON data, and transform it to an R dataframe.

key<-"rtBzASO4UaGeYcOP5i1wjPGxAzIzH06M"


# just to test if we are getting the  response back from the api
response1<-GET("https://api.nytimes.com/svc/archive/v1/2021/2.json", query = list(api_key = key, order = "by-title", offset = 20))
response1[2]
## $status_code
## [1] 401
getArchive<-function(year, month, rows) {
  stopifnot(!any(is.na(c(year, month))))
  stopifnot(year >= 2000)
  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.main) %>%
    mutate(Headline = stri_trans_totitle(headline.main))
  
  head(resp, rows)
}

getArchive(2018, 3, 10)
##                                                                  headline.main
## 1                Britain Presses U.S. to Avoid Death Penalty for ISIS Suspects
## 2                         What to Watch if You Loved the Best-Picture Nominees
## 3                        Walmart to Raise Age to Buy Guns and Ammunition to 21
## 4                       Is Bitcoin a Waste of Electricity, or Something Worse?
## 5                        Raymond Danowski, Stockpiler of Poetry, Is Dead at 74
## 6                   Sean Lavery, Ballet Star in a Shortened Career, Dies at 61
## 7  U.S. Banks on Diplomacy With North Korea, but Moves Ahead on Military Plans
## 8              As Elections Near, Egypt Finds a New Target: Foreign News Media
## 9                      Exxon Mobil Scraps a Russian Deal, Stymied by Sanctions
## 10                              How China’s Media Sold Xi Jinping’s Power Grab
##                                                                       Headline
## 1                Britain Presses U.s. To Avoid Death Penalty For Isis Suspects
## 2                         What To Watch If You Loved The Best-Picture Nominees
## 3                        Walmart To Raise Age To Buy Guns And Ammunition To 21
## 4                       Is Bitcoin A Waste Of Electricity, Or Something Worse?
## 5                        Raymond Danowski, Stockpiler Of Poetry, Is Dead At 74
## 6                   Sean Lavery, Ballet Star In A Shortened Career, Dies At 61
## 7  U.s. Banks On Diplomacy With North Korea, But Moves Ahead On Military Plans
## 8              As Elections Near, Egypt Finds A New Target: Foreign News Media
## 9                      Exxon Mobil Scraps A Russian Deal, Stymied By Sanctions
## 10                              How China’s Media Sold Xi Jinping’s Power Grab