很久没更新但还是陆续有访问者,有点而不安。
发个和百度最近的说吧有点儿关系的脚本。
你懂得。
?
#!/usr/bin/env ruby
# @auther xhan
# @date sep19, 2010
# @website http://ixhan.com
require 'net/http'
require 'net/https'
require 'rubygems'
require 'json'
def follow!
# puts "usage: from_id to_id(not included)" and return unless ARGV.count == 2 #why not works?
if ARGV.count != 2
puts "usage: from_id to_id(not included)"
return
end
from,to = ARGV
from,to = from.to_i,to = to.to_i - 1
total = to - from + 1
puts "wrong argus" and return if total < 0
puts "=== Task starts at duration [#{from},#{to}]"
http = Net::HTTP.new('t.baidu.com')
path = '/follow/setfollow?rn=56.392819142620596'
headers = {
'Cookie' => "YOUR COOKIES HERE",
'Referer' => 'http://t.baidu.com/',
'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'
# X-Request-By baidu.ajax
}
# {"status":-1,"msg":"已经关注了"}
# {"status":-1,"msg":"该用户不存在或者帐号异常"}
# status = 1 => success
successed = 0
start_time = Time.now
(from..to).each do |uid|
data = "&uid=#{uid}&t=t"
resp, data = http.post(path, data, headers)
# puts 'Code = ' + resp.code + ' Msg = ' + resp.message
# resp.each {|key, val| puts key + ' = ' + val}
puts "uid #{uid}: data:#{data}"
json = JSON.parse(data)
successed += 1 if json['status'].to_i == 1
end
end_time = Time.now
puts "=== Task finished =============="
puts "time: #{end_time-start_time}"
puts "followed:#{successed}"
puts "failed:#{total - successed}"
puts "================================"
end
follow!
?使用说明
http://gist.github.com/586555
?
不过好久没写ruby有个问题请教下。
?
为什么
# puts "usage: from_id to_id(not included)" and return unless ARGV.count == 2 #why not works?这句话的 return 不管用?