Created
April 30, 2009 03:08
-
-
Save daviscabral/104221 to your computer and use it in GitHub Desktop.
Mechanize tests to get orkut/twitter messages
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
# Mechanize tests with Orkut/Twitter | |
# @author Davis Zanetti Cabral | |
# Orkut usage example: | |
# >> require 'jum_proxy' | |
# >> orkut = JumProxy::Sites::Orkut.new("[email protected]", "pa$$w0rd", "pt-BR") | |
# >> orkut.scraps | |
# | |
# Twitter usage example: | |
# >> require 'jum_proxy' | |
# >> twitter = JumProxy::Sites::Twitter.new("daviscabral", "pa$$w0rd") | |
# >> twitter.tweets | |
# >> twitter.tweets("anyuser") | |
# >> twitter.post("Reading a book? :-)") | |
require 'rubygems' | |
require 'mechanize' | |
require 'nokogiri' | |
require 'uri' | |
module JumProxy | |
class NotFound < StandardError; end | |
class NotLoggedIn < StandardError; end | |
class NotAuthorized < StandardError; end | |
class ArgumentInvalid < StandardError; end | |
class Base | |
attr_accessor :agent, :user_input, :pass_input, :logged_in | |
def get(url) | |
raise JumProxy::ArgumentInvalid, "URL can't be blank" if url.nil? || url.empty? | |
self.agent = WWW::Mechanize.new if self.agent.nil? | |
self.agent.get(url) | |
unless self.agent.page | |
raise JumProxy::NotFound, "JumProxy can’t open the page “#{url}”." | |
else | |
self.agent.page | |
end | |
end | |
def login(user, pass, url) | |
page = self.get(url) | |
form = page.forms.first | |
# Fill/submit form | |
form[self.user_input] = user | |
form[self.pass_input] = pass | |
self.agent.submit(form) | |
end | |
def logged_in? | |
self.logged_in == true | |
end | |
end | |
module Sites | |
class Orkut < JumProxy::Base | |
def initialize(user, pass, lang='en-US') | |
self.user_input = "Email" | |
self.pass_input = "Passwd" | |
url = "https://www.google.com/accounts/ServiceLogin?service=orkut&hl=#{lang}"+ | |
"&rm=false&continue=http%3A%2F%2Fm.orkut.com%2FRedirLogin%3Fmsg%3D0%26"+ | |
"page%3Dhttp%253A%252F%252Fm.orkut.com%252FHome&cd=US&nui=5&btmpl=mobi"+ | |
"le<mpl=mobile&passive=true&skipvpage=true&sendvemail=false" | |
page = self.login(user, pass, url) | |
unless page.body.match("errormsg_0_Passwd").nil? | |
raise JumProxy::NotAuthorized, "JumProxy can't login with this credential (#{user})." | |
else | |
page = self.agent.get page.meta.first.attributes['href'].gsub(/'/,'') | |
page = self.agent.get page.uri.to_s.sub(/\?.*$/, "?ui=html&zy=n") | |
self.logged_in = true | |
end | |
end | |
def scraps(user=nil) | |
raise JumProxy::NotLoggedIn, "You need login before access this resource." unless logged_in? | |
if user.nil? | |
page = self.get("http://m.orkut.com:80/Scrapbook") | |
scrap_items = [] | |
page.parser.css("div.mblock").each do |block| | |
scrap_items << block unless block.inner_html.match("FullProfile").nil? | |
end | |
return scrap_items | |
else | |
#@TODO Search user here | |
end | |
end | |
def logout | |
raise JumProxy::NotLoggedIn, "You need login before access this resource." unless logged_in? | |
get("http://m.orkut.com/GLogin?cmd=logout") | |
end | |
end | |
class Twitter < JumProxy::Base | |
def initialize(user, pass) | |
self.user_input = "session[username_or_email]" | |
self.pass_input = "session[password]" | |
url = "http://m.twitter.com/login" | |
page = self.login(user, pass, url) | |
unless page.body.match("Wrong Username/Email and password combination").nil? | |
raise JumProxy::NotAuthorized, "JumProxy can't login with this credential (#{user})." | |
else | |
self.logged_in = true | |
end | |
end | |
def tweets(user=nil) | |
raise JumProxy::NotLoggedIn, "You need login before access this resource." unless logged_in? | |
if user.nil? | |
url = "http://m.twitter.com/home" | |
else | |
url = "http://m.twitter.com/#{user}" | |
end | |
page = self.get(url) | |
tweet_items = [] | |
page.parser.css("li").each do |block| | |
tweet_items << block.content | |
end | |
return tweet_items | |
end | |
def post(message) | |
raise JumProxy::NotLoggedIn, "You need login before access this resource." unless logged_in? | |
raise JumProxy::ArgumentInvalid, "Your message is too long (max. 140 chars)." if message.length > 140 | |
page = self.get("http://m.twitter.com/home") | |
form = page.forms.first | |
form["status"] = message | |
self.agent.submit(form) | |
end | |
end | |
class BlogSpot < JumProxy::Base | |
def last_posts(url) | |
end | |
def comments(post) | |
end | |
def post_comment(name, email, message) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment