library(jsonlite)
library(httr)
api_key<-"ZG5iX7DyAy42pXiOem9VhpqgscAbq4Dd"
topic<-"arts"
#function Get_JSON is written
Get_JSON<-function(api_key,topic){
template_URL<-paste("https://api.nytimes.com/svc/topstories/v2/",topic,".json?api-key=",api_key,sep="")
df<-jsonlite::fromJSON(template_URL,flatten=TRUE)
df_raw<-as.data.frame(df)
df<-data.frame(df_raw$results.title,df_raw$results.subsection,df_raw$results.abstract)
colnames(df)<-c("title","subsection","abstract")
head(df_raw,5)
head(df,5)
}
Get_JSON(api_key,topic)
## title
## 1 Alec Baldwin Fatally Shoots Crew Member With Prop Firearm, Authorities Say
## 2 A Cinematographer Killed on Set Is Recalled as Talented and Spirited
## 3 In the Company of Wes Anderson
## 4 How Hans Zimmer Conjured the Otherworldly Sounds of ‘Dune’
## 5 ‘American Veteran’ Honors the Many Faces of the U.S. Soldier
## subsection
## 1
## 2
## 3
## 4
## 5 television
## abstract
## 1 The movie’s director of photography was killed and the director was injured on the set of “Rust,” a Western, a sheriff’s office in New Mexico said.
## 2 Halyna Hutchins, the director of photography for the film “Rust,” was killed when the actor Alec Baldwin discharged a firearm being used as a prop.
## 3 What is it about the director that draws stars like Frances McDormand and Tilda Swinton again and again? There are many reasons, but the nightly feasts don’t hurt.
## 4 The composer worked with a far-flung “band” of collaborators who sung, scraped metal, invented instruments and more for the score.
## 5 On the heels of America’s longest war, a new PBS documentary series sits down with nearly 50 veterans in hopes of helping to bridge a growing gap.
#Test 1: Topic "automobiles"
topic<-"automobiles"
Get_JSON(api_key,topic)
## title
## 1 N.T.S.B. says Tesla owner was in the driver’s seat in fatal crash, contradicting local officials.
## 2 A Tour of Italy, and a Century of Stunning Cars
## 3 Nostalgia Plus Time Equals a New Breed of Collectible Cars
## 4 High Octane and a Stick Shift: Standout Cars as an Era Starts to End
## 5 Tesla’s quarterly profit nearly quintuples to $1.6 billion as car sales surge.
## subsection
## 1
## 2
## 3
## 4
## 5
## abstract
## 1 A Harris County sheriff’s constable said in April that evidence at the scene of the accident suggested that no one was driving the car when it crashed.
## 2 There’s more to see than Ferrari and Lamborghini, and three museums in the country’s automotive heartland offer a deeper look.
## 3 The pursuit of a car wistfully recalled from the writer’s youth begins with an exhaustive online search and ends with an auction victory and a special delivery.
## 4 Hitting the road and track with Cadillac’s roaring Blackwings, a Porsche 911 GTS and the rare BMW M2 CS.
## 5 The automaker also reported a big jump in revenue, to $13.8 billion from $8.8 billion a year ago, as sales of the Model Y continue to rise.
#Test 2: Topic "books"
topic<-"books"
Get_JSON(api_key,topic)
## title
## 1 ‘The End of Bias’ Says There’s Hope for Meaningful Change
## 2 What George Orwell’s Roses Tell Us About His Politics
## 3 Writing About a Past Injustice Helped Her See What Has and Hasn’t Changed
## 4 This Novel Nods to Virginia Woolf While Staring Down Modern Class Lines
## 5 Globetrotting
## subsection
## 1
## 2 review
## 3
## 4
## 5
## abstract
## 1 In her new book, Jessica Nordell examines ways to overcome unexamined stereotypes and the harm they cause.
## 2 In “Orwell’s Roses,” Rebecca Solnit argues that the English writer was driven not merely by political rage but by a love of beauty and nature.
## 3 Nadifa Mohamed is a Booker Prize finalist for her novel “The Fortune Men,” a story about a false accusation and the tragedy that resulted.
## 4 In “The Days of Afrekete,” by Asali Solomon, a woman hosting a dinner party for her soon-to-be-disgraced husband spends time remembering a woman she loved in college.
## 5 Your sneak preview of books in translation coming out in 2021, updated each season.