suppressMessages(suppressWarnings(library('tidyverse')))
suppressMessages(suppressWarnings(library('httr')))
suppressMessages(suppressWarnings(library('jsonlite')))

Accessing NYT API

I put my key into a line of code that is not included on the printout.

I selected the API for the most veiwed Science articles from the last day, you can see that refelected in the url: “/mostviewed/Science/1.json” as the ‘1’ before the josn file extension denotes number of days. NYT gave the option of 1, 7, or 30. I chose 1 because the emphasis was extracting the data from the API and putting the data in the json into a dataframe, not neccessarily a large data frame.

nyt_url <- 'https://api.nytimes.com/svc/mostpopular/v2/mostviewed/Science/1.json' # the url following the NYT's instructions.
nyt_mp <- nyt_url %>% GET(add_headers('api-key'=key)) %>% content(as='text') %>% fromJSON(simplifyDataFrame = TRUE)# The key is in a hidden line of code.
# reading as text here turned out to be the easiest route since fromJSON() will automatically put the json file 
#into a dataframe when 'simplifyDataFrame = TRUE'.
nyt_mp_df <- nyt_mp$results
head(nyt_mp_df)
##                                                                               url
## 1    https://www.nytimes.com/2017/10/26/science/dinosaur-raccoon-bandit-mask.html
## 2 https://www.nytimes.com/2017/10/19/science/saudi-arabia-gates-google-earth.html
## 3        https://www.nytimes.com/2017/09/29/science/rosetta-comet-lost-photo.html
## 4            https://www.nytimes.com/2017/10/25/science/skull-tsunami-victim.html
## 5         https://www.nytimes.com/2017/10/25/science/oysters-noise-pollution.html
## 6               https://www.nytimes.com/2017/10/24/science/beets-red-enzymes.html
##                                                                                                                                                                  adx_keywords
## 1                                                                                                                   Dinosaurs;Current Biology (Journal);Paleontology;Feathers
## 2                                                Google Inc;Saudi Arabia;Archaeology and Anthropology;Satellites;Kennedy, David L;Arabian Archaeology and Epigraphy (Journal)
## 3                                                                                                                             Space and Astronomy;Comets;Rosetta (Spacecraft)
## 4 Archaeology and Anthropology;Tidal Waves and Tsunamis;Geology;Papua New Guinea;PLoS One (Journal);Skull (Body Part);Goff, James;Terrell, John Edward;Cochrane, Ethan Edward
## 5                                                                                                    Oysters;Noise;Oceans and Seas;PLoS One (Journal);Massabuau, Jean-Charles
## 6                                                                           Beets;Biology and Biochemistry;Color;Evolution (Biology);New Phytologist (Journal);Maeda, Hiroshi
##       column section                 byline    type
## 1 Trilobites Science  By NICHOLAS ST. FLEUR Article
## 2            Science  By NICHOLAS ST. FLEUR Article
## 3 Trilobites Science  By NICHOLAS ST. FLEUR Article
## 4 Trilobites Science  By NICHOLAS ST. FLEUR Article
## 5 Trilobites Science     By DOUGLAS QUENQUA Article
## 6 Trilobites Science By VERONIQUE GREENWOOD Article
##                                                                 title
## 1             A Turkey-Sized Dinosaur With a Raccoon-Like Bandit Mask
## 2 Hundreds of Mysterious Stone <U+0091>Gates<U+0092> Found in Saudi Arabia<U+0092>s Desert
## 3        Rosetta<U+0092>s Lost Picture From Moments Before It Struck a Comet
## 4        Ancient Skull May Be History<U+0092>s Earliest Known Tsunami Victim
## 5           Yes, Oysters Can <U+0091>Hear.<U+0092> They Probably Wish We<U+0092>d Clam Up.
## 6                                           How Beets Became Beet-Red
##                                                                                                                                                                abstract
## 1                       By analyzing Sinosauropteryx specimens found in China, paleontologists found that certain camouflage patterns have a deep evolutionary history.
## 2                          With Google Earth, archaeologists have uncovered strange stone structures that may have been built by nomadic tribes in ancient lava fields.
## 3                                            One year after the spacecraft dived into Comet 67P/Churyumov-Gerasimenko, it gave scientists a gift from beyond the grave.
## 4 The 6,000-year-old Aitape skull, found in Papua New Guinea in 1929, was excavated from sediments that contain telltale signs of ocean water left behind by a tsunami.
## 5           Researchers found shellfish in a tank closed their shells when they heard frequencies similar to noises made by cargo ships and underwater oil exploration.
## 6           Confronted with a surplus of an amino acid found in many plants, beets evolved the ability to transform it into the scarlet hue found on your dinner table.
##   published_date             source    id asset_id views
## 1     2017-10-26 The New York Times 1e+14    1e+14     1
## 2     2017-10-19 The New York Times 1e+14    1e+14     2
## 3     2017-09-29 The New York Times 1e+14    1e+14     3
## 4     2017-10-25 The New York Times 1e+14    1e+14     4
## 5     2017-10-25 The New York Times 1e+14    1e+14     5
## 6     2017-10-24 The New York Times 1e+14    1e+14     6
##                                                     des_facet
## 1                           DINOSAURS, PALEONTOLOGY, FEATHERS
## 2                    ARCHAEOLOGY AND ANTHROPOLOGY, SATELLITES
## 3           SPACE AND ASTRONOMY, COMETS, ROSETTA (SPACECRAFT)
## 4                           TIDAL WAVES AND TSUNAMIS, GEOLOGY
## 5                             OYSTERS, NOISE, OCEANS AND SEAS
## 6 BEETS, BIOLOGY AND BIOCHEMISTRY, COLOR, EVOLUTION (BIOLOGY)
##                                                             org_facet
## 1                                           CURRENT BIOLOGY (JOURNAL)
## 2             GOOGLE INC, ARABIAN ARCHAEOLOGY AND EPIGRAPHY (JOURNAL)
## 3                                                                    
## 4 ARCHAEOLOGY AND ANTHROPOLOGY, PLOS ONE (JOURNAL), SKULL (BODY PART)
## 5                                                  PLOS ONE (JOURNAL)
## 6                                           NEW PHYTOLOGIST (JOURNAL)
##                                                   per_facet
## 1                                                          
## 2                                          KENNEDY, DAVID L
## 3                                                          
## 4 GOFF, JAMES, TERRELL, JOHN EDWARD, COCHRANE, ETHAN EDWARD
## 5                                   MASSABUAU, JEAN-CHARLES
## 6                                            MAEDA, HIROSHI
##          geo_facet
## 1                 
## 2     SAUDI ARABIA
## 3                 
## 4 PAPUA NEW GUINEA
## 5                 
## 6                 
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         media
## 1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
## 2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
## 3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
## 4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
## 5 image, photo, Not being able to detect natural events, like rainfall or thunderstorms, could prevent oysters from knowing when it<U+0092>s time to spawn., Steven Siewert/Fairfax Media, via Getty Images, 1, https://static01.nyt.com/images/2017/10/26/science/26TB-OYSTERS/26TB-OYSTERS-thumbStandard.jpg, https://static01.nyt.com/images/2017/10/26/science/26TB-OYSTERS/26TB-OYSTERS-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2017/10/26/science/26TB-OYSTERS/26TB-OYSTERS-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 6                                                                   image, photo, Beets evolved a different way of turning red than other red things in the plant kingdom., Karsten Moran for The New York Times, 1, https://static01.nyt.com/images/2017/10/25/science/25TB-BEETS/25TB-BEETS-thumbStandard.jpg, https://static01.nyt.com/images/2017/10/25/science/25TB-BEETS/25TB-BEETS-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2017/10/25/science/25TB-BEETS/25TB-BEETS-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440