In this part of my programming series I expand on my previous work and analyze my followers and my followings. Feel free to follow along!
If you would like to follow what I'm doing this at home you should check out Part 1 and Part 2 of this series. Last time we pulled some general account data. Today we want to get to know our followers a little more! Who is following me back - who am I following back?
This tutorial is written in Ruby with the help of Radiator API client provided by @inertia.
First we shall take over the script for fallback nodes from last time:
require 'radiator'
options = {
ur: 'https://api.steemit.com',
failover_urls: [
'https://api.steemitstage.com'
]
}
api = Radiator::Api.new(options)
And the script for getting followers:
followapi = Radiator::FollowApi.new
response = followapi.get_followers('cryptonik', 0, 'blog', 1000)
$followers_ary = response.result.map(&:follower)
Next, we will retrieve the users that I am following:
response = followapi.get_following('cryptonik', 0, 'blog', 1000)
$following_ary = response.result.map(&:following)
=>Doesn't work
Turns out it's a little more tricky.
The method get_following is limited to retrieving 100 users at a time (4th arg). The second argument startFollowing will ask for the username from where to start retrieving. To start at the beginning, we start at null. To construct our following list manually, we can fetch the users list multiple times:
$following_ary = followapi.get_following('cryptonik',0, 'blog',100).result.map(&:following)
$fetch_next = followapi.get_following('cryptonik',"#{$following_ary[99]}", 'blog',100).result.map(&:following)
$following_ary.push(*$fetch_next)
$fetch_next = followapi.get_following('cryptonik',"#{$fetch_next[99]}",'blog',100).result.map(&:following)
$following_ary.push(*$fetch_next)
There problem here is that I fetch the 100th element 2 times, since the next fetch starts at 100. Ideally I should start at element 101 for the next call, but this is not possible. To overcome this we can throw in a cheecky pop to get rid of the last element before merging:
$following_ary.pop
Now we can make a loop that fetches all users I'm following, independent of list length:
$following_ary = followapi.get_following('cryptonik',0, 'blog',100).result.map(&:following)
$fetch_next = followapi.get_following('cryptonik',"#{$following_ary[99]}", 'blog',100).result.map(&:following)
$following_ary.pop
$following_ary.push(*$fetch_next)
loop do
$fetch_next = followapi.get_following('cryptonik',"#{$fetch_next[99]}",'blog',100).result.map(&:following)
$following_ary.pop
$following_ary.push(*$fetch_next)
break if($fetch_next.length<98)
end
We set the break condition to fetch_next.length<98 to determine if the buffer is full or not. If the buffer is not full, we he have fetched all outstanding users.
No that we have our followers and following we can play with the data. Let's find out who isn't following me back?
We could loop through the arrays and compare elements, however there is a much easier way in Ruby, that returns the difference in array elements as a new array:
$diff=$following_ary-$followers_ary
And for users that are following me, but I am not following back:
$diff2=$followers_ary-$following_ary
To present the data we can print out the first 20 elements of each array:
$diff=$following_ary-$followers_ary
puts "\n\n#{$diff.length} users are not following you back:"
for i in 0..19
$stdout.print("#{$diff[i]}, ")
end
$stdout.print("...and #{$diff.length-20} more")
In this case we use $stdout.print() because it looks nicer if we print on a single line instead of a new line for each username. We can implement the same for the other data outputs.
Make a file in your editor of choice and save it with the *.rb extension.
require 'radiator'
options = {
ur: 'https://api.steemit.com',
failover_urls: [
'https://api.steemitstage.com'
]
}
api = Radiator::Api.new(options)
followapi = Radiator::FollowApi.new
response = followapi.get_followers('cryptonik', 0, 'blog', 1000)
$followers_ary = response.result.map(&:follower)
$following_ary = followapi.get_following('cryptonik',0, 'blog',100).result.map(&:following)
$fetch_next = followapi.get_following('cryptonik',"#{$following_ary[99]}", 'blog',100).result.map(&:following)
$following_ary.pop
$following_ary.push(*$fetch_next)
loop do
$fetch_next = followapi.get_following('cryptonik',"#{$fetch_next[99]}",'blog',100).result.map(&:following)
$following_ary.pop
$following_ary.push(*$fetch_next)
break if($fetch_next.length<98)
end
puts "Done fetching data for 'cryptonik'..."
puts "\nYou have #{$followers_ary.length} followers:"
for i in 0..19
$stdout.print("#{$followers_ary[i]}, ")
end
$stdout.print("...and #{$followers_ary.length-20} more")
puts "\n\nYou are following #{$following_ary.length} users:"
for i in 0..19
$stdout.print("#{$following_ary[i]}, ")
end
$stdout.print("...and #{$following_ary.length-20} more")
$diff=$following_ary-$followers_ary
puts "\n\n#{$diff.length} users are not following you back:"
for i in 0..19
$stdout.print("#{$diff[i]}, ")
end
$stdout.print("...and #{$diff.length-20} more")
$diff2=$followers_ary-$following_ary
puts "\n\nYou don't follow back #{$diff2.length} users:"
for i in 0..19
$stdout.print("#{$diff2[i]}, ")
end
$stdout.print("...and #{$diff2.length-20} more\n\n")
$ruby get_fwg3.rb
Feel free to try this at home and if you get stuck you can always ping me.
Link to Part 1 and Part 2 of this series.
Resources:
Radiator docs
Radiator github
Steem github