Skip to content

Instantly share code, notes, and snippets.

@noplans
Created March 3, 2009 03:31
Show Gist options
  • Save noplans/73157 to your computer and use it in GitHub Desktop.
Save noplans/73157 to your computer and use it in GitHub Desktop.
# vim:fileencoding=utf-8
require 'rubygems' if RUBY_VERSION < '1.9'
require 'pit'
require 'nokogiri'
require 'mechanize'
require 'uri'
require 'activesupport'
Base = 'http://twitter.com/'
account = Pit.get('twitter.com')
followers_url = URI.join(Base, 'followers')
def check_enabled?
timestamp = 'timestamp.yaml'
last_checked = File.exist?(timestamp) ? YAML.load(File.open(timestamp)) : 5.minutes.ago
Time.now - last_checked > 5.minutes
end
unless check_enabled?
puts "too early"
exit
end
ua = WWW::Mechanize.new
ua.get(followers_url) do |page|
followers_page = page.form_with(:action => 'https://twitter.com/sessions') do |f|
f['session[username_or_email]'] = account['login']
f['session[password]'] = account['password']
end.click_button
# followers_url
h2 = followers_page.at('//h2').inner_text
numbers = if /Your (\d+) Followers/ =~ h2 then $1.to_i else 0 end
n = numbers/20 + 1
followers = (1..n).inject([]) do |followers, i|
followers_url.query = "page=#{i}"
puts followers_url
f = ua.get(followers_url)
followers + (f/'//div[@class="person-actions"]/button/../..//a[@class="url uid"]').map{|url| url['href']}
end
YAML.dump(Time.now, File.open('timestamp.yaml', 'w'))
YAML.dump(followers, File.open('followers.yaml', 'w'))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment