-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch_friends.R
44 lines (33 loc) · 1.67 KB
/
fetch_friends.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# This script fetches the "following" list (called "friends" in Twitter API terminology) of each deputy
# Twitter profile using the `rtweet` package; because of Twitter API's rate limiting, this takes quite
# some time; saves the result – consisting of Twitter user IDs – in `data/deputies_twitter_friends_tmp.RDS`.
#
# December 2018, Markus Konrad <markus.konrad@wzb.eu>
#
library(dplyr)
library(rtweet)
source('twitterkeys.R') # contains access tokens for Twitter API
# ---- load deputies data with their Twitter handle ----
dep <- read.csv('data/deputies_twitter_20190702.csv',
stringsAsFactors = FALSE,
colClasses = c(personal.location.postal_code = "character"))
twitternames <- filter(dep, !is.na(twitter_name)) %>% pull(twitter_name) %>% unique()
n_twitternames <- length(twitternames)
# ---- fetch list of Twitter friends ----
# get authentication token for Twitter API
token <- create_token(
app = twitter_app,
consumer_key = consumer_key,
consumer_secret = consumer_secret,
access_token = access_token,
access_secret = access_secret)
print(paste('will fetch list of friends for', n_twitternames, 'users'))
# this will return a data frame consisting of column "user" (the deputy Twitter handle) and
# "user_id", which is a Twitter user ID of a friend of the respective "user"
# we will need to use "lookup_users()" in order to get information such as the Twitter name
# about the friends user IDs
friends <- get_friends(twitternames, retryonratelimit = TRUE)
friends$fetch_friends_timestamp <- Sys.time() # add a timestamp for the collection time
head(friends)
# save to file
saveRDS(friends, 'data/deputies_twitter_friends_tmp_20190702.RDS')