Skip to content

Instantly share code, notes, and snippets.

@tadelv
Created April 10, 2010 20:43
Show Gist options
  • Save tadelv/362286 to your computer and use it in GitHub Desktop.
Save tadelv/362286 to your computer and use it in GitHub Desktop.
a dumb script if you are bored and don't know who to follow on twitter
#!/usr/bin/env ruby
# Analyzes your twitter friends if they are following someone interesting
# akshully it just shows you, who has the most many followers
# usage: ./twitteggester.rb <username> <how many followers to research>
#
# works by reading your twitter page (the span class vcard)
require "rubygems"
require "open-uri"
require "hpricot"
####################################
# => extract whom is your friend following
# => adds all interesting people to an array
# => calculates the number of occurences
####################################
def process_friend(fol,interesting, ignore)
data = fol.search('a.url').first
ignore << data.attributes['href']
url = "http://twitter.com#{data.attributes['href']}"
#go to the users page
begin
doc = Hpricot.parse(open(url))
rescue
return
end
fol = doc.search('span.vcard')
fol.each do |pos|
info = pos.search('a.url').first
ref = info.attributes['href']
index = 0
need_create = -1
interesting.each_with_index do |elem,idx|
if elem.key?(ref)
interesting[idx] = {ref => elem[ref]+=1}
need_create = 0
break
else
need_create = 1
end
end
if need_create == 1 && !ignore.include?(ref.gsub("/","")) && !ignore.include?(ref)
interesting << {ref => 1}
need_create=0
end
end
end
####################################
# => sorts the array by comparing the number of
# => occurences
####################################
def sortHash(hash)
r = hash.sort {|a,b| b[b.keys[0]] <=> a[a.keys[0]] }
return r
end
####################################
# => the main procedure
####################################
name = ARGV[0]
limit = ARGV[1]? ARGV[1].to_i : 0
print "searching for suggestions for %s (this may take a while...)\n" % [name]
begin
doc = Hpricot.parse(open("http://twitter.com/#{name}"))
rescue
puts "sry... i haz error"
exit
end
following = doc.search('span.vcard')
interesting = [{"noone" => 0}]
ignorez = [name]
following.each_with_index do |fol,i|
process_friend(fol,interesting,ignorez)
if i == limit && limit != 0
break
end
end
puts "sorting..."
i = sortHash(interesting)
puts "="*24
(0..10).each do |idx|
a = i[idx]
v = a[a.keys[0]]
print "%i: %s -- %i %s following\n" % [idx+1, (a.keys[0]).gsub("/",""), v, v > 1? "friends" : "friend"]
end
@tadelv
Copy link
Author

tadelv commented Apr 10, 2010

This is still a lot of crap

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