Skip to content

Instantly share code, notes, and snippets.

@marckohlbrugge
Created September 3, 2024 10:44
Show Gist options
  • Save marckohlbrugge/5ea126c18d7ccc493acb02dfc6ab0b11 to your computer and use it in GitHub Desktop.
Save marckohlbrugge/5ea126c18d7ccc493acb02dfc6ab0b11 to your computer and use it in GitHub Desktop.
Calculate "followers per tweet", etc for a list of usernames – https://x.com/marckohlbrugge/status/1830919319573700786
require 'http'
require 'json'
require 'terminal-table'
users = [
{ username: "marckohlbrugge" }
]
# Fetch data for each user
users.each do |user|
username = user[:username]
data = HTTP.get("https://api.fxtwitter.com/#{username}").parse
followers = data.dig('user', 'followers')
tweets = data.dig('user', 'tweets')
followers_per_tweet = (followers.to_f / tweets).round(2)
user.merge!(
followers:,
tweets:,
followers_per_tweet:
)
end
# Sort by follower count (highest first)
sorted_users = users.sort_by { |user| -user[:followers] }
table = Terminal::Table.new do |t|
t.headings = ['Username', 'Followers', 'Tweets', 'Followers per tweet']
sorted_users.each do |user|
t << [
user[:username],
user[:followers],
user[:tweets],
user[:followers_per_tweet]
]
end
end
puts table
# Calculate correlation between tweets and followers
tweets = sorted_users.map { |user| user[:tweets] }
followers = sorted_users.map { |user| user[:followers] }
sum_tweets = tweets.sum
sum_followers = followers.sum
sum_tweets_squared = tweets.map { |t| t**2 }.sum
sum_followers_squared = followers.map { |f| f**2 }.sum
sum_tweets_followers = tweets.zip(followers).map { |t, f| t * f }.sum
n = tweets.length
numerator = (n * sum_tweets_followers) - (sum_tweets * sum_followers)
denominator = Math.sqrt((n * sum_tweets_squared - sum_tweets**2) * (n * sum_followers_squared - sum_followers**2))
correlation = numerator / denominator
puts "\nCorrelation between tweets and followers: #{correlation.round(4)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment