Skip to content

Instantly share code, notes, and snippets.

@charlieegan3
Created October 8, 2014 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlieegan3/4007feab77a7ae89685d to your computer and use it in GitHub Desktop.
Save charlieegan3/4007feab77a7ae89685d to your computer and use it in GitHub Desktop.
Data on Twitter Subscriptions
require 'twitter'
TWITTER_CONSUMER_KEY = 'xxxx'
TWITTER_CONSUMER_SECRET = 'xxxx'
USERNAME = 'username'
FRIEND_COUNT = 200 #Max 200, I think.
TWEET_COUNT = 50
START_INDEX = 68 #if you hit the rate limit start where you left off
twitter_client = Twitter::REST::Client.new do |config|
config.consumer_key = TWITTER_CONSUMER_KEY
config.consumer_secret = TWITTER_CONSUMER_SECRET
end
puts "username,%replies,%retweets,%media,%contain links,average length,tweets per day"
friends = twitter_client.friends(USERNAME, { count: FRIEND_COUNT }).each.to_a
friends[START_INDEX,friends.size].each do |friend|
begin
tweets = twitter_client.user_timeline(friend.screen_name, { count: TWEET_COUNT })
rescue Exception => e
puts e.message
puts "Wait 15 mins or so."
exit
end
breakdown = { replies: 0, retweets: 0, media: 0, links: 0, text_length: 0 }
tweets.each do |tweet|
breakdown[:replies] += 1 if tweet.reply?
breakdown[:retweets] += 1 if tweet.retweet?
breakdown[:media] += 1 if tweet.media?
breakdown[:links] += 1 if tweet.urls?
breakdown[:text_length] += tweet.text.length
end
breakdown[:replies] = ((breakdown[:replies].to_f / tweets.size) * 100).round(3)
breakdown[:retweets] = ((breakdown[:retweets].to_f / tweets.size) * 100).round(3)
breakdown[:media] = ((breakdown[:media].to_f / tweets.size) * 100).round(3)
breakdown[:links] = ((breakdown[:links].to_f / tweets.size) * 100).round(3)
breakdown[:text_length] = breakdown[:text_length] / tweets.size
days = (Time.new - tweets.last.created_at) / 60 / 60 / 24
tweets_per_day = (tweets.size.to_f / days).round(3)
puts "#{friend.screen_name},#{breakdown[:replies]},#{breakdown[:retweets]},#{breakdown[:media]},#{breakdown[:links]},#{breakdown[:text_length]},#{tweets_per_day}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment