Created
May 11, 2021 13:09
-
-
Save jeremy-allen/b7201667ad5f15ff4736e29adf777eb9 to your computer and use it in GitHub Desktop.
Delete all tweets older than 30 days unless they have certain hashtags
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(here) | |
library(rtweet) | |
library(tidyverse) | |
library(assertthat) | |
#---- prep ---- | |
my_dir <- here() | |
# read in vector of 'keeper' hashtags we will use to filter | |
data_tags <- read_lines("hashtags_to_keep.txt") | |
date_cutoff <- Sys.Date() - 30 | |
tweets <- get_timeline( | |
"jeremy_data", | |
n = 10000 | |
) | |
# ids of tweets to keep | |
data_related_ids <- tweets %>% | |
select(status_id, hashtags) %>% | |
filter(!is.na(hashtags)) %>% # must have a hashtag | |
unnest(hashtags) %>% | |
mutate(hashtags = str_to_lower(hashtags)) %>% | |
filter(hashtags %in% data_tags) %>% # must be a data-related hashtag | |
unique() %>% | |
pull(status_id) | |
ids_to_delete <- tweets %>% | |
select(status_id, created_at) %>% | |
filter(as.Date(created_at) < as.Date(date_cutoff)) %>% # are older than 30 days | |
filter(!status_id %in% data_related_ids) %>% # are not data-related | |
pull(status_id) | |
total <- length(ids_to_delete) | |
# confirm that tweets to delete really are older than 30 days | |
assert_that( | |
tweets %>% filter(status_id == sample(ids_to_delete, 1)) %>% pull(created_at) %>% | |
as.Date() < date_cutoff | |
) | |
#---- work ---- | |
for (i in seq_along(ids_to_delete)) { | |
skip_to_next <- FALSE | |
Sys.sleep(.5) | |
message("... ", i, " of ", total, " ... trying to delete ", ids_to_delete[i]) | |
tryCatch( | |
expr = post_tweet("", destroy_id = ids_to_delete[i]), # non-intuitive post request to twitter api | |
error = function(e) { | |
message("... Destroy seems to have failed for ", i, ":\n") | |
write(x = paste0(ids_to_delete[i], "\n", e), | |
file = paste0(my_dir, "/log.txt"), | |
append = TRUE) | |
skip_to_next <<- TRUE | |
} | |
) # end tryCatch | |
if(skip_to_next) next | |
} # end loop | |
#---- rest ---- | |
closeAllConnections() | |
cat("Enjoy!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment