For a deep learning project, I needed to create a dataset of profile images and decided to use Twitter profile images of users I am following.

For today's daily coding exercise, doing this with the rtweet package is easy and quick using the below code!

# Download profile images from twitter for a deep learning project


## load rtweet package

twitter_handle <- "tweetsks"

friends <- get_friends(twitter_handle, n = 75000)

## view merged ddata

for (user_id in friends$user_id){

  ## GOOD: combine user id and screen name using list()
  user <- as_userid(user_id)
  twitter_user <- lookup_users(user)
  screen_name <- twitter_user$screen_name
  profile_image <- twitter_user$profile_image_url

  segments <- stringr::str_split_fixed(profile_image, "_normal", n=Inf)

  profile_image_original <- str_flatten(segments)
  download.file(profile_image_original, destfile = paste0("./data/twitter/",screen_name,".png"), method = "wget", extra = "-r -p --random-wait")

That small script gets me pictures of ~2000 profiles I am following to feed into my deep learning project!