-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_news.R
53 lines (46 loc) · 1.65 KB
/
get_news.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
#' Get news
#' Get the contents of a rss feed
#'
#' @param website a url of a new source in the format "news.ycombinator.com"
#' @param topic the topic of the feed, by default it is NULL which means it
#' will fetch the "main" feed. topics are 'tech', 'news', 'business', 'science',
#' 'finance', 'food', 'politics', 'economics', 'travel', 'entertainment',
#' 'music', 'sport', 'world', but not all site have all topics.
#' use \code{describe_url("website")} to check for available feeds.
#' @param rss_table a dataframe with urls and rss feeds in case you
#' need to construct your own out of websites not in the included database.
#' Be sure to have the same format as the included data. See \code{?package_rss}
#' for details.
#'
#' @return a tibble containing the contents of the rss feed
#'
#' @export
#'
#' @examples
#' \dontrun{
#' Sys.sleep(3) # adding a small time delay to avoid
#' # simultaneous posts to the API
#' get_news(website = "ycombinator.com", rss_table = package_rss)
#' }
get_news <- function(website = "ycombinator.com", topic = NULL, rss_table = package_rss) {
check_url(website, rss_table)
news_source <- rss_table[rss_table$clean_url == website,]
if (is.null(topic)) {
news_source <- news_source[news_source$main == 1,]
} else if (!any(topic %in% news_source$topic_unified)) {
message(
paste(
"Website:",
website,
"does not have a feed for the specified topic.",
sep = " "
)
)
describe_url("bbc.com")
return(invisible(NULL))
} else {
news_source <- news_source[news_source$topic_unified == topic,]
}
feed_entries <- tidyfeed(news_source$rss_url)
return(feed_entries)
}