library(httr)
library(jsonlite)
api_key <- "C6AszAhyzZTb6Cmrsi4nCumNZSHwqz3f"
# Construct the full URL with the API key embedded
url <- paste0("https://api.nytimes.com/svc/mostpopular/v2/viewed/7.json?api-key=", api_key)
# Get request created using full url and api key
response <- GET(url)
# Check if the request and connection was successful, the converts results field into dataframe
if (status_code(response) == 200) {
json_data <- content(response, as = "text", encoding = "UTF-8")
parsed_data <- fromJSON(json_data, flatten = TRUE)
articles_df <- as.data.frame(parsed_data$results)
head(articles_df)
} else {
stop("Failed to retrieve data: ", status_code(response))
}
## uri
## 1 nyt://article/eb5d2983-90ef-5ee2-9c32-407216721dba
## 2 nyt://article/0aa72568-0a85-5351-b0b5-4a30aec39a04
## 3 nyt://article/5810dbf3-c9bc-5fd0-aa77-ba03d49424f3
## 4 nyt://article/91698114-f2a6-5dca-9cd9-6c9bf9dad79c
## 5 nyt://article/68d86c92-fdd4-5fd9-b1a4-f515141bd40c
## 6 nyt://article/44b394dd-bb2f-536f-bf7f-5fbc9ed5c349
## url
## 1 https://www.nytimes.com/2024/10/22/us/politics/samuel-alito-princess-gloria.html
## 2 https://www.nytimes.com/2024/10/24/us/politics/harris-town-hall-cnn-takeaways.html
## 3 https://www.nytimes.com/2024/10/23/us/politics/north-korea-russia-military-ukraine.html
## 4 https://www.nytimes.com/2024/10/23/nyregion/jahmeik-modlin-dead-nyc-acs.html
## 5 https://www.nytimes.com/2024/10/20/arts/television/snl-recap-alec-baldwin-michael-keaton.html
## 6 https://www.nytimes.com/2024/10/25/opinion/college-university-students-reading.html
## id asset_id source published_date updated section
## 1 1e+14 1e+14 New York Times 2024-10-22 2024-10-23 13:38:19 U.S.
## 2 1e+14 1e+14 New York Times 2024-10-24 2024-10-24 15:35:05 U.S.
## 3 1e+14 1e+14 New York Times 2024-10-23 2024-10-24 08:29:41 U.S.
## 4 1e+14 1e+14 New York Times 2024-10-23 2024-10-25 08:25:00 New York
## 5 1e+14 1e+14 New York Times 2024-10-20 2024-10-20 11:53:15 Arts
## 6 1e+14 1e+14 New York Times 2024-10-25 2024-10-26 16:36:29 Opinion
## subsection nytdsection
## 1 Politics u.s.
## 2 Politics u.s.
## 3 Politics u.s.
## 4 new york
## 5 Television arts
## 6 opinion
## adx_keywords
## 1 Content Type: Personal Profile;Gifts to Public Officials;Abortion;Royal Families;Right-Wing Extremism and Alt-Right;Conservatism (US Politics);United States Politics and Government;United States International Relations;Gloria, Princess of Thurn and Taxis;Alito, Samuel A Jr;Supreme Court (US);Roman Catholic Church;Germany;Regensburg (Germany)
## 2 Presidential Election of 2024;Authoritarianism (Theory and Philosophy);United States Politics and Government;News and News Media;Television;internal-paywall-exempt-elections-app;Harris, Kamala D;Trump, Donald J;CNN
## 3 Russian Invasion of Ukraine (2022);Defense and Military Forces;Mercenaries and Private Military Contractors;United States International Relations;Putin, Vladimir V;Kim Jong-un;Russia;North Korea;Ukraine
## 4 Child Abuse and Neglect;Deaths (Fatalities);Malnutrition;Modlin, Jahmeik (d 2024);Ragsdale, Nytavia;Modlin, Laron;Administration for Children's Services (NYC);New York City;Harlem (Manhattan, NY)
## 5 Television
## 6 Colleges and Universities;Books and Literature;Labor and Jobs
## column byline type
## 1 NA By Abbie VanSickle, Philip Kaleta and Ingmar Nolting Article
## 2 NA By Reid J. Epstein and Lisa Lerer Article
## 3 NA By Eric Schmitt, David E. Sanger and Anatoly Kurmanaev Article
## 4 NA By Andy Newman Article
## 5 NA By Dave Itzkoff Article
## 6 NA By Jonathan Malesic Article
## title
## 1 The Princess and the Justice
## 2 Harris Calls Trump a Fascist: 6 Takeaways From Her CNN Town Hall
## 3 U.S. Says North Korean Troops Are in Russia to Aid Fight Against Ukraine
## 4 A 4-Year-Old Starved to Death at Home. How Were the Signs Missed?
## 5 ‘Saturday Night Live’ Welcomes Back Alec Baldwin
## 6 There’s a Very Good Reason College Students Don’t Read Anymore
## abstract
## 1 Princess Gloria von Thurn und Taxis bonded with Justice Samuel A. Alito Jr. over Catholicism and ending abortion. She introduced him to her sumptuous world when he visited her Bavarian palace.
## 2 Entering new rhetorical territory, the vice president turned even unrelated questions into attacks on Donald Trump as she offered long, winding answers to questions from voters.
## 3 Defense Secretary Lloyd J. Austin III called the situation “very, very serious,” though he said that what the soldiers were doing in Russia was “left to be seen.”
## 4 Jahmeik Modlin was found emaciated in a Harlem apartment stocked with food. His family’s child-welfare case had been closed two years before.
## 5 The longtime friend of “S.N.L.” made his first appearance on the show since an involuntary manslaughter case against him was dismissed. Michael Keaton was the host.
## 6 The world is sending a message about its priorities, and they do not appear to include “Moby-Dick.”
## des_facet
## 1 Content Type: Personal Profile, Gifts to Public Officials, Abortion, Royal Families, Right-Wing Extremism and Alt-Right, Conservatism (US Politics), United States Politics and Government, United States International Relations
## 2 Presidential Election of 2024, Authoritarianism (Theory and Philosophy), United States Politics and Government, News and News Media, Television, internal-paywall-exempt-elections-app
## 3 Russian Invasion of Ukraine (2022), Defense and Military Forces, Mercenaries and Private Military Contractors, United States International Relations
## 4 Child Abuse and Neglect, Deaths (Fatalities), Malnutrition
## 5 Television
## 6 Colleges and Universities, Books and Literature, Labor and Jobs
## org_facet
## 1 Supreme Court (US), Roman Catholic Church
## 2 CNN
## 3
## 4 Administration for Children's Services (NYC)
## 5
## 6
## per_facet
## 1 Gloria, Princess of Thurn and Taxis, Alito, Samuel A Jr
## 2 Harris, Kamala D, Trump, Donald J
## 3 Putin, Vladimir V, Kim Jong-un
## 4 Modlin, Jahmeik (d 2024), Ragsdale, Nytavia, Modlin, Laron
## 5
## 6
## geo_facet
## 1 Germany, Regensburg (Germany)
## 2
## 3 Russia, North Korea, Ukraine
## 4 New York City, Harlem (Manhattan, NY)
## 5
## 6
## media
## 1 image, photo, Princess Gloria von Thurn und Taxis, a 1980s party girl, has evolved into a conservative Catholic with ties to the European far right., , 1, https://static01.nyt.com/images/2024/10/18/multimedia/18dc-princess-top1-ghwb/18dc-princess-top1-ghwb-thumbStandard.jpg, https://static01.nyt.com/images/2024/10/18/multimedia/18dc-princess-top1-ghwb/18dc-princess-top1-ghwb-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2024/10/18/multimedia/18dc-princess-top1-ghwb/18dc-princess-top1-ghwb-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 2 image, photo, , Erin Schaff/The New York Times, 1, https://static01.nyt.com/images/2024/11/23/world/23pol-harris-takeaways-promo/23pol-harris-takeaways-promo-thumbStandard.jpg, https://static01.nyt.com/images/2024/11/23/world/23pol-harris-takeaways-promo/23pol-harris-takeaways-promo-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2024/11/23/world/23pol-harris-takeaways-promo/23pol-harris-takeaways-promo-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 3 NULL
## 4 image, photo, After Jahmeik Modlin, 4, was found dying of starvation in his Harlem apartment, his three siblings, also severely malnourished, were hospitalized., David Dee Delgado for The New York Times, 1, https://static01.nyt.com/images/2024/10/22/multimedia/22ny-child-death-kjbh/22ny-child-death-kjbh-thumbStandard-v2.jpg, https://static01.nyt.com/images/2024/10/22/multimedia/22ny-child-death-kjbh/22ny-child-death-kjbh-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2024/10/22/multimedia/22ny-child-death-kjbh/22ny-child-death-kjbh-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 5 image, photo, Michael Keaton hosted “Saturday Night Live” this weekend. (With Mikey Day, left, and Andy Samberg.), NBCUniversal, 1, https://static01.nyt.com/images/2024/10/20/arts/20snl/20snl-thumbStandard.jpg, https://static01.nyt.com/images/2024/10/20/arts/20snl/20snl-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2024/10/20/arts/20snl/20snl-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 6 image, photo, , Janet Mac, 0, https://static01.nyt.com/images/2024/10/25/opinion/25malesic-image/25malesic-image-thumbStandard.png, https://static01.nyt.com/images/2024/10/25/opinion/25malesic-image/25malesic-image-mediumThreeByTwo210.png, https://static01.nyt.com/images/2024/10/25/opinion/25malesic-image/25malesic-image-mediumThreeByTwo440.png, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## eta_id
## 1 0
## 2 0
## 3 0
## 4 0
## 5 0
## 6 0
#Cleans up the articles dataframe
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ purrr::flatten() masks jsonlite::flatten()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
articles_df <- articles_df %>%
select(title, byline, published_date, url) %>%
rename(
Title = title,
Author = byline,
PublishedDate = published_date,
ArticleURL = url
)
#Displays a sample of the data using kable
library(knitr)
kable(head(articles_df), caption = "Sample of Most Viewed Articles", align = 'l')