/
get_tweets.R
74 lines (62 loc) · 1.51 KB
/
get_tweets.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
#' Get tweet number
#'
#' @param buffer Numeric. The number to add to the last number of tweets pulled in such that we always ask for more tweets than necessary the next time the script is run.
#'
#' @export
#' @importFrom dplyr %>%
#'
get_tweet_number <- function(buffer = 100) {
last_val <-
readr::read_lines(n_tweets_path) %>%
as.numeric()
last_val + buffer
}
#' Save tweet number
#'
#' @param val the vector of tweet numbers
#'
#' @export
#'
save_tweet_number <- function(val) {
readr::write_lines(val, n_tweets_path)
}
#' Get Tweets
#'
#' @param save_number Whether to save the numbers of the tweets or not
#' @export
get_tweets <- function(save_number = TRUE) {
n_tweets_to_grab <-
get_tweet_number()
tbl <- rtweet::get_timeline("RLangTip", n = n_tweets_to_grab)
if (save_number) {
n_tweets <- nrow(tbl)
save_tweet_number(n_tweets)
}
tbl %>%
select(status_id, created_at, text, favorite_count, retweet_count)
}
utils::globalVariables(c("status_id", "created_at", "text"))
#' Add #rstats
#'
#' @param tbl Tip text.
#' @param col Column to add hashtags to.
#'
#' @export
#'
#' @importFrom dplyr enquo case_when
#' @examples
#'
#' tibble::tibble(text = "foo") %>% add_rstats_hashtag(text)
#' tibble::tibble(text = "foo #rstats") %>% add_rstats_hashtag(text)
#'
add_rstats_hashtag <- function(tbl, col) {
q_col <- enquo(col)
tbl %>%
mutate(
text =
case_when(
! stringr::str_detect(!!q_col, "#rstats") ~ !!q_col %>% stringr::str_c(" #rstats"),
TRUE ~ text
)
)
}