Topic: Choose one the the NYT API’s and construct an interface in R to read the JSON DATA, and transform into Dataframe. I will read the most viewed articles for the last seven days
Load libraries
library(httr)
library(jsonlite)
## Warning: package 'jsonlite' was built under R version 4.3.3
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
NYT API keyt
api_key <- "U8widxelyFPIQTYG6XD4fI4uCDur2Iem"
Load the website most viewed articles for the last seven days.
api_endpoint <- "https://api.nytimes.com/svc/mostpopular/v2/viewed/1.json?api-key="
concatenate the website and API
api_url <- paste0(api_endpoint, api_key)
*8Retrieve JSON data**
json_object <- fromJSON(api_url)
assign the resulkts into a dataframe
df <- json_object$results
class(df)
## [1] "data.frame"
glimpse(df)
## Rows: 20
## Columns: 22
## $ uri <chr> "nyt://article/22072f09-d98b-5fb7-9183-c84e022ca272", "…
## $ url <chr> "https://www.nytimes.com/2024/03/22/opinion/kate-middle…
## $ id <dbl> 1e+14, 1e+14, 1e+14, 1e+14, 1e+14, 1e+14, 1e+14, 1e+14,…
## $ asset_id <dbl> 1e+14, 1e+14, 1e+14, 1e+14, 1e+14, 1e+14, 1e+14, 1e+14,…
## $ source <chr> "New York Times", "New York Times", "New York Times", "…
## $ published_date <chr> "2024-03-22", "2024-03-22", "2024-03-23", "2024-03-21",…
## $ updated <chr> "2024-03-23 09:30:01", "2024-03-23 09:13:44", "2024-03-…
## $ section <chr> "Opinion", "U.S.", "Theater", "Real Estate", "Business"…
## $ subsection <chr> "", "Politics", "", "", "", "", "Move", "", "", "", "",…
## $ nytdsection <chr> "opinion", "u.s.", "theater", "real estate", "business"…
## $ adx_keywords <chr> "Royal Families;News and News Media;Rumors and Misinfor…
## $ column <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ byline <chr> "By Maureen Dowd", "By Catie Edmondson", "By Rob Tannen…
## $ type <chr> "Article", "Article", "Article", "Interactive", "Articl…
## $ title <chr> "Kate and the King", "Congress Passes Spending Bill in …
## $ abstract <chr> "Cancer is a very personal thing, but the 42-year-old p…
## $ des_facet <list> <"Royal Families", "News and News Media", "Rumors and …
## $ org_facet <list> <>, <"House of Representatives", "Senate", "Democratic…
## $ per_facet <list> <"Catherine, Princess of Wales", "Charles III, King of…
## $ geo_facet <list> "Great Britain", <>, <>, "Santa Barbara (Calif)", <>, …
## $ media <list> [<data.frame[1 x 6]>], [<data.frame[1 x 6]>], [<data.f…
## $ eta_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
df <- subset(df, select = c(type, section, title, byline, published_date, updated ))
print(df)
## type section
## 1 Article Opinion
## 2 Article U.S.
## 3 Article Theater
## 4 Interactive Real Estate
## 5 Article Business
## 6 Article Briefing
## 7 Article Well
## 8 Article Science
## 9 Article U.S.
## 10 Article Opinion
## 11 Article New York
## 12 Article U.S.
## 13 Interactive The Upshot
## 14 Article Opinion
## 15 Article Opinion
## 16 Interactive World
## 17 Article World
## 18 Interactive Briefing
## 19 Article Arts
## 20 Article Well
## title
## 1 Kate and the King
## 2 Congress Passes Spending Bill in Wee Hours to Fend Off Shutdown
## 3 With ‘Tommy’ Revival, Pete Townshend Is Talking to a New Generation
## 4 In Santa Barbara, Their Budget Would Buy a Mobile Home. Which One Would You Choose?
## 5 First He Came for Cancel Culture. Now He Wants to Cancel Smartphones
## 6 A New Game From The Times
## 7 5 Exercises We Hate, and Why You Should Do Them Anyway
## 8 A Total Solar Eclipse Is Coming. Here’s What You Need to Know.
## 9 Jasmin Paris Becomes First Woman to Complete Extreme Barkley Race
## 10 America’s Most Overlooked Political Divide Is Also Its Most Revealing
## 11 The Hotel Guest Who Wouldn’t Leave
## 12 F.B.I. Tells Passengers on Alaska Flight They May Have Been Crime Victims
## 13 Flashback: Your Weekly History Quiz, March 23, 2024
## 14 James Carville, the Cajun Who Can’t Stop Ragin’
## 15 The Online Degradation of Women and Girls That We Meet With a Shrug
## 16 Maps and Diagrams of the Moscow Concert Hall Attack
## 17 U.S. Says ISIS Was Responsible for Deadly Moscow Concert Hall Attack
## 18 The New York Times News Quiz, March 22, 2024
## 19 The 50 Best TV Shows on Netflix Right Now
## 20 Four Years On, the Mysteries of Covid Are Unraveling
## byline
## 1 By Maureen Dowd
## 2 By Catie Edmondson
## 3 By Rob Tannenbaum
## 4 By Livia Albeck-Ripka
## 5 By Emma Goldberg
## 6 By David Leonhardt
## 7 By Anna Maltby
## 8 By Katrina Miller
## 9 By Emmett Lindner
## 10 By David French
## 11 By Matthew Haag
## 12 By Mark Walker
## 13
## 14 By Maureen Dowd
## 15 By Nicholas Kristof
## 16 By Lazaro Gamio, Martín González Gómez, Ang Li and Karthik Patanjali
## 17 By Julian E. Barnes and Eric Schmitt
## 18
## 19 By Noel Murray
## 20 By Knvul Sheikh
## published_date updated
## 1 2024-03-22 2024-03-23 09:30:01
## 2 2024-03-22 2024-03-23 09:13:44
## 3 2024-03-23 2024-03-24 13:32:07
## 4 2024-03-21 2024-03-22 12:10:19
## 5 2024-03-23 2024-03-24 13:44:30
## 6 2024-03-24 2024-03-24 12:24:48
## 7 2024-03-06 2024-03-20 15:52:27
## 8 2024-03-22 2024-03-23 12:43:55
## 9 2024-03-23 2024-03-24 08:16:50
## 10 2024-03-22 2024-03-24 09:41:50
## 11 2024-03-24 2024-03-24 09:13:53
## 12 2024-03-22 2024-03-22 16:05:28
## 13 2024-03-22 2024-03-22 21:14:38
## 14 2024-03-23 2024-03-24 11:30:03
## 15 2024-03-23 2024-03-24 09:49:59
## 16 2024-03-23 2024-03-24 00:20:51
## 17 2024-03-22 2024-03-23 12:28:04
## 18 2024-03-22 2024-03-22 17:44:51
## 19 2020-09-04 2024-03-21 15:18:42
## 20 2024-03-09 2024-03-12 08:44:59