First we’ll test out HTTR on the NYT Archive API
# Input the key supplied by the NYT API service
key.arch <- "dc85bc8c234047b6a4c349b2f4ba7696"
# Use January, 1900
yr.arch <- 1900
mn.arch <- 1
# Assemble the API call - hashed verbose() for easy running
api.arch <- paste0("http://api.nytimes.com/svc/archive/v1/", yr.arch, "/", mn.arch, ".json?api-key=", key.arch)
nyt.arch.test1 <- GET(api.arch)
# Confirm the status of the extraction operation
http_status(nyt.arch.test1)## $category
## [1] "Success"
##
## $reason
## [1] "OK"
##
## $message
## [1] "Success: (200) OK"
# warn_for_status(r$status_code)
# Access headers from the data extrqacted in the test - hashed for easy running
# headers(nyt.arch.test1)
# Access the content parsed - hashed for easy running.
# content(nyt.arch.test1, "parse")Now we’ll try extracting data from the Times Newsire API, which allows more fine-tuning of the call.
# For the Newsire API, URI structure depends on whether call is for a specific news item or a set of recent items.
# Specific new item: http://api.nytimes.com/svc/news/{version}/content[.response-format]?url={item-url}&api-key={your-API-key}
# Set of recent news items: http://api.nytimes.com/svc/news/{version}/content/{source}/{section}[/time-period][.response-format]?api-key={your-API-key}
# We'll use the latter for this exercise.
# Input the {key} supplied by the NYT API service
key.wire <- "dc85bc8c234047b6a4c349b2f4ba7696"
# API call syntax includes {version}, which we'll default to v3
vers.wire <- "v3"
# Also enables specification of {source} - iht, nyt, all
src.wire <- "all"
# Also enables specification of {section} - arts, business, world, u.s., sports, etc.
sect.wire <- "arts"
# Specify time by indicating cutoff for how recently published (in hours)
time.wire <- "all"
# json or jsonp
rf.wire <- ".json"
# Also enables specification of number of news items
items.wire <- "10"
# Build the Times Newsire API
api.wire <- paste0("http://api.nytimes.com/svc/news/", vers.wire, "/content/", src.wire, "/", sect.wire, "/", time.wire, rf.wire, "?limit=", items.wire, "&api-key=", key.wire)# Extract from JSON
tech <- fromJSON(api.wire)
# Convert to a data frame, extracting paramaters to summarize
tech.df <- data.frame(
article <- tech$results$title,
date <- str_extract(tech$results$first_published_date, ".+(?=T)"), # Pull out the year, month, and date prior to the "T" marker
time <- str_extract(tech$results$first_published_date, "(?<=T).+"), # Pull ou the time and time zone following the "T" marker
abstract <- paste0((str_extract(tech$results$abstract, "^.{100}")), "..."), # Pull out the first 100 characters of the abstract and paste on an ellipsis
url <- tech$results$url,
byline <- tech$results$byline
)
# Label the columns of the data frame for legibility
colnames(tech.df) <- c("article", "date", "time", "abstract", "url", "byline")
# Visualize the result
kable(tech.df, "html")| article | date | time | abstract | url | byline |
|---|---|---|---|---|---|
| Live Nation Rules Music Ticketing, Some Say With Threats | 2018-04-01 | 10:00:05-04:00 | Eight years after the Live Nation and Ticketmaster merger, the giant companys power has drawn compl… | https://www.nytimes.com/2018/04/01/arts/music/live-nation-ticketmaster.html | By BEN SISARIO and GRAHAM BOWLEY |
| Whats on TV Sunday: ‘Jesus Christ Superstar’ and ‘The Child in Time’ | 2018-04-01 | 05:00:04-04:00 | John Legend steps into the sandals of Jesus. And Benedict Cumberbatch stars in a new movie about gri… | https://www.nytimes.com/2018/04/01/arts/television/whats-on-tv-sunday-jesus-christ-superstar-and-the-child-in-time.html | By ANDREW R. CHOW |
| 11 of Our Best Weekend Reads | 2018-03-31 | 07:54:07-04:00 | Roseanne is back, and shes a Trump supporter. A Parkland shooting victim hits out at #WalkUpNotOut…. | https://www.nytimes.com/2018/03/31/arts/11-of-our-best-weekend-reads.html | By KALY SOTO |
| Whats on TV Saturday: ‘Lover for a Day’ and ‘Operation Odessa’ | 2018-03-31 | 03:00:04-04:00 | A young woman seeks comfort after a breakup in Mubis “Lover for a Day.” And the true crime document… | https://www.nytimes.com/2018/03/31/arts/television/whats-on-tv-saturday-lover-for-a-day-and-operation-odessa.html | By SARA ARIDI |
| Abreus Legacy: The Best Classical Moments of the Week on YouTube | 2018-03-30 | 15:22:20-04:00 | NA… | https://www.nytimes.com/2018/03/30/arts/music/youtube-moments-classical.html | NA |
| Rem Koolhaas Firm Reveals Design for Los Angeles Temple Expansion | 2018-03-30 | 14:34:04-04:00 | NA… | https://www.nytimes.com/2018/03/30/arts/design/rem-koolhaas-firm-wilshire-boulevard-temple-expansion.html | By SARA ARIDI |
| The Playlist: A Vintage Surprise From the Weeknd, and 9 More New Songs | 2018-03-30 | 14:33:37-04:00 | The tracks that caught our critics attention this week, from Cardi B, Stephen Malkmus & the Jicks, … | https://www.nytimes.com/2018/03/30/arts/music/playlist-weeknd-cardi-b-stephen-malkmus.html | By JON PARELES, JON CARAMANICA and GIOVANNI RUSSONELLO |
| Review: An Ecstatic ‘Rosenkavalier’ Introduces a Conductor to Carnegie | 2018-03-30 | 13:29:20-04:00 | Kirill Petrenko, who will lead the Berlin Philharmonic, led his Bavarian State Orchestra in a sparkl… | https://www.nytimes.com/2018/03/30/arts/music/review-bavarian-orchestra-kirill-petrenko-rosenkavalier.html | By ANTHONY TOMMASINI |
| March in Live Jazz: 5 Standout Shows | 2018-03-30 | 13:28:37-04:00 | Great performances, including the sons of John Coltrane and Pharoah Sanders and a night with the DIV… | https://www.nytimes.com/2018/03/30/arts/music/best-jazz-ravi-coltrane-miles-okazaki-omer-avital.html | By GIOVANNI RUSSONELLO |
| Review: A Pulitzer Sequel for Orchestra, Packed With Drama in Microcosm | 2018-03-30 | 13:03:43-04:00 | Nature-immersed, John Luther Adamss “Become Desert” follows his “Become Ocean,” which won the Pulit… | https://www.nytimes.com/2018/03/30/arts/music/review-john-luther-adams-become-desert-seattle-symphony.html | By SETH COLTER WALLS |