[API] newsAPI

API & Databases R Courses

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

Thierry Warin https://warin.ca/aboutme.html (HEC Montréal and CIRANO (Canada))https://www.hec.ca/en/profs/thierry.warin.html
03-30-2020

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 tutorial uses the newsAPI package documentation


Citation

For attribution, please cite this work as

Warin (2020, March 30). Thierry Warin, PhD: [API] newsAPI. Retrieved from https://warin.ca/posts/api-newsAPI/

BibTeX citation

@misc{warin2020[api],
  author = {Warin, Thierry},
  title = {Thierry Warin, PhD: [API] newsAPI},
  url = {https://warin.ca/posts/api-newsAPI/},
  year = {2020}
}