tamalw (owner)

Revisions

  • 9c951b firblitz Thu Feb 05 18:00:02 -0800 2009
gist: 59170 Download_button fork
public
Public Clone URL: git://gist.github.com/59170.git
Embed All Files: show embed
twitter-follower-overlap.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env ruby
# Written by @tamalw for @jasonfried (http://twitter.com/jasonfried/status/1177797820)
# Portions of the Twitter class borrowed from @hagus (http://gist.github.com/41922)
 
require 'rubygems'
require 'net/http'
require 'hpricot'
 
class Twitter
 
  def initialize(username, password)
    @http = Net::HTTP.start('twitter.com')
    @username = username
    @password = password
  end
  
  def _get_doc_for_url(url)
    url = URI.parse(url)
    req = Net::HTTP::Get.new(url.request_uri)
    req.basic_auth(@username, @password)
    res = @http.request(req)
    return Hpricot.XML(res.body)
  end
 
  def followers_ids(username=@username)
    url = "http://twitter.com/followers/ids/#{username}.xml"
    doc = self._get_doc_for_url(url)
    followers = doc.search("ids id").collect { |u| u.inner_text }
    return followers
  end
  
end
 
f = Twitter.new("your username", "your password")
 
first_username = "jasonfried"
second_username = "37signals"
 
first_followers = f.followers_ids(first_username)
second_followers = f.followers_ids(second_username)
overlap_followers = first_followers & second_followers
 
puts "Twitter follower comparison"
puts "---------------------------"
puts "#{first_username}: #{overlap_followers.size.to_f/first_followers.size*100}% overlap with #{second_username}"
puts "#{second_username}: #{overlap_followers.size.to_f/second_followers.size*100}% overlap with #{first_username}"