Snippets

Alexander Hanel @thegrugq curated

Created by Alexander Hanel last modified
# @thegrugq curated

# Execute following command to install package if default install option fails
# install.packages("PACKAGE_NAME", dependencies=TRUE, repos='http://cran.rstudio.com/') 
library(ROAuth)
library(twitteR)
library(httr)
library(methods)

# values are found at https://apps.twitter.com/app/
consumer_key <- "REMOVED"
consumer_secret <- "REMOVED"
access_token <- "REMOVED"
access_secret <- "REMOVED"

setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
local_cache <- get("oauth_token", twitteR:::oauth_cache)

og_tweets = userTimeline("@thegrugq", n=1000, includeRts=TRUE)
tweets_df <- twListToDF(og_tweets)

cur_date <- Sys.Date() - 1

df_date <- as.Date(tweets_df$created)
  
todays_tweets <- tweets_df[with(tweets_df, df_date == cur_date),]

rt = todays_tweets$isRetweet == 1
nt = todays_tweets$isRetweet == 0

today_retweets <- todays_tweets[rt,]
today_tweets <- todays_tweets[nt,]

top_5_rt <- head(today_retweets[order(today_retweets$retweetCount, decreasing=TRUE), ], 5)
top_5_tw <- head(today_tweets[order(today_tweets$retweetCount, decreasing=TRUE), ], 5)

# retweet the top retweets
for (id in top_5_rt$id){ 
  retweet_url <- sprintf("https://api.twitter.com/1.1/statuses/retweet/%s.json", id)
  # twitteR doesn't appear to have a way to retweet. Using 
  req <- POST(retweet_url,config(token = local_cache))
  warn_for_status(req)
}

# retweet the top non-retweets 
for (id in top_5_tw$id){ 
  retweet_url <- sprintf("https://api.twitter.com/1.1/statuses/retweet/%s.json", id)
  req <- POST(retweet_url,config(token = local_cache))
  warn_for_status(req)
}

print("Completed")



Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.