twitter friends and followers
July 2009 update : this is a way better script.
A script for determining two Twitter sets : friends and followers, and the union of those two sets. It leverages the Twitter REST API.
I use it for my Twitter [social] graph gardening needs.
Prerequisite :
sudo gem install -y json_pure rufus-verbs
The script expects to find your Twitter username and password in the environment (TWUSER and TWPASS).
require 'rubygems'
require 'json'
require 'rufus/verbs'
TW = "https://twitter.com"
USER = ENV['TWUSER']
PASS = ENV['TWPASS']
#
# fetches user and pass
# in the environment
def find (user, relation, page=1)
res = Rufus::Verbs.get(
"#{TW}/statuses/#{relation}/#{user}.json",
:params => { :lite => true, :page => page },
:hba => [ USER, PASS ])
raise "error (#{res.code})\n#{res.body}" \
unless res.code.to_i == 200
f = JSON.parse(res.body)
f = f.collect { |f| f['screen_name'] }
f = f + find(user, relation, page+1) \
if f.size >= 100
f
end
# /!\ Clients may not make more than 30 requests
# per hour
friends = find USER, :friends
followers = find USER, :followers
(friends + followers).sort.uniq.each do |f|
flag = if (friends.include?(f) and
followers.include?(f))
"*"
elsif followers.include?(f)
"<"
else
"."
end
puts " #{flag} #{f}"
end
It will output a list that looks like :
* dominieq . drniq * edmondedgar . elswa < ericsabaurin . github < gerloo < hickup * hotoande * htws
where . means “following him”, < “he follows me” and * “mutual follow”.
The script works well for my account, it should paginate friends/followers correctly for users with more than 100 persons in those sets. Twitter seems very sensitive about the request rate…
Very cool! I’m doing some twitter experiments too. Did you have any troubles with their downtime?
I had _a lot_ of problems in the past with their service uptime, especially the XMPP parts.
dominiek
June 4, 2008 at 8:31 am
Hello Dominiek,
no troubles with the downtime, but had to be patient while developing the script, hitting the 30 requests per hour ceiling quite quickly. Thanks for the warning.
Cheers,
John Mettraux
June 5, 2008 at 12:18 am
John,
Sounds like an interesting project. Let me know if you package it for us mortals who don’t code, I have been looking for something like this.
Could you auto-friend everyone who is following you? Uhh, on second thought it might not be a good idea.
Jonathan Crow
July 9, 2008 at 12:23 am
Hi Jonathan,
for non-coders that use a mac, it would go like :
1). paste code into file named “tw.rb” (directly in your “house” folder)
2). open terminal and run those three commands :
export TWUSER=jcrow
export TWPASS=supersecretpasswordshhhdonottell
ruby tw.rb
and it should spit the friends / followers list.
I don’t know if the script will be ok with more than 100 friends or followers, I don’t track that many people.
I have to say that I don’t use this script too much these days, I’m playing around with identi.ca (http://identi.ca/jmettraux/all), and when I want to block unnecessary [bot] followers I do it via the twitter web interface (http://twitter.com/jcrow/followers) so that I can immediately check the follower’s page to see if he is a bot or not. Lately Twitter has been displaying follow/following on that page so my script has become less interesting.
BTW, congrats for the successful conference !
John
John Mettraux
July 9, 2008 at 2:11 am