The goal of mediacloudr is to provide a consistent wrapper for the mediacloud.org API. The Media Cloud platform is an open-source platform, that collects all kind of news stories and provides various functionalities to query, download and analyze them. This packages tries to support R users by providing a set of functions to access various functionalities of the mediacloud.org.
And the development version from GitHub with:
# install.packages("devtools")
::install_github("jandix/mediacloudr") devtools
Please register as a new user. Afterwards, you can copy your API key from your profile page.
I suggest to save the API key to your R environment file. The R environment file is loaded every time R is started/restarted. You should not add the key to your scripts, because other users could misuse your key. The following steps show how to add the key to your R environment file.
.Renviron
.MEDIACLOUD_API_KEY=<YOUR_API_KEY>
.The mediacloud.org states the following for API request/rate limits:
“Each user is limited to 1,000 API calls and 20,000 stories returned in any 7 day period. Requests submitted beyond this limit will result in a status 403 error. Users who need access to more requests should email info@mediacloud.org.”
You can query news stories by their ids. The ids can be found using
the graphical interface or using the get_story_list
function.
Note: You don’t have to add the api_key
argument if you followed the steps to add the api key to your R
environment file.
<- 27456565L
story_id <- get_story(story_id = story_id) story
You can query a list of news stories using
get_story_list
. You can use the q
and
fq
arguments to filter stories. A guide to the query
parameters can be found here.
Note: You don’t have to add the api_key
argument if you followed the steps to add the api key to your R
environment file.
<- get_story_list(q = "trump") stories
You can query media sources by their ids. The ids can be found using the graphical online interface. mediacloud.org provides various meta data for their media sources.
Note: You don’t have to add the api_key
argument if you followed the steps to add the api key to your R
environment file.
<- 1L
media_id <- get_media_source(media_id = media_id) media_source
You can use the article URL to download the complete article and extract social meta data. The meta data can be analyzed using techniques such as sentiment analysis or simply compared to the article content.
# load httr
library(httr)
# define article url
<- "https://bits.blogs.nytimes.com/2013/04/07/the-potential-and-the-risks-of-data-science"
url # download article
<- GET(url)
response # extract article html
<- content(response, type = "text", encoding = "UTF-8")
html_document # extract meta data from html document
<- extract_meta_data(html_doc = html_document) meta_data