-
-
Save darachm/7a52bb14b4f2e2f784de47c6bd8b9c49 to your computer and use it in GitHub Desktop.
Archive a user's tweets - now plus following/followers
This file contains 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(rtweet) | |
library(tidyverse) | |
# Initial authorization setup, only need to do once | |
# auth_setup_default() #nolint | |
# Authorize | |
auth_as("default") | |
# Set user name | |
user_name <- "PUT USER NAME HERE" | |
# Download all tweets from user (as many as possible anyways) | |
# The docs say it should work for up to 3200 tweets | |
# https://docs.ropensci.org/rtweet/reference/get_timeline.html | |
my_tweets <- get_timeline( | |
user = user_name, | |
n = Inf, | |
retryonratelimit = TRUE) | |
# And save them forever! | |
saveRDS(my_tweets, "my_tweets.RDS") | |
# Extract URLs of media (images etc) | |
# We can map back to the tweet the image came from by `tweet_id`, which | |
# matches `id` of my_tweets | |
media_url <- | |
my_tweets %>% | |
rename(tweet_id = id) %>% | |
mutate(media = map(entities, "media")) %>% | |
select(tweet_id, media) %>% | |
unnest(media) %>% | |
filter(!is.na(id)) %>% | |
mutate( | |
filename = str_match(media_url, "\\/([^\\/]*)$") |> | |
magrittr::extract(, 2) | |
) | |
# Download media to "media" folder | |
walk2( | |
media_url$media_url, | |
media_url$filename, | |
~download.file(.x, glue::glue("media/{.y}"))) | |
# Then some stuff for following/followers | |
# Download all following | |
following <- get_friends( | |
users = user_name, | |
n = Inf, | |
retryonratelimit = TRUE) | |
following | |
# Download all followers | |
followers <- get_followers( | |
user = user_name, | |
n = Inf, | |
retryonratelimit = TRUE) | |
followers | |
# Get a map of id to users for all you had in your network | |
user_ids_interacted_with <- union(following$to_id,followers$from_id) | |
user_map <- lookup_users(user_ids_interacted_with, | |
retryonratelimit=T,verbose=T) | |
user_map | |
# and save that, lotsa details in there | |
saveRDS(user_map, "user_map.RDS") | |
# add on the name/screenname/description and save it | |
following %>% | |
left_join( | |
user_map %>% | |
select(id,name,screen_name,description) %>% | |
rename(to_id=id, | |
following_name=name, | |
following_screen_name=screen_name, | |
following_description=description) %>% | |
mutate(to_id=as.character(to_id)) | |
,by='to_id') %>% | |
saveRDS("my_following.RDS") | |
# add on the name/screenname/description and save it | |
followers %>% | |
left_join( | |
user_map %>% | |
select(id,name,screen_name,description) %>% | |
rename(from_id=id, | |
followers_name=name, | |
followers_screen_name=screen_name, | |
followers_description=description) %>% | |
mutate(from_id=as.character(from_id)) | |
,by='from_id') %>% | |
saveRDS("my_followers.RDS") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment