Web APIs

Christina Taylor

October 30, 2015

NYT Most Popular API

# Structure:
# http://api.nytimes.com/svc/mostpopular/{version}/{resource-type}/{section}[/share-types]/{time-period}[.response-format]?api-key={your-API-key}
library(jsonlite)
key <- "?api-key=e7c51f5bcb9350abf1952726633dbf44:1:73345229"
# Search for the past week's most shared (by Email) articles in all sections
url <- "http://api.nytimes.com/svc/mostpopular/v2/mostshared/all-sections/email/7"
req <- fromJSON(paste0(url, key))
# Choose relevant elements from returned list
v <- cbind(req$results$title, req$results$section, req$results$byline, req$results$url)
# Trim white spaces
trim <- function(x) gsub("^\\s+|\\s+$|,", "", x)
df <- data.frame(apply(v, c(1, 2), trim), stringsAsFactors = FALSE)
# Name columns
names(df) <- c("title", "section", "author", "link")
# The final result
library(knitr)
kable(df, right = FALSE)
title section author link
Greenland Is Melting Away World By CORAL DAVENPORT JOSH HANER LARRY BUCHANAN and DEREK WATKINS http://www.nytimes.com/interactive/2015/10/27/world/greenland-is-melting-away.html
Does Exercise Slow the Aging Process? Health By GRETCHEN REYNOLDS http://well.blogs.nytimes.com/2015/10/28/does-exercise-slow-the-aging-process/
Lady Gaga and the Life of Passion Opinion By DAVID BROOKS http://www.nytimes.com/2015/10/23/opinion/lady-gaga-and-the-life-of-passion.html
Can You Get Smarter? Opinion By RICHARD A. FRIEDMAN http://www.nytimes.com/2015/10/25/opinion/sunday/can-you-get-smarter.html
My Dark California Dream Opinion By DANIEL DUANE http://www.nytimes.com/2015/10/25/opinion/sunday/my-dark-california-dream.html
Everything You Need to Know About the New SAT Education By ERIC HOOVER http://www.nytimes.com/2015/11/01/education/edlife/everything-you-need-to-know-about-the-new-sat.html
The Disproportionate Risks of Driving While Black U.S. By SHARON LaFRANIERE and ANDREW W. LEHREN http://www.nytimes.com/2015/10/25/us/racial-disparity-traffic-stops-driving-black.html
Costs for Dementia Care Far Exceeding Other Diseases Study Finds Health By GINA KOLATA http://www.nytimes.com/2015/10/27/health/costs-for-dementia-care-far-exceeding-other-diseases-study-finds.html
36 Hours in San Francisco Travel By FRITZIE ANDRADE MAX CANTOR CHRIS CARMICHAEL WILL LLOYD and LOUIE ALFARO http://www.nytimes.com/video/travel/100000003980684/36-hours-in-san-francisco.html
Reinventing the Library Opinion By ALBERTO MANGUEL http://www.nytimes.com/2015/10/24/opinion/reinventing-the-library.html
The New Apple TV Invigorates the Set-Top Box Technology By BRIAN X. CHEN http://www.nytimes.com/2015/10/29/technology/personaltech/the-new-apple-tv-invigorates-the-set-top-box.html
Keynes Comes to Canada Opinion By PAUL KRUGMAN http://www.nytimes.com/2015/10/23/opinion/keynes-comes-to-canada.html
The Ambivalent Marriage Takes a Toll on Health Health By TARA PARKER-POPE http://well.blogs.nytimes.com/2015/10/26/the-ambivalent-marriage-takes-a-toll-on-health/
Oh Those Debating Republicans Opinion By GAIL COLLINS http://www.nytimes.com/2015/10/29/opinion/oh-those-debating-republicans.html
Grave of ‘Griffin Warrior’ at Pylos Could Be a Gateway to Civilizations Science By NICHOLAS WADE http://www.nytimes.com/2015/10/27/science/a-warriors-grave-at-pylos-greece-could-be-a-gateway-to-civilizations.html
The New York Times Best Illustrated Children’s Books of 2015 Books By THE NEW YORK TIMES http://www.nytimes.com/interactive/2015/10/28/books/review/28-new-york-times-best-illustrated-childrens-books-of-2015.html
Meat Is Linked to Higher Cancer Risk W.H.O. Report Finds Health By ANAHAD O’CONNOR http://www.nytimes.com/2015/10/27/health/report-links-some-types-of-cancer-with-processed-or-red-meat.html
Jimmy Carter: A Five-Nation Plan to End the Syrian Crisis Opinion By JIMMY CARTER http://www.nytimes.com/2015/10/26/opinion/jimmy-carter-a-five-nation-plan-to-end-the-syrian-crisis.html
Cutting Sugar Improves Children’s Health in Just 10 Days Health By ANAHAD O’CONNOR http://well.blogs.nytimes.com/2015/10/27/cutting-sugar-improves-childrens-health-in-just-10-days/
How 4 Federal Lawyers Paved the Way to Kill Osama bin Laden U.S. By CHARLIE SAVAGE http://www.nytimes.com/2015/10/29/us/politics/obama-legal-authorization-osama-bin-laden-raid.html