API: newsAPI

Access to breaking news headlines and articles from over 30,000 news sources and blogs through the newsAPI API.

Thierry Warin https://www.nuance-r.com/principalInvestigator.html (SKEMA Business School (Raleigh, NC))https://www.skemagloballab.io
03-30-2020

Table of Contents


By using SKEMA Quantum Studio framework (Warin 2019), this course will teach you how to use the newsAPI package.

Database description

The package newsAPI is an API wrapper/R client for accessing https://newsapi.org.

API KEY

Go to newsapi.org and register to get an API KEY. Save the key as an environment variable.


# API key (this is a fake API Key copy paste your own)
NEWSAPI_KEY <- "4345e85e8ae1427480xxxxxxxxxxxxxx"

# save your API Key in your environment
cat(
  paste0("NEWSAPI_KEY=", NEWSAPI_KEY),
  append = TRUE,
  fill = TRUE,
  file = file.path("~", ".Renviron")
)

Functions

This package gives access to breaking news headlines, and articles from over 30,000 news sources and blogs.

Each of these functions are detailed in this course and some examples are provided.

get_sources()

This method allows you to search for news sources by language. For example, to find all english language news sources, use the following code:


# invoke library
library(newsAPI)

# get all english language news sources (made available by newsapi.org)
src <- get_sources(language = "en")

# preview data
print(src, width = 500)

get_articles

Once you have identified news source names (IDs) pas them to the get_articles() function. The following code, for example, retrive all articles from the search above.


## apply get_articles function to each news source
df <- lapply(src$id, get_articles)

## collapse into single data frame
df <- do.call("rbind", df)

## preview data
print(df, width = 500)

tl;dr


# invoke library
library(newsAPI)

# get all english language news sources (made available by newsapi.org)
src <- get_sources(language = "en")

# preview data
print(src, width = 500)

## apply get_articles function to each news source
df <- lapply(src$id, get_articles)

## collapse into single data frame
df <- do.call("rbind", df)

## preview data
print(df, width = 500)

Code learned this week

Command Detail
get_sources() Search for news source names (IDs)
get_articles() Get data by news source names (IDs)

References

This course uses the newsAPI package documentation


Warin, Thierry. 2019. “SKEMA Quantum Studio: A Technological Framework for Data Science in Higher Education.” https://doi.org/10.6084/m9.figshare.8204195.v2.

Citation

For attribution, please cite this work as

Warin (2020, March 30). Virtual Campus: API: newsAPI. Retrieved from https://virtualcampus.skemagloballab.io/posts/api-newsAPI/

BibTeX citation

@misc{warin2020api:,
  author = {Warin, Thierry},
  title = {Virtual Campus: API: newsAPI},
  url = {https://virtualcampus.skemagloballab.io/posts/api-newsAPI/},
  year = {2020}
}