# Install and load packages

#install.packages("httr")
#install.packages("jsonlite")
#install.packages("dplyr")

library(httr)       # For making HTTP requests
library(jsonlite)   # For parsing JSON
## Warning: package 'jsonlite' was built under R version 4.4.3
library(dplyr)      # For data manipulation
## Warning: package 'dplyr' was built under R version 4.4.3
## 
## 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
api_key <- "YWE2St6LJrSEv7rHooAbCAUauyYTEPm5"

# Choose a section
section <- "technology"

# Build the full URL for the API request
url <- paste0(
  "https://api.nytimes.com/svc/topstories/v2/",
  section,
  ".json?api-key=",
  api_key
)

#
#Make the GET request to the API

response <- GET(url)

#
#heck for success and parse response
#
if (status_code(response) == 200) {
  
  # Get the JSON content as text
  data_raw <- content(response, "text")
  
  # Convert JSON text to a structured R list (flattened for easier DataFrame conversion)
  data_json <- fromJSON(data_raw, flatten = TRUE)
  
  # Extract the list of articles from the "results" field
  articles_df <- data_json$results
  
  # Show a preview of the full raw data
  print("Full raw dataset:")
  print(head(articles_df))
  
  # 
  #  Clean and select useful article fields

  articles_clean <- articles_df %>%
    select(
      section,
      title,
      abstract,
      byline,
      published_date,
      url
    )
  
  # Show a preview of the cleaned data
  print("Cleaned data:")
  print(head(articles_clean))
  
} else {
  # If the request failed, show the error code
  print(paste("Error: HTTP status", status_code(response)))
}
## No encoding supplied: defaulting to UTF-8.
## [1] "Full raw dataset:"
##      section subsection
## 1   business           
## 2 technology           
## 3 technology           
## 4 technology           
## 5      style           
## 6   business           
##                                                                     title
## 1                                              Are You Smarter Than A.I.?
## 2                                     How Artificial Intelligence Reasons
## 3    Facebook Returns to Its Roots: Showing Posts From Friends and Family
## 4 The Two Democrats Trump Fired From the F.T.C. Sue Over Their Dismissals
## 5      People Love Studio Ghibli. But Should They Be Able to Recreate It?
## 6                    How Major Car Brands Are Affected by Trump’s Tariffs
##                                                                                                                                             abstract
## 1 Some experts predict that A.I. will surpass human intelligence within the next few years. Play this puzzle to see how far the machines have to go.
## 2                        Companies like OpenAI and China’s DeepSeek offer chatbots designed to take their time with an answer. Here’s how they work.
## 3                               A new Friends Tab will feature posts from a user’s friends and relatives, which was the original mission of the app.
## 4 Rebecca Kelly Slaughter and Alvaro Bedoya, who were fired from the agency this month, accused President Trump in a lawsuit of executive overreach.
## 5                      An update to ChatGPT made it easy to simulate Hayao Miyazaki’s style of animation, which has flooded social media with memes.
## 6                                                               The scale of the damage depends on the circumstances of each company’s supply chain.
##                                                                                                 url
## 1 https://www.nytimes.com/interactive/2025/03/26/business/ai-smarter-human-intelligence-puzzle.html
## 2                  https://www.nytimes.com/2025/03/26/technology/ai-reasoning-chatgpt-deepseek.html
## 3                      https://www.nytimes.com/2025/03/27/technology/meta-facebook-friends-tab.html
## 4              https://www.nytimes.com/2025/03/27/technology/ftc-fired-democrats-trump-lawsuit.html
## 5                            https://www.nytimes.com/2025/03/27/style/ai-chatgpt-studio-ghibli.html
## 6                    https://www.nytimes.com/2025/03/27/business/trump-auto-tariffs-car-brands.html
##                                                      uri
## 1 nyt://interactive/e9660635-5233-5aa6-a184-7dbb64f3e929
## 2     nyt://article/faf9611e-5f71-50c0-bd6e-171107713c9f
## 3     nyt://article/63894ca4-1c64-5c3a-bb6b-b32266161e75
## 4     nyt://article/61c91493-ae63-5cd5-bed3-93fb23606923
## 5     nyt://article/1766276c-3dc9-583c-aae0-8d63c6f231ef
## 6     nyt://article/5742b9a9-68fb-5a6b-b6d2-942749298af6
##                            byline   item_type              updated_date
## 1 By Dylan Freedman and Cade Metz Interactive 2025-03-27T15:47:24-04:00
## 2 By Cade Metz and Dylan Freedman     Article 2025-03-27T05:49:43-04:00
## 3                   By Mike Isaac     Article 2025-03-27T17:28:48-04:00
## 4                 By David McCabe     Article 2025-03-27T14:59:35-04:00
## 5       By Madison Malone Kircher     Article 2025-03-27T17:12:40-04:00
## 6                  By Vikas Bajaj     Article 2025-03-27T16:32:19-04:00
##                created_date            published_date material_type_facet
## 1 2025-03-26T18:31:25-04:00 2025-03-26T18:31:25-04:00                    
## 2 2025-03-26T18:41:45-04:00 2025-03-26T18:41:45-04:00                    
## 3 2025-03-27T12:30:09-04:00 2025-03-27T12:30:09-04:00                    
## 4 2025-03-27T13:01:00-04:00 2025-03-27T13:01:00-04:00                    
## 5 2025-03-27T15:00:11-04:00 2025-03-27T15:00:11-04:00                    
## 6 2025-03-27T11:04:16-04:00 2025-03-27T11:04:16-04:00                    
##   kicker
## 1       
## 2       
## 3       
## 4       
## 5       
## 6       
##                                                                                                                                                                                               des_facet
## 1                                                                                                                            Artificial Intelligence, Research, vis-design, Puzzles, Content Type: Quiz
## 2                                                                                                       Artificial Intelligence, Computers and the Internet, ChatGPT, Innovation, Content Type: Service
## 3                                                                                                                                         Social Media, Computers and the Internet, Mobile Applications
## 4 United States Politics and Government, Appointments and Executive Changes, Suits and Litigation (Civil), Presidential Power (US), Regulation and Deregulation of Industry, Computers and the Internet
## 5                                                                                                                                        Social Media, Artificial Intelligence, Animated Films, ChatGPT
## 6                                                                                                      Automobiles, International Trade and World Market, Customs (Tariff), Factories and Manufacturing
##                                                                                          org_facet
## 1                                                                                      OpenAI Labs
## 2                                                                                      OpenAI Labs
## 3                                                                 Facebook Inc, Meta Platforms Inc
## 4                                                                         Federal Trade Commission
## 5                                                                       Studio Ghibli, OpenAI Labs
## 6 Tesla Motors Inc, General Motors, Ford Motor Co, Stellantis NV, Volkswagen AG, Toyota Motor Corp
##                                                per_facet geo_facet
## 1                                                             NULL
## 2                                             Klein, Dan      NULL
## 3 Zuckerberg, Mark E, Alison, Tom (Technology Executive)      NULL
## 4    Slaughter, Rebecca, Bedoya, Alvaro, Trump, Donald J      NULL
## 5                      Altman, Samuel H, Miyazaki, Hayao      NULL
## 6                            Trump, Donald J, Musk, Elon      NULL
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           multimedia
## 1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               https://static01.nyt.com/images/2025/03/21/business/ai-smarter-human-intelligence-puzzle-1743004295210/ai-smarter-human-intelligence-puzzle-1743004295210-superJumbo.jpg, https://static01.nyt.com/images/2025/03/21/business/ai-smarter-human-intelligence-puzzle-1743004295210/ai-smarter-human-intelligence-puzzle-1743004295210-threeByTwoSmallAt2X.jpg, https://static01.nyt.com/images/2025/03/21/business/ai-smarter-human-intelligence-puzzle-1743004295210/ai-smarter-human-intelligence-puzzle-1743004295210-thumbLarge.jpg, Super Jumbo, threeByTwoSmallAt2X, Large Thumbnail, 1000, 400, 150, 1500, 600, 150, image, image, image, photo, photo, photo, , , , The New York Times, The New York Times, The New York Times
## 2                                                                                                                                                                                                                                                                                                                                                                             https://static01.nyt.com/images/2025/03/24/multimedia/24biz-reasoning-1-04-fmhg/24biz-reasoning-1-04-fmhg-superJumbo.jpg, https://static01.nyt.com/images/2025/03/24/multimedia/24biz-reasoning-1-04-fmhg/24biz-reasoning-1-04-fmhg-threeByTwoSmallAt2X.jpg, https://static01.nyt.com/images/2025/03/24/multimedia/24biz-reasoning-1-04-fmhg/24biz-reasoning-1-04-fmhg-thumbLarge.jpg, Super Jumbo, threeByTwoSmallAt2X, Large Thumbnail, 1536, 400, 150, 2048, 600, 150, image, image, image, photo, photo, photo, OpenAI’s chatbot has steadily improved as the company has added new technology behind the scenes., OpenAI’s chatbot has steadily improved as the company has added new technology behind the scenes., OpenAI’s chatbot has steadily improved as the company has added new technology behind the scenes., Kelsey McClellan for The New York Times, Kelsey McClellan for The New York Times, Kelsey McClellan for The New York Times
## 3 https://static01.nyt.com/images/2025/03/27/business/27facebook27FACEBOOK-VIDEO-1-cover/27facebook27FACEBOOK-VIDEO-1-cover-superJumbo.jpg, https://static01.nyt.com/images/2025/03/27/business/27facebook27FACEBOOK-VIDEO-1-cover/27facebook27FACEBOOK-VIDEO-1-cover-threeByTwoSmallAt2X.jpg, https://static01.nyt.com/images/2025/03/27/business/27facebook27FACEBOOK-VIDEO-1-cover/27facebook27FACEBOOK-VIDEO-1-cover-thumbLarge.jpg, Super Jumbo, threeByTwoSmallAt2X, Large Thumbnail, 836, 400, 150, 960, 600, 150, image, image, image, photo, photo, photo, On Thursday, Meta is announcing a new Facebook feature: Going back to showing people a feed of their friend connections. It’s a move toward what Mark Zuckerberg has called “returning to OG Facebook,” where everything old is new again., On Thursday, Meta is announcing a new Facebook feature: Going back to showing people a feed of their friend connections. It’s a move toward what Mark Zuckerberg has called “returning to OG Facebook,” where everything old is new again., On Thursday, Meta is announcing a new Facebook feature: Going back to showing people a feed of their friend connections. It’s a move toward what Mark Zuckerberg has called “returning to OG Facebook,” where everything old is new again., via Meta, via Meta, via Meta
## 4                                                                                                                                                                                                           https://static01.nyt.com/images/2025/03/27/multimedia/27biz-ftc-suit-hfo-kjhz/27biz-ftc-suit-hfo-kjhz-superJumbo.jpg, https://static01.nyt.com/images/2025/03/27/multimedia/27biz-ftc-suit-hfo-kjhz/27biz-ftc-suit-hfo-kjhz-threeByTwoSmallAt2X.jpg, https://static01.nyt.com/images/2025/03/27/multimedia/27biz-ftc-suit-hfo-kjhz/27biz-ftc-suit-hfo-kjhz-thumbLarge.jpg, Super Jumbo, threeByTwoSmallAt2X, Large Thumbnail, 1366, 400, 150, 2048, 600, 150, image, image, image, photo, photo, photo, The Federal Trade Commission. A lawsuit by two former F.T.C. commissioners is the latest legal battle over Mr. Trump’s attempts to expand the power of the presidency. , The Federal Trade Commission. A lawsuit by two former F.T.C. commissioners is the latest legal battle over Mr. Trump’s attempts to expand the power of the presidency. , The Federal Trade Commission. A lawsuit by two former F.T.C. commissioners is the latest legal battle over Mr. Trump’s attempts to expand the power of the presidency. , Eric Lee/The New York Times, Eric Lee/The New York Times, Eric Lee/The New York Times
## 5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   https://static01.nyt.com/images/2025/03/27/multimedia/27GHIBLI-AI-ART-01-hmqf/27GHIBLI-AI-ART-01-hmqf-superJumbo.jpg, https://static01.nyt.com/images/2025/03/27/multimedia/27GHIBLI-AI-ART-01-hmqf/27GHIBLI-AI-ART-01-hmqf-threeByTwoSmallAt2X.jpg, https://static01.nyt.com/images/2025/03/27/multimedia/27GHIBLI-AI-ART-01-hmqf/27GHIBLI-AI-ART-01-hmqf-thumbLarge.jpg, Super Jumbo, threeByTwoSmallAt2X, Large Thumbnail, 1620, 400, 150, 1080, 600, 150, image, image, image, photo, photo, photo, , , , Kouka Webb, Kouka Webb, Kouka Webb
## 6                                                                                                                                                                                                                                                                                                                                               https://static01.nyt.com/images/2025/03/27/multimedia/27biz-auto-companies-whzl/27biz-auto-companies-whzl-superJumbo.jpg, https://static01.nyt.com/images/2025/03/27/multimedia/27biz-auto-companies-whzl/27biz-auto-companies-whzl-threeByTwoSmallAt2X.jpg, https://static01.nyt.com/images/2025/03/27/multimedia/27biz-auto-companies-whzl/27biz-auto-companies-whzl-thumbLarge.jpg, Super Jumbo, threeByTwoSmallAt2X, Large Thumbnail, 1536, 400, 150, 2048, 600, 150, image, image, image, photo, photo, photo, Tesla chargers in Pasadena, Calif. The company is perhaps the least exposed to tariffs among major automakers., Tesla chargers in Pasadena, Calif. The company is perhaps the least exposed to tariffs among major automakers., Tesla chargers in Pasadena, Calif. The company is perhaps the least exposed to tariffs among major automakers., Philip Cheung for The New York Times, Philip Cheung for The New York Times, Philip Cheung for The New York Times
##   short_url
## 1          
## 2          
## 3          
## 4          
## 5          
## 6          
## [1] "Cleaned data:"
##      section
## 1   business
## 2 technology
## 3 technology
## 4 technology
## 5      style
## 6   business
##                                                                     title
## 1                                              Are You Smarter Than A.I.?
## 2                                     How Artificial Intelligence Reasons
## 3    Facebook Returns to Its Roots: Showing Posts From Friends and Family
## 4 The Two Democrats Trump Fired From the F.T.C. Sue Over Their Dismissals
## 5      People Love Studio Ghibli. But Should They Be Able to Recreate It?
## 6                    How Major Car Brands Are Affected by Trump’s Tariffs
##                                                                                                                                             abstract
## 1 Some experts predict that A.I. will surpass human intelligence within the next few years. Play this puzzle to see how far the machines have to go.
## 2                        Companies like OpenAI and China’s DeepSeek offer chatbots designed to take their time with an answer. Here’s how they work.
## 3                               A new Friends Tab will feature posts from a user’s friends and relatives, which was the original mission of the app.
## 4 Rebecca Kelly Slaughter and Alvaro Bedoya, who were fired from the agency this month, accused President Trump in a lawsuit of executive overreach.
## 5                      An update to ChatGPT made it easy to simulate Hayao Miyazaki’s style of animation, which has flooded social media with memes.
## 6                                                               The scale of the damage depends on the circumstances of each company’s supply chain.
##                            byline            published_date
## 1 By Dylan Freedman and Cade Metz 2025-03-26T18:31:25-04:00
## 2 By Cade Metz and Dylan Freedman 2025-03-26T18:41:45-04:00
## 3                   By Mike Isaac 2025-03-27T12:30:09-04:00
## 4                 By David McCabe 2025-03-27T13:01:00-04:00
## 5       By Madison Malone Kircher 2025-03-27T15:00:11-04:00
## 6                  By Vikas Bajaj 2025-03-27T11:04:16-04:00
##                                                                                                 url
## 1 https://www.nytimes.com/interactive/2025/03/26/business/ai-smarter-human-intelligence-puzzle.html
## 2                  https://www.nytimes.com/2025/03/26/technology/ai-reasoning-chatgpt-deepseek.html
## 3                      https://www.nytimes.com/2025/03/27/technology/meta-facebook-friends-tab.html
## 4              https://www.nytimes.com/2025/03/27/technology/ftc-fired-democrats-trump-lawsuit.html
## 5                            https://www.nytimes.com/2025/03/27/style/ai-chatgpt-studio-ghibli.html
## 6                    https://www.nytimes.com/2025/03/27/business/trump-auto-tariffs-car-brands.html