This repository has been archived by the owner on Nov 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathretweet_nflverse.R
91 lines (72 loc) · 2.59 KB
/
retweet_nflverse.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
suppressPackageStartupMessages({
library(rtweet)
library(dplyr, warn.conflicts = FALSE)
library(purrr, warn.conflicts = FALSE)
library(tidyr, warn.conflicts = FALSE)
})
#' search_tweets() FOR #nflverse WHERE status_id > status_from_last_run EVERY fifteen minutes
#'
#' apply cleaning:
#' a) do not retweet where more than 3 hashtags are used
#' b) do not retweet more than one message from the same author (author vatiable is missing?)
# TODO maintain a list of banned/blocked users
# TODO remove swearing and/or not-pg tweets? (censor it)
api_key <- Sys.getenv("TWITTERAPIKEY")
api_secret <- Sys.getenv("TWITTERAPISECRET")
access_token <- Sys.getenv("TWITTERACCESSTOKEN")
access_secret <- Sys.getenv("TWITTERACCESSTOKENSECRET")
bot <- rtweet::rtweet_bot(
api_key = api_key,
api_secret = api_secret,
access_token = access_token,
access_secret = access_secret
)
rtweet::auth_as(bot)
last_id_saved <- readLines("last_id.txt")
retweet_nflverse <- function(last_id_saved){
message(paste("Starting retweet job:",Sys.time()))
blocked_ids <- readLines("blocked_ids.txt")
tweets_raw <- rtweet::search_tweets(
q = "#nflverse",
n = 1000,
include_rts = FALSE,
parse = FALSE,
since_id = last_id_saved,
retryonratelimit = TRUE
)
tweets_filtered <- tweets_raw |>
purrr::pluck(1,"statuses")
if(is.null(tweets_filtered)) return("No tweets to retweet")
tweets_filtered <- tweets_filtered |>
tidyr::unpack(user,names_sep = "_") |>
tidyr::unpack(entities) |>
dplyr::filter(
purrr::map_lgl(hashtags, ~nrow(.x) <= 3),
!user_id_str %in% blocked_ids
) |>
dplyr::select(
created_at,
user_id = user_id_str,
screen_name = user_screen_name,
status_id = id_str,
full_text
) |>
dplyr::group_by(user_id) |>
dplyr::slice_min(order_by = status_id, n = 3) |>
dplyr::ungroup()
writeLines(max(tweets_filtered$status_id), "last_id.txt")
if(nrow(tweets_filtered) == 0) return("No tweets to retweet")
write.table(tweets_filtered,
file = paste0("data/",Sys.Date(),".csv"),
qmethod = "double",
row.names = FALSE,
append = file.exists(paste0("data/",Sys.Date(),".csv")),
col.names = !file.exists(paste0("data/",Sys.Date(),".csv")),
sep = ",")
r <- purrr::map(tweets_filtered$status_id,
purrr::possibly(~rtweet::post_tweet(retweet_id = .x),
otherwise = NA))
message(paste(nrow(tweets_filtered), "#nflverse messages retweeted! --", Sys.time()))
return(NULL)
}
retweet_nflverse(last_id_saved)