processi

about processes and engines

Archive for the ‘social’ Category

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 [/sourcecode] 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…

Written by John Mettraux

May 30, 2008 at 5:01 am

Posted in rest, ruby, social, twitter