Created
July 24, 2017 07:33
-
-
Save maelle/1bb7bd1e3955b1f150ac53f640a9eea6 to your computer and use it in GitHub Desktop.
Waiting + saving intermediary results in order to be able to re-launch if script stopped
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
get_followers_and_wait <- function(user){ | |
# don't re-query the same data twice | |
exists <- paste0("followers", user, ".RData") %in% dir("data/2017-06-03-twitter") | |
if(exists){ | |
# if it already exists use it | |
load(paste0("data/2017-06-03-twitter/followers", user, ".RData")) | |
}else{ | |
# otherwise query after checking the rate limit | |
rates <- rtweet::rate_limit(token = rtweet::get_tokens(), | |
query = "followers/ids") | |
if(rates$remaining == 0){ | |
try_sleeping <- try(Sys.sleep(as.numeric(rates$reset)*60), | |
silent = TRUE) | |
if(methods::is(try_sleeping, "try-error")){ | |
Sys.sleep(15*60) | |
} | |
} | |
followers <- rtweet::get_followers(user = user) | |
followers <- dplyr::mutate_(followers, followed_person = ~user) | |
save(followers, file = paste0("data/2017-06-03-twitter/followers", user, ".RData")) | |
} | |
return(followers) | |
} | |
followers_followers <- purrr::map_df(followers$user_id, | |
get_followers_and_wait) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment