Created
April 23, 2012 10:33
-
-
Save onodes/2470144 to your computer and use it in GitHub Desktop.
twitter crawler for complex networks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require 'twitter' | |
class TwitterUser | |
def initialize() | |
@twitter = Twitter.new | |
end | |
def get_user(user_name) | |
begin | |
return @twitter.user(user_name) | |
rescue | |
end | |
end | |
def get_following(user_name) | |
begin | |
return @twitter.friend_ids(user_name) | |
rescue | |
end | |
end | |
def get_followers(user_name) | |
begin | |
return @twitter.follower_ids(user_name) | |
rescue | |
end | |
end | |
end | |
class TwitterCrawler | |
def initialize(start_user) | |
@twitter_user = TwitterUser.new | |
@start_user_id = Twitter.user(start_user)["id"] | |
end | |
def following_relation | |
@relation_array = Array.new | |
@following_ids = @twitter_user.get_following(@start_user_id)["ids"] | |
@following_ids.map{|id| | |
@relation_array << [@start_user_id,id] | |
} | |
end | |
end | |
array = TwitterCrawler.new("onodes").following_relation[0] | |
f = File.open("following.dat","w") | |
array.each{|id_list| | |
begin | |
TwitterCrawler.new(id_list[1]).following_relation[0].map{|ids| | |
f.puts(ids[0].to_s + " " + ids[1].to_s) | |
} | |
rescue | |
next | |
end | |
} | |
f.close | |
#p TwitterCrawler.new.get_user("onodes") | |
#p TwitterCrawler.new.get_followers("onodes") | |
describe do | |
before do | |
@twitter = TwitterUser.new.should be_true | |
end | |
it do | |
@twitter.get_user("onodes").should be_true | |
end | |
it do | |
@twitter.get_user(17942457).should be_true | |
end | |
it do | |
@twitter.get_user("onodes").should eq @twitter.get_user(17942457) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment