Skip to content

Instantly share code, notes, and snippets.

@stephlocke
Last active February 16, 2022 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephlocke/c62cd57e77103cbfb6d40b2bf9760605 to your computer and use it in GitHub Desktop.
Save stephlocke/c62cd57e77103cbfb6d40b2bf9760605 to your computer and use it in GitHub Desktop.
A gist demonstrating how to retrieve the people you follow, assign them to lists, and unfollow in batches
library(rtweet) #' uses v0.7.0.9012 or higher
library(tidyverse)
## authenticate via web browser
auth_setup_default()
## people i follow
follow <- get_friends("theStephLocke")
## their user data
follow_deets <- lookup_users(follow$to_id)
## start inferring some potential lists
follow_deets %>%
select(screen_name, last_tweet=text, description, profile_expanded_url) %>%
mutate(data = str_detect(tolower(last_tweet), "data|sql|dataOps")) ->
initial_categorisation
# dump to csv for manual review
write_csv(initial_categorisation, "follows.csv")
# read dataset back in after manual amends
follow_categorised<-read_csv("~/follows1.csv")
# create lists members
follow_categorised %>%
filter(data > 0) %>%
pull(screen_name) ->
data_list
# generate lists
post_list( data_list, name = "Data folks",
description = "People working primarily with data inc #SQLServer and #PowerBI")
# work around rate limits and flakiness of the endpoint by dumping to csv for manual c&P in tweetdeck
spit = function(x) {
d = get(x)
n = 1+(length(d) %/% 100)
for (i in 1:n) {
acc = paste0("@",d[((i-1)*100):(i*100)])
acc = acc[!is.na(acc)]
write_lines(acc, paste0(x,i,".csv"))
}
}
spit(data_list)
# perform an unfollow to leave family etc in main timeline
unfollow = function(x) {
n=50
i=0
while(i*n<=length(x)){
print(i)
to_uf=x[((i*n)+1):((i+1)*n)]
print(to_uf)
lapply(to_uf, post_unfollow_user)
i=i+1
Sys.sleep(15*60)
}
}
# get all to unfollow
follow_categorised %>%
filter(is.na(stay_follow)) %>%
pull(screen_name) %>%
unfollow()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment