suppressMessages(suppressWarnings(library('tidyverse')))
suppressMessages(suppressWarnings(library('httr')))
suppressMessages(suppressWarnings(library('jsonlite')))
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 reflected 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/2018/10/26/science/mars-cloud-volcano-eruption-arsia-mons.html
## 2 https://www.nytimes.com/2018/10/23/science/lavender-scent-anxiety.html
## 3 https://www.nytimes.com/2018/10/25/science/fish-evolution-shallow-coasts.html
## 4 https://www.nytimes.com/2018/10/24/science/human-bodies-cold-weather-adjustment.html
## 5 https://www.nytimes.com/2018/10/25/science/six-tiger-subspecies.html
## 6 https://www.nytimes.com/2018/10/23/science/stephen-hawking-final-paper.html
## adx_keywords
## 1 Space and Astronomy;Mars (Planet);Volcanoes;Weather
## 2 Lavender;Smells and Odors;Anxiety and Stress;Research;Brain;Frontiers in Behavioral Neuroscience (Journal);Herbs;Kashiwadani, Hideki;your-feed-selfcare
## 3 Evolution (Biology);Animals;Fish and Other Marine Life;Oceans and Seas;Paleontology;Fossils;Science (Journal);Sallan, Lauren
## 4 Temperature;Winter (Season);Seasons and Months;Skin
## 5 Tigers;Genetics and Heredity;Current Biology (Journal);Far East, South and Southeast Asia and Pacific Areas;Luo, Shu-Jin;Endangered and Extinct Species;Conservation of Resources
## 6 Hawking, Stephen W;Black Holes (Space);Physics;Mathematics;Einstein, Albert;Space and Astronomy
## column section byline type
## 1 Trilobites Science By KENNETH CHANG Article
## 2 Trilobites Science By JOANNA KLEIN Article
## 3 Trilobites Science By NICHOLAS ST. FLEUR Article
## 4 <NA> Science By NIRAJ CHOKSHI Article
## 5 Science By RACHEL NUWER Article
## 6 Out There Science By DENNIS OVERBYE Article
## title
## 1 A Volcanic Eruption on Mars? Nope.
## 2 Lavenders Soothing Scent Could Be More Than Just Folk Medicine
## 3 Where Did Fish First Evolve? The Answer May Be Shallow
## 4 Heres Why a 50-Degree Day Feels Colder in Fall Than in Spring
## 5 Divide and Preserve: Reclassifying Tigers to Help Save Them From Extinction
## 6 Stephen Hawkings Final Paper: How to Escape From a Black Hole
## abstract
## 1 Its just a cloud. A very long cloud.
## 2 In mice, researchers found that some components of lavender odor had effects on anxiety similar to taking Valium.
## 3 They lacked jaws. Some had armor and spikes. And they were some of our earliest ancestors with spines in the shallow coasts around supercontinents.
## 4 Why might nippy temperatures now feel much more comfortable in just a few months? The body takes time to adapt to the cold.
## 5 Are there many subspecies of tiger, or only two? A correct accounting is the only way to preserve what is left of the animals genetic diversity, some scientists say.
## 6 In a study from beyond the grave, the theoretical physicist sings (mathematically) of memory, loss and the possibility of data redemption.
## published_date source id asset_id views
## 1 2018-10-26 The New York Times 1e+14 1e+14 1
## 2 2018-10-23 The New York Times 1e+14 1e+14 2
## 3 2018-10-25 The New York Times 1e+14 1e+14 3
## 4 2018-10-24 The New York Times 1e+14 1e+14 4
## 5 2018-10-25 The New York Times 1e+14 1e+14 5
## 6 2018-10-23 The New York Times 1e+14 1e+14 6
## des_facet
## 1 SPACE AND ASTRONOMY, MARS (PLANET), VOLCANOES, WEATHER
## 2 SMELLS AND ODORS, ANXIETY AND STRESS, RESEARCH, BRAIN, HERBS, YOUR-FEED-SELFCARE
## 3 EVOLUTION (BIOLOGY), ANIMALS, FISH AND OTHER MARINE LIFE, OCEANS AND SEAS
## 4 WINTER (SEASON), SEASONS AND MONTHS, SKIN
## 5 TIGERS, GENETICS AND HEREDITY, ENDANGERED AND EXTINCT SPECIES, CONSERVATION OF RESOURCES
## 6 BLACK HOLES (SPACE), PHYSICS, SPACE AND ASTRONOMY
## org_facet
## 1
## 2 FRONTIERS IN BEHAVIORAL NEUROSCIENCE (JOURNAL)
## 3 PALEONTOLOGY, FOSSILS, SCIENCE (JOURNAL)
## 4 TEMPERATURE
## 5 CURRENT BIOLOGY (JOURNAL)
## 6 MATHEMATICS
## per_facet
## 1
## 2 KASHIWADANI, HIDEKI
## 3 SALLAN, LAUREN
## 4
## 5 LUO, SHU-JIN
## 6 HAWKING, STEPHEN W, EINSTEIN, ALBERT
## geo_facet
## 1
## 2
## 3
## 4
## 5 FAR EAST, SOUTH AND SOUTHEAST ASIA AND PACIFIC AREAS
## 6
## media
## 1 image, photo, A picture taken by the European Space Agencys Mars Express spacecraft showing an elongated white plume that resembles volcanic ejecta., ESA/GCP/UPV/EHU Bilbao, 1, https://static01.nyt.com/images/2018/10/28/science/28MARSpromo/28MARSpromo-thumbStandard.jpg, https://static01.nyt.com/images/2018/10/28/science/28MARSpromo/28MARSpromo-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2018/10/28/science/28MARSpromo/28MARSpromo-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 2 image, photo, A lavender field in Valensole, France., Eric Gaillard/Reuters, 1, https://static01.nyt.com/images/2018/10/24/science/24TB-LAVENDER/24TB-LAVENDER-thumbStandard.jpg, https://static01.nyt.com/images/2018/10/24/science/24TB-LAVENDER/24TB-LAVENDER-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2018/10/24/science/24TB-LAVENDER/24TB-LAVENDER-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 3 image, photo, An artist's impression of pituriaspids, among early types of fish, which are first thought to have evolved around 480 million years ago., Nobumichi Tamura, 1, https://static01.nyt.com/images/2018/10/26/science/26TB-FISH1/26TB-FISH1-thumbStandard.jpg, https://static01.nyt.com/images/2018/10/26/science/26TB-FISH1/26TB-FISH1-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2018/10/26/science/26TB-FISH1/26TB-FISH1-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 4 image, photo, Denver Broncos fans huddling before a home game against the Los Angeles Rams this month., Isaiah J. Downing/USA Today Sports, via Reuters, 1, https://static01.nyt.com/images/2018/10/24/science/24xp-acclimation/24xp-acclimation-thumbStandard.jpg, https://static01.nyt.com/images/2018/10/24/science/24xp-acclimation/merlin_145303512_f81e6b52-5c58-4a7a-81cf-dbc3faafae9c-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2018/10/24/science/24xp-acclimation/merlin_145303512_f81e6b52-5c58-4a7a-81cf-dbc3faafae9c-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 5 image, photo, A Sumatran tiger at the Ragunan Zoo in Jakarta, Indonesia. Sumatran tigers were the first to evolve from all tigers common ancestor., Romeo Gacad/Agence France-Presse Getty Images, 1, https://static01.nyt.com/images/2018/10/26/science/26TIGERS4/26TIGERS4-thumbStandard-v2.jpg, https://static01.nyt.com/images/2018/10/26/science/26TIGERS4/26TIGERS4-mediumThreeByTwo210-v2.jpg, https://static01.nyt.com/images/2018/10/26/science/26TIGERS4/26TIGERS4-mediumThreeByTwo440-v2.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 6 image, photo, In his posthumously published, last scientific paper, Stephen Hawking theorized that information may yet be able to elude a black hole by becoming encoded on its surface in the form of soft hair., Desiree Martin/Agence France-Presse Getty Images, 1, https://static01.nyt.com/images/2018/10/23/science/00OUTTHERE1/00OUTTHERE1-thumbStandard.jpg, https://static01.nyt.com/images/2018/10/23/science/00OUTTHERE1/00OUTTHERE1-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2018/10/23/science/00OUTTHERE1/00OUTTHERE1-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
I was able to call NY times website using their web API. I searched for most viewed science articles in last one day and I found 6 articles and information around those articles. Looks like ble to use web-api is a very helpful skill.