by agate - Published: 2010-02-13 [1:00 下午] - Category: 程序编码
by agate - Published: 2008-09-01 [7:45 下午] - Category: 程序编码
today i saw a Jason Torres said on http://www.mashupgarage.com/ that he write a Plurk Post API. it use the Mechanize on ruby! it's great! and thanks for the idea, i code a douban "i say" api by myself!
##
# Douban API Power by Mechanize
# http://agatezone.cn
# by agate.
#
require 'rubygems'
require 'mechanize'
class Douban
@username = ''
@password = ''
@agent = nil
def initialize(username, password)
@username = username
@password = password
@agent = WWW::Mechanize.new { |a|
a.user_agent_alias = 'Mac Safari'
}
end
def send_message(message)
@agent.get('http://www.douban.com') do |douban|
my_page = douban.form_with(:action => '/login') do |form|
form.form_email = @username
form.form_password = @password
end.submit
post_params = { :mb_text => message }
@agent.post('http://www.douban.com/mine/miniblogs', post_params)
end
end
end
DOUBAN_USERNAME = 'your-email'
DOUBAN_PASSWORD = 'your-password'
# Usage
douban = Douban.new(DOUBAN_USERNAME, DOUBAN_PASSWORD)
douban.send_message('test')
by agate - Published: 2008-06-15 [9:06 下午] - Category: 程序编码
power by ruby language:
require "net/http"
module SmallBlogSender
def SmallBlogSender.send(username, password, web_server, message, param_name="status")
url = URI.parse(web_server)
req = Net::HTTP::Post.new(url.path)
req.basic_auth username, password
req.set_form_data({param_name => message}, ';')
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
res.error!
end
return res
end
UPDATESERVERS = {
"fanfou" => "http://api.fanfou.com/statuses/update.xml",
"twitter" => "http://twitter.com/statuses/update.xml"
}
end
puts "message:(less than 140 characters)"
message=gets
print "username: "
username=gets.delete("\n")
print "password: "
password=gets.delete("\n")
print "==== Now sending to Fanfou ===>"
response = SmallBlogSender.send(username, password, SmallBlogSender::UPDATESERVERS["fanfou"], message)
puts " OK." if response==Net::HTTPSuccess or Net::HTTPRedirection
print "==== Now sending to Twitter ===>"
response = SmallBlogSender.send(username, password, SmallBlogSender::UPDATESERVERS["twitter"], message)
puts " OK." if response==Net::HTTPSuccess or Net::HTTPRedirection
suggest to run on linux(unix or mac) bash!