Assignment – Web APIs

The New York Times web site provides a rich set of APIs, as described here: https://developer.nytimes.com/apis You’ll need to start by signing up for an API key. Your task is to choose one of the New York Times APIs, construct an interface in R to read in the JSON data, and transform it into an R DataFrame.

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.4 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.0      ✔ stringr 1.4.1 
## ✔ readr   2.1.2      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(jsonlite)
## 
## Attaching package: 'jsonlite'
## 
## The following object is masked from 'package:purrr':
## 
##     flatten
library(httr)

Get API

api_search <- "https://api.nytimes.com/svc/search/v2/articlesearch.json?q=election&api-key="
r <- GET(paste0(api_search, api_key))
status_code(r)
## [1] 200

Retrieve API

data <- content(r, as="text")
newdata <- fromJSON(data, flatten=TRUE) %>% data.frame()

colnames(newdata)
##  [1] "status"                               
##  [2] "copyright"                            
##  [3] "response.docs.abstract"               
##  [4] "response.docs.web_url"                
##  [5] "response.docs.snippet"                
##  [6] "response.docs.lead_paragraph"         
##  [7] "response.docs.source"                 
##  [8] "response.docs.multimedia"             
##  [9] "response.docs.keywords"               
## [10] "response.docs.pub_date"               
## [11] "response.docs.document_type"          
## [12] "response.docs.news_desk"              
## [13] "response.docs.section_name"           
## [14] "response.docs.type_of_material"       
## [15] "response.docs._id"                    
## [16] "response.docs.word_count"             
## [17] "response.docs.uri"                    
## [18] "response.docs.print_section"          
## [19] "response.docs.print_page"             
## [20] "response.docs.subsection_name"        
## [21] "response.docs.headline.main"          
## [22] "response.docs.headline.kicker"        
## [23] "response.docs.headline.content_kicker"
## [24] "response.docs.headline.print_headline"
## [25] "response.docs.headline.name"          
## [26] "response.docs.headline.seo"           
## [27] "response.docs.headline.sub"           
## [28] "response.docs.byline.original"        
## [29] "response.docs.byline.person"          
## [30] "response.docs.byline.organization"    
## [31] "response.meta.hits"                   
## [32] "response.meta.offset"                 
## [33] "response.meta.time"

Tidy Data

df_news <- newdata %>%
select(response.docs.headline.main, response.meta.hits, response.docs.web_url,response.docs.pub_date)
df_news %>%
rename("Headline" = 1, "Hits" = 2, "URL" = 3, "Date" = 4,)
##                                                                     Headline
## 1                        ‘The Run-Up’ Podcast Explains the Midterm Elections
## 2            European Election Observers Warn of Republican Election Deniers
## 3                                     Why Does Israel Keep Having Elections?
## 4              Suburban Women, No Longer ‘Soccer Moms,’ Hold Key to Midterms
## 5          Fueled by Billionaires, Political Spending Shatters Records Again
## 6                J.D. Vance’s Ambition Comes at a Price in ‘Hillbilly’ Terms
## 7        Republicans Target a Top House Democrat as Winds Shift to the Right
## 8         Campaign Press Aides Move From the Shadows to Star on Social Media
## 9  First Kansas, Next Michigan and Beyond as Abortion Ballot Measures Spread
## 10   In the California Desert, L.G.B.T.Q. Voters Could Sway a Key House Race
##      Hits
## 1  924536
## 2  924536
## 3  924536
## 4  924536
## 5  924536
## 6  924536
## 7  924536
## 8  924536
## 9  924536
## 10 924536
##                                                                                          URL
## 1                  https://www.nytimes.com/2022/11/05/podcasts/run-up-midterm-elections.html
## 2          https://www.nytimes.com/2022/10/27/us/politics/midterms-osce-election-voting.html
## 3         https://www.nytimes.com/2022/11/01/world/middleeast/israel-why-many-elections.html
## 4                         https://www.nytimes.com/2022/11/04/us/suburban-women-midterms.html
## 5             https://www.nytimes.com/2022/11/03/us/politics/midterm-money-billionaires.html
## 6                    https://www.nytimes.com/2022/10/27/us/politics/jd-vance-trump-ohio.html
## 7          https://www.nytimes.com/2022/10/26/us/politics/sean-patrick-maloney-new-york.html
## 8      https://www.nytimes.com/2022/10/26/us/politics/campaign-press-aides-social-media.html
## 9           https://www.nytimes.com/2022/09/09/us/politics/michigan-abortion-referendum.html
## 10 https://www.nytimes.com/2022/09/03/us/politics/lgbtq-voters-ken-calvert-will-rollins.html
##                        Date
## 1  2022-11-05T11:00:07+0000
## 2  2022-10-27T22:50:52+0000
## 3  2022-11-01T07:01:23+0000
## 4  2022-11-04T15:29:18+0000
## 5  2022-11-03T17:37:05+0000
## 6  2022-10-27T09:00:33+0000
## 7  2022-10-26T19:01:48+0000
## 8  2022-10-26T09:00:18+0000
## 9  2022-09-09T22:54:36+0000
## 10 2022-09-03T19:00:12+0000

Odd observation that they all had the same number of hits