Skip to content

Instantly share code, notes, and snippets.

@Andrewglass1
Created March 16, 2012 17:24
Show Gist options
  • Save Andrewglass1/2051261 to your computer and use it in GitHub Desktop.
Save Andrewglass1/2051261 to your computer and use it in GitHub Desktop.
klout
#Iteration 5
# Once we have pulled our array of followers from Twitter, we can push the data through another API to grab some data. Lets tap into Klout.com- a site that scores the influence of one's social media presence- to see which of our followers are the most infleuncial
#Step 0: requirements
# This iteration requires a rubygem- 'klout' along with an API key
require 'klout'
@k = Klout::API.new('w7cd26nt27pzgy4gj7waruw2') #, {:format => 'xml', :secure => true})
#step 1- Klout around
# lets loop through our list of friends to find everyone's klout score. create a hash to store this information.
# for your friend list from iteration 2, take each user name and apply the method 'klout' to retreive the klout score
# you'll notice when we run @k.klout(friend), score comes back in an odd format
#heres what we get back:
# {"status"=>200, "users"=>[{"twitter_screen_name"=>"mikec", "kscore"=>48.38}]}
#whoah! Lets break this down. This element is a hash with 2 keys: 'status' and 'users'
# the key to users holds our data, so we apply ["users"] to extract it but you'll notice it is a array of hashes(with only one element)
# apply [0] to grab the first (and only) hash in that array. Now we can apply ["kscore"] to grab the score
# each score (the hash's value) is put into an array with its corresponding user name (the hash's key)
def klout_around
@klouthash = Hash.new
friends = @client.friends
#make a manual array to test this out
#friends= ["mikec","melanieeeg"]
friends.each do |friend|
@klouthash[friend] = @k.klout(friend)["users"][0]["kscore"]
end
end
# step 2- printing and sorting
# now lets try to take a look at the user names in an order where we can see who is the most influencial
# Lets take our hash and sort it by the value. note we must do this in reverse so the highest scores are printed first
def print_klouts
klout_around
@klouthash = @klouthash.sort_by {|key, value| value}.reverse
@klouthash.each{|key, value| puts "#{key} has a clout score #{value}"}
end
# step 3- set a threshold
#great! now lets let the user set a threshold to determine what should be deemed 'popular'
#first we run our 'klout_around' method to take everyones klout score
# prompt the user to input a threshold number.
# next, take each user name and test if their score is above that threshold. If it is, lets put in in out newly created hash, '@popularkids'
#finally loop through each user in @popularkids and print it's value
def find_poppies
klout_around
puts "enter a min klout score to judge popularity"
minscore = gets.to_i
@popularkids = Hash.new
@klouthash.each do |key, value|
if value > minscore
@popularkids[key] = value
end
end
@popularkids.each{|key, value| puts "#{key} is popular with a #{value}"}
end
#step 4- spam the influencers!
#ok, now we've identified who we consider to be influencial. What should we do next.... SPAM them
# first, lets run 'find_poppies' to see who is influencial
# next, print the number of 'popular' followers to the user and ask them if they'd like to spam them
#if they do, ask what message should be sent
#then for each follower, send them the message
def spam_populars
find_poppies
puts "you follow #{@popularkids.length} popular kids. type 'yes' to spam them?"
answer = gets.chomp
if answer == "yes"
puts "what do you want to spam them with?"
message = gets.chomp
@popularkids.each do |follower|
follower = follower[0]
dm(follower, message)
end
else
"you are kind"
end
end
@Andrewglass1
Copy link
Author

Let's continue experimenting with various APIs.

Klout (klout.com) is a service that, "measures influence on social networks." The Klout service analyzes a variety of networks to determine an individual's "Klout Score." Measured from 0 to 100, a higher Klout Score indicates greater influence on various social networks.

Klout offers a freem public API that lets you retrieve Klout scores by simply providing a Twitter username. Let's attempt to retrieve the Klout Score of all the users we follow and then rank them by their influence.

Step 1 - Install Klout Gem

First, hop into Terminal and run this line:

gem install klout

Next, open irb so we can experiment with the Klout API:

k = Klout::API.new('6f2zva63qwtan3hgwvesa7b8')
k.klout("jack")
=>{"status"=>200, "users"=>[{"twitter_screen_name"=>"jack", "kscore"=>74.61}]}

This attempts to return the Klout score for username "jack" (this is Twitter's co-founder). This return value contains the data we want, "74.61", but it's slightly obfuscated.
Let's take a deeper look at was returned to extract the data we want. The command returned a hash. to retrieve the "users" section of that hash which contains the Klout score, we command
k.klout("jack")["users"]
=>[{"twitter_screen_name"=>"jack", "kscore"=>74.61}]
this returned the 'value' for the key 'users'.
That value happens to be an array. This array is unique in that it holds one element (arrays are capable of storing many elements). Although the array only has one element, we must still extract that element to call methods upon it and retrieve the Klout score. Lets call [0] to take the first(and in this case, only) element of the array.
k.klout("jack")["users"][0]
=>{"twitter_screen_name"=>"jack", "kscore"=>74.61}
Great! Now we are left with only the hash we need. To pull the kscore, we simply add ["kscore"] to call the value for the kscore key
k.klout("jack")["users"]["kscore"]
=> 74.61

Eureka! Here we've asked Klout to return the Klout score for the Twitter username "jack" (the original founder of Twitter). With this in mind, let's try to obtain the Klout score for everyone we follow!

Step 2 - requiring Klout

First, you'll want to require the Klout gem in your JSTwitter implementation, and make sure you can make requests to the Klout service. At the top of the jstwitter file, insert the following just below require 'jumpstart_auth'

require 'klout'

Next, in your initialize method, insert the following:

@k = Klout::API.new('6f2zva63qwtan3hgwvesa7b8')

Great! Now you're set up to make requests to Klout's API!

Step 3 -

So, you've already written logic to obtain a list of your friends, which looks something like this:

screen_names = @client.friends.collect{ |f| f.screen_name }

If we wanted to write a method that prints out the Klout score for all of our friends, its logic would be structured something like this:

Obtain a list of all your friends and save that list inside a variable named screen_names (this is done using the method above)
Step through the list of friends. For reach friend, issue a request to Klout to obtain their Klout score.
Print out the screen_name followed by their Klout score.
Here's the basic setup for this method, you can fill in the gaps:

def klout_score
friends = @client.friends.collect{|f| f.screen_name}
friends.each do |friend|

print your friend's screen_name

print your friends's Klout score

puts "" Print a blank line to separate each friend
end
end

Once you're finished, test the method by inserting the following line at the bottom of the file:

jst.klout_score

and then run the program. Did it work? Who has the highest Klout score amongst your friends?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment