library(httr)
## Warning: package 'httr' was built under R version 4.4.2
library(jsonlite)
## Warning: package 'jsonlite' was built under R version 4.4.2
library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.4.2
## Warning: package 'dplyr' was built under R version 4.4.2
## ── 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
library(kableExtra)
## Warning: package 'kableExtra' was built under R version 4.4.2
## 
## Attaching package: 'kableExtra'
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
library(dplyr)
dotenv::load_dot_env(file = '.env')
Sys.getenv('my_key')
## [1] "oJaiGJzQtRpzC4t73LcoxAMAPVOvXG08"
url <- paste0("https://api.nytimes.com/svc/mostpopular/v2/viewed/7.json?api-key=", Sys.getenv('my_key'))
response <- httr::GET(url)
str(response)
## List of 10
##  $ url        : chr "https://api.nytimes.com/svc/mostpopular/v2/viewed/7.json?api-key=oJaiGJzQtRpzC4t73LcoxAMAPVOvXG08"
##  $ status_code: int 200
##  $ headers    :List of 25
##   ..$ date                         : chr "Mon, 11 Nov 2024 06:33:21 GMT"
##   ..$ content-type                 : chr "application/json; charset=utf-8"
##   ..$ transfer-encoding            : chr "chunked"
##   ..$ connection                   : chr "keep-alive"
##   ..$ cache-control                : chr "max-age=60"
##   ..$ x-nyt-most-popular-values    : chr "VIEWED 7"
##   ..$ x-request-id                 : chr "9eedd427-2157-4941-a2e3-4ea2f3b9b4d1"
##   ..$ x-envoy-upstream-service-time: chr "234"
##   ..$ server                       : chr "envoy"
##   ..$ content-encoding             : chr "gzip"
##   ..$ x-envoy-decorator-operation  : chr "trending-api.publishing.nyti.nyt.net:443/*"
##   ..$ accept-ranges                : chr "bytes"
##   ..$ via                          : chr "1.1 varnish"
##   ..$ age                          : chr "37"
##   ..$ x-served-by                  : chr "cache-iad-kjyo7100175-IAD"
##   ..$ x-cache                      : chr "HIT"
##   ..$ x-cache-hits                 : chr "1"
##   ..$ x-timer                      : chr "S1731306802.671933,VS0,VE1"
##   ..$ vary                         : chr "Accept-Encoding"
##   ..$ x-nyt-mktg-group             : chr "group1"
##   ..$ access-control-allow-origin  : chr "*"
##   ..$ access-control-allow-headers : chr "Accept, Content-Type, X-Forwarded-For, X-Prototype-Version, X-Requested-With"
##   ..$ access-control-expose-headers: chr "Content-Length, X-JSON"
##   ..$ access-control-allow-methods : chr "GET, OPTIONS"
##   ..$ strict-transport-security    : chr "max-age=63072000; preload; includeSubdomains"
##   ..- attr(*, "class")= chr [1:2] "insensitive" "list"
##  $ all_headers:List of 1
##   ..$ :List of 3
##   .. ..$ status : int 200
##   .. ..$ version: chr "HTTP/1.1"
##   .. ..$ headers:List of 25
##   .. .. ..$ date                         : chr "Mon, 11 Nov 2024 06:33:21 GMT"
##   .. .. ..$ content-type                 : chr "application/json; charset=utf-8"
##   .. .. ..$ transfer-encoding            : chr "chunked"
##   .. .. ..$ connection                   : chr "keep-alive"
##   .. .. ..$ cache-control                : chr "max-age=60"
##   .. .. ..$ x-nyt-most-popular-values    : chr "VIEWED 7"
##   .. .. ..$ x-request-id                 : chr "9eedd427-2157-4941-a2e3-4ea2f3b9b4d1"
##   .. .. ..$ x-envoy-upstream-service-time: chr "234"
##   .. .. ..$ server                       : chr "envoy"
##   .. .. ..$ content-encoding             : chr "gzip"
##   .. .. ..$ x-envoy-decorator-operation  : chr "trending-api.publishing.nyti.nyt.net:443/*"
##   .. .. ..$ accept-ranges                : chr "bytes"
##   .. .. ..$ via                          : chr "1.1 varnish"
##   .. .. ..$ age                          : chr "37"
##   .. .. ..$ x-served-by                  : chr "cache-iad-kjyo7100175-IAD"
##   .. .. ..$ x-cache                      : chr "HIT"
##   .. .. ..$ x-cache-hits                 : chr "1"
##   .. .. ..$ x-timer                      : chr "S1731306802.671933,VS0,VE1"
##   .. .. ..$ vary                         : chr "Accept-Encoding"
##   .. .. ..$ x-nyt-mktg-group             : chr "group1"
##   .. .. ..$ access-control-allow-origin  : chr "*"
##   .. .. ..$ access-control-allow-headers : chr "Accept, Content-Type, X-Forwarded-For, X-Prototype-Version, X-Requested-With"
##   .. .. ..$ access-control-expose-headers: chr "Content-Length, X-JSON"
##   .. .. ..$ access-control-allow-methods : chr "GET, OPTIONS"
##   .. .. ..$ strict-transport-security    : chr "max-age=63072000; preload; includeSubdomains"
##   .. .. ..- attr(*, "class")= chr [1:2] "insensitive" "list"
##  $ cookies    :'data.frame': 0 obs. of  7 variables:
##   ..$ domain    : logi(0) 
##   ..$ flag      : logi(0) 
##   ..$ path      : logi(0) 
##   ..$ secure    : logi(0) 
##   ..$ expiration: 'POSIXct' num(0) 
##   ..$ name      : logi(0) 
##   ..$ value     : logi(0) 
##  $ content    : raw [1:39921] 7b 22 73 74 ...
##  $ date       : POSIXct[1:1], format: "2024-11-11 06:33:21"
##  $ times      : Named num [1:6] 0 0.0402 0.0752 0.2391 0.5283 ...
##   ..- attr(*, "names")= chr [1:6] "redirect" "namelookup" "connect" "pretransfer" ...
##  $ request    :List of 7
##   ..$ method    : chr "GET"
##   ..$ url       : chr "https://api.nytimes.com/svc/mostpopular/v2/viewed/7.json?api-key=oJaiGJzQtRpzC4t73LcoxAMAPVOvXG08"
##   ..$ headers   : Named chr "application/json, text/xml, application/xml, */*"
##   .. ..- attr(*, "names")= chr "Accept"
##   ..$ fields    : NULL
##   ..$ options   :List of 2
##   .. ..$ useragent: chr "libcurl/8.3.0 r-curl/5.2.1 httr/1.4.7"
##   .. ..$ httpget  : logi TRUE
##   ..$ auth_token: NULL
##   ..$ output    : list()
##   .. ..- attr(*, "class")= chr [1:2] "write_memory" "write_function"
##   ..- attr(*, "class")= chr "request"
##  $ handle     :Class 'curl_handle' <externalptr> 
##  - attr(*, "class")= chr "response"
summary(response)
##             Length Class       Mode       
## url             1  -none-      character  
## status_code     1  -none-      numeric    
## headers        25  insensitive list       
## all_headers     1  -none-      list       
## cookies         7  data.frame  list       
## content     39921  -none-      raw        
## date            1  POSIXct     numeric    
## times           6  -none-      numeric    
## request         7  request     list       
## handle          1  curl_handle externalptr
mostpolular <- httr::content(response, as = "text", encoding = "UTF-8")
df <- fromJSON(mostpolular, flatten = TRUE)
df <- data.frame(df$results, stringsAsFactors = FALSE)

#Get column names
colnames(df)
##  [1] "uri"            "url"            "id"             "asset_id"      
##  [5] "source"         "published_date" "updated"        "section"       
##  [9] "subsection"     "nytdsection"    "adx_keywords"   "column"        
## [13] "byline"         "type"           "title"          "abstract"      
## [17] "des_facet"      "org_facet"      "per_facet"      "geo_facet"     
## [21] "media"          "eta_id"
selected_columns <- df %>%
  select(
    uri,
    url,
    id,
    asset_id,
    source,
    published_date,
    updated,
    section,
    subsection,
    nytdsection,
    title,
    abstract,
    byline,
    published_date,
    section
  )

# Preview the cleaned DataFrame
head(selected_columns)
##                                                      uri
## 1 nyt://interactive/82df359c-4a5d-5f5a-8e6f-84e3711a7485
## 2     nyt://article/657f574b-2765-5a32-a542-75c924ad9ac3
## 3 nyt://interactive/addf9b67-dc9d-5a4e-8f27-2207dae3f4e4
## 4 nyt://interactive/a5e2cc45-d3d9-5e19-bc3e-dc43088f5f1a
## 5     nyt://article/590c765c-dccf-5b10-87da-20da55afa5f8
## 6 nyt://interactive/2d44e623-26b0-521b-95fc-3069ebb0a81a
##                                                                                                 url
## 1        https://www.nytimes.com/interactive/2024/11/05/us/elections/results-arizona-us-senate.html
## 2                           https://www.nytimes.com/2024/11/08/us/politics/trump-musk-zelensky.html
## 3 https://www.nytimes.com/interactive/2024/us/elections/results-timing-presidential-race-calls.html
## 4        https://www.nytimes.com/interactive/2024/11/05/us/elections/results-arizona-president.html
## 5                https://www.nytimes.com/2024/11/06/us/politics/trump-america-election-victory.html
## 6      https://www.nytimes.com/interactive/2024/11/06/us/elections/results-house-races-tracker.html
##      id asset_id         source published_date             updated section
## 1 0e+00    0e+00 New York Times     2024-11-05 2024-11-11 01:31:43    U.S.
## 2 1e+14    1e+14 New York Times     2024-11-08 2024-11-09 11:45:23    U.S.
## 3 1e+14    1e+14 New York Times     2024-11-01 2024-11-05 09:19:05    U.S.
## 4 0e+00    0e+00 New York Times     2024-11-05 2024-11-11 01:31:43    U.S.
## 5 1e+14    1e+14 New York Times     2024-11-06 2024-11-07 03:03:04    U.S.
## 6 1e+14    1e+14 New York Times     2024-11-06 2024-11-11 01:31:44    U.S.
##       subsection nytdsection
## 1 2024 Elections        u.s.
## 2       Politics        u.s.
## 3 2024 Elections        u.s.
## 4 2024 Elections        u.s.
## 5       Politics        u.s.
## 6 2024 Elections        u.s.
##                                                                     title
## 1                                    Arizona U.S. Senate Election Results
## 2                       Trump Put Musk on Phone With Zelensky During Call
## 3      When to Expect Results in Each State on Election Night, and Beyond
## 4                                   Arizona Presidential Election Results
## 5 ‘Trump’s America’: Comeback Victory Signals a Different Kind of Country
## 6                         Path to 218: Tracking the Remaining House Races
##                                                                                                                                                                          abstract
## 1                                                                                                               Get live results and maps from the 2024 Arizona general election.
## 2 Elon Musk was with Donald Trump at Mar-a-Lago when the president-elect spoke with Ukraine’s leader. It is not clear if they discussed any change in U.S. policy toward Ukraine.
## 3                   The Times surveyed election officials in all 50 states to learn how they will count the votes, and when we might know the outcome in the battleground states.
## 4                                                                                                               Get live results and maps from the 2024 Arizona general election.
## 5     In the end, Donald J. Trump is not the historical aberration some thought he was, but instead a transformational force reshaping the modern United States in his own image.
## 6                                                                                     Follow the results in the undecided congressional districts in the race for House control. 
##                                  byline
## 1                                      
## 2                    By Maggie Haberman
## 3 By Alicia Parlapiano and Lazaro Gamio
## 4                                      
## 5                        By Peter Baker
## 6
NYTsection <- selected_columns%>%
  group_by(section)%>%
  summarise(num=n())%>%
  arrange(desc(num))
print(NYTsection)
## # A tibble: 2 × 2
##   section      num
##   <chr>      <int>
## 1 U.S.          19
## 2 The Upshot     1
NYT <- selected_columns %>%
  group_by(section) %>%
  summarise(Count = n()) %>%
  arrange(desc(Count))
ggplot(data = NYT, aes(x = reorder(section, Count), y = Count)) +
  geom_bar(stat = "identity", fill = "orange") +
  coord_flip() +
  labs(
    title = "Distribution of NYT Most Viewed Articles by Section",
    x = "Section",
    y = "Number of Articles"
  ) +
  theme_minimal()

NYT_Publihsed_Date <- selected_columns %>%
  group_by(published_date) %>%
  summarise(num = n()) %>%
  arrange(desc(num))
ggplot(data = NYT_Publihsed_Date, aes(x = published_date, y = num, fill = published_date))+ geom_bar(stat="identity", position="dodge") + ggtitle("Number of Polular Articles Recently Published on Facebook") + ylab("Frequency")+  geom_text(aes(label = num))+
theme(axis.text.x = element_text(angle = 45, hjust = 1))