经过一些测试,我编写了这个包含进度条并从用户那里获取所有关注者 ID 的递归函数:
# Load library
library('rtweet')
# Global variables:
ids <- 75000 # Max number of ids per token every 15 minutes
f <- list() # Vector where user_ids from followers will be appended
# Function to get all the followers from a user with pagination
GetFollowersRecursivePagination <- function(userId, followers, page) {
if (ids == 0) {
# API Twitter Limit reached - Wait
message("Waiting 15 mins...")
total <- 15*60 # Total time = 15 min ~ 900 sec
pb <- txtProgressBar(min = 0, max = total, style = 3) # create progress bar
for (i in 1:total) {
Sys.sleep(time = 1) # 1 second interval
setTxtProgressBar(pb, i) # update progress bar
}
close(pb)
# Check rate limit followers/ids query
if (!rate_limit(token = NULL)[38,]$reset > 14.9) {
message("Waiting 15 seconds more...")
Sys.sleep(time = 15) # wait 15 seconds more...
}
message("Go!")
ids <<- 75000
}
if (followers <= ids) {
message(paste("Followers < ids | Number of Followers: ",
followers, " | Number of resting ids: ", ids, sep = ""))
ftemp <- get_followers(user = userId, n = followers, page = page)
if (page == '-1') {
f <<- append(f, list(ftemp)) # append followers ids
}
if (page != '-1') {
df <- data.frame('user_id' = ftemp)
f <<- append(f, list(df)) # append followers ids
}
ids <<- ids - followers
message("Finished!")
rtemp <- f
f <<- list()
return(rtemp)
} else if (followers > ids) {
message(paste("Followers > ids | Number of Followers: ",
followers, " | Number of resting ids: ", ids, sep = ""))
ftemp <- get_followers(user = userId, n = ids, page = page)
if (page == '-1') {
f <<- append(f, list(ftemp)) # append followers ids
}
if (page != '-1') {
df <- data.frame('user_id' = ftemp)
f <<- append(f, list(df)) # append followers ids
}
n <- ids # n = count of followers ids already acquired
pageTemp <- next_cursor(ftemp) # Pagination
# API Twitter Limit reached - Wait
message("Waiting 15 mins...")
total <- 15*60 # Total time = 15 min ~ 900 sec
pb <- txtProgressBar(min = 0, max = total, style = 3) # create progress bar
for (i in 1:total) {
Sys.sleep(time = 1) # 1 second interval
setTxtProgressBar(pb, i) # update progress bar
}
close(pb)
# Check rate limit followers/ids query
if (!rate_limit(token = NULL)[38,]$reset > 14.9) {
message("Waiting 15 seconds more...")
Sys.sleep(time = 15) # wait 15 seconds more...
}
message("Go!")
ids <<- 75000
# Recursive function call
GetFollowersRecursivePagination(userId = userId,
followers = followers - n,
page = pageTemp)
}
}
# Test ( > 75000 followers )
# Get user
user1 <- lookup_users(users = "146620155")
FAOClimate <- GetFollowersRecursivePagination(userId = user1$user_id,
followers = user1$followers_count,
page = '-1')
# Output:
Followers > ids | Number of Followers: 87208 | Number of resting ids: 75000
Followers < ids | Number of Followers: 12208 | Number of resting ids: 62792
Finished!
str(FAOClimate)
List of 2
$ :'data.frame': 75000 obs. of 1 variable:
..$ user_id: chr [1:75000] "87189802" "884863013928546304" "886995444512964608" "852940633652301824" ...
..- attr(*, "next_cursor")= num 1.45e+18
$ :'data.frame': 12207 obs. of 1 variable:
..$ user_id: chr [1:12207] "2175314977" "2168898233" "1491745484" "2175065456" ...
注意:如果您想要朋友而不是关注者get_followers
,请将功能更改为。get_friends