-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_functions.R
110 lines (95 loc) · 3.76 KB
/
api_functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
library(httr)
library(dplyr)
library(data.table)
library(lubridate)
library(shinydashboard)
library(shiny)
library(stringr)
library(DT)
library(jpeg)
library(RCurl)
key <- "2272f67e000a4034b63e3e2dda03dbfa"
key_str <- paste0("&apiKey=", key)
base_url <- "https://newsapi.org/v2/"
get_sources <- function(category, api_key) {
if(api_key == "") {
print("API Key required! Please enter on sidebar.")
return()
}
categories <- c("business", "entertainment", "general", "health", "science", "sports", "technology")
stopifnot(tolower(category) %in% categories)
url <- paste0(base_url, "/sources?country=us&language=en",
"&category=", category,
"&language=en&country=us",
"&apiKey=", api_key)
request <- content(GET(url), "parsed")$sources
source_info <- rbindlist(request, fill = TRUE) %>%
as_tibble %>%
select(-id, -category, -language, -country) %>%
rename(Source = name, Description = description)
return(source_info)
}
get_headlines <- function(sources, q, api_key, page_size = 20, page = 1, verbose = F) {
stopifnot(page_size > 0, page_size <= 100)
#https://stackoverflow.com/questions/6347356/creating-a-comma-separated-vector
sources <- str_replace_all(sources, " ", "")
if(length(sources) > 1) {
sources <- paste(shQuote(sources, type="cmd2"), collapse=",")
}
url <- paste0(base_url, "top-headlines?",
"sources=", sources,
"&q=", q,
"&pageSize=", page_size,
"&page=", page,
"&apiKey=", api_key)
if(verbose) {print(paste("URL:", url))}
request <- content(GET(url), "parsed")$articles
if(!length(request)) {
print("No articles matching these criteria!")
return()
}
source_info <- as_tibble(rbindlist(request, fill = TRUE))
# https://stackoverflow.com/questions/13461829/select-every-other-element-from-a-vector
ids <- source_info$source[c(T, F)] %>% unlist %>% rep(each = 2)
names <- source_info$source[c(F, T)] %>% unlist %>% rep(each = 2)
source_info <- source_info %>% mutate(source_id = ids,
source_name = names) %>%
select(-source, -source_id, -content) %>%
select(source_name, everything())
source_info <- source_info[!duplicated(source_info),]
return(source_info)
}
get_historic <- function(q, q_title, sources, from, to, api_key, sort_by = "publishedAt", page_size = 20, page = 1, verbose = F) {
from <- as.Date(from)
to <- as.Date(to)
sort_methods <- c("relevancy", "popularity", "publishedAt")
# https://stackoverflow.com/questions/14169620/add-a-month-to-a-date/14169749
stopifnot(length(sources) <= 20, to >= from, from >= Sys.Date() %m-% months(1) ,
sort_by %in% sort_methods, page_size >= 0, page_size <= 100)
sources <- str_replace_all(sources, " ", "")
if(length(sources) > 1) {
sources <- paste(shQuote(sources, type="cmd2"), collapse=",")
}
url <- paste0(base_url, "everything?",
"q=", q,
"&qInTitle=", q_title,
"&sources=", sources,
"&from=", from, "&to=", to,
"&sortBy=", sort_by,
"&pageSize=", page_size,
"&page=", page,
"&apiKey=", api_key)
if(verbose) {print(paste("URL:", url))}
request <- content(GET(url), "parsed")$articles
if(!length(request)) {
print("No articles matching these criteria!")
return()
}
source_info <- as_tibble(rbindlist(request, fill = TRUE))
names <- source_info$source[c(F, T)] %>% unlist %>% rep(each = 2)
source_info <- source_info %>% mutate(source_name = names) %>%
select(-source) %>%
select(source_name, everything())
source_info <- source_info[!duplicated(source_info),]
return(source_info)
}