Chapter 8 User follows
Chapter progress bar
███████████████████████████░░░ 90%
There are different ways to get the users that a given user follows.
If we are interested in getting only the Twitter user_id
of each user out entry point user follows we can use this API endpoint (at this time a corresponding v2 version is not available as far as I know)
<-
url "https://api.twitter.com/1.1/friends/ids.json"
and this parameters
<-
entry_point_user_id '2962620002'
<-
params list(user_id = entry_point_user_id,
count = 5000)
The only required parameter is the user_id
or alternatively screen_name
.
We store the JSON-formatted results into a data folder which we set with
<-
json_data_dir "json_data"
if(!dir.exists(json_data_dir)) {
dir.create(json_data_dir)
}
We are now ready to request a paginate the results (in case we our entry point user follows more than 5,000 users).
<-
headers c(`Authorization` = sprintf('Bearer %s',
Sys.getenv("BEARER_TOKEN")))
<-
res ::GET(url,
httr::add_headers(.headers = headers),
httrquery = params)
<-
obj.r ::content(res, as = "text") %>%
httr::fromJSON()
jsonlite
::write_json(httr::content(res, as = "parsed"),
jsonlitepath = sprintf("%s/%s_follows_ids_%s.json",
json_data_dir,
entry_point_user_id,sprintf("%04d", 1)))
Do we have additional pages?
if (obj.r$next_cursor != 0) {
<- 2
counter
while(TRUE) {
'cursor']] <-
params[[$next_cursor_str
obj.r
print(sprintf("Next cursor: %s...", obj.r$next_cursor_str))
<-
res ::GET(url,
httr::add_headers(.headers = headers),
httrquery = params)
<-
obj.r ::content(res, as = "text") %>%
httr::fromJSON()
jsonlite
if (!is.null(obj.r$error) && obj.r$error['code'] == 88) {
while(TRUE) {
print(obj.r$errors)
Sys.sleep(60)
<-
res ::GET(url,
httr::add_headers(.headers = headers),
httrquery = params)
<-
obj.r ::content(res, as = "text") %>%
httr::fromJSON()
jsonlite
if (is.null(obj.r$errors)) {
break
}
}
}
::write_json(httr::content(res, as = "parsed"),
jsonlitepath = sprintf("%s/%s_follows_ids_%s.json",
json_data_dir,
entry_point_user_id,sprintf("%04d", counter)))
if (obj.r$next_cursor == 0) {
break
}
<-
counter + 1
counter
}
}