Skip to content

Instantly share code, notes, and snippets.

@Constellation
Created January 21, 2009 12:52
Show Gist options
  • Save Constellation/49966 to your computer and use it in GitHub Desktop.
Save Constellation/49966 to your computer and use it in GitHub Desktop.
check tumblr followers and followings. work with mechanize 0.9.0
#!/usr/bin/env ruby
require 'rubygems'
require 'mechanize'
require 'cgi'
email = ''
password = ''
email, password = ARGV
#version < 1.8.7 対策
if RUBY_VERSION < '1.8.7'
class Object
def tap
yield(self)
self
end
end
end
module Tumblr
class Dashboard
PREFIX = 'http://www.tumblr.com/dashboard/iframe?src='
attr_reader :followers, :followings
def initialize(email, password)
@email = email
@password = password
@agent = WWW::Mechanize.new
login
end
def login
@agent.get('http://www.tumblr.com/login')
@agent.page.form_with(:action => '/login') do |f|
f.field_with(:name => 'email').value = @email
f.field_with(:name => 'password').value = @password
f.click_button
end
end
def logout
@agent.get('http://www.tumblr.com/logout')
end
def get_followers
doc = @agent.get('http://www.tumblr.com/followers')
@followers = doc.search('a.username')
end
def get_followings
doc = @agent.get('http://www.tumblr.com/following')
@followings = doc.search('a.username')
end
def check
get_followers
get_followings
temp = @followings.map{|a| a.inner_text}.sort
@followers.inject([]) do |memo, a|
if(temp.index(a.inner_text) == nil) then
memo.push(a)
end
memo
end.tap do |a|
p "follow #{a.size} users"
end.each do |a|
print "#{a.inner_text}..."
doc = @agent.get(PREFIX+CGI.escape(a.attributes['href'].value))
form = doc.forms.first
form && form.click_button
p "done"
end
#logout
end
end
end
dashboard = Tumblr::Dashboard.new email, password
dashboard.check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment