Skip to content

Commit

Permalink
添加发送带图片微博到搜狐微博功能
Browse files Browse the repository at this point in the history
  • Loading branch information
hooopo committed May 18, 2011
1 parent cb391ff commit 92f7c56
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 100 deletions.
9 changes: 7 additions & 2 deletions lib/oauth_china.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
require 'rubygems'
require 'oauth'
require File.expand_path(File.join(File.dirname(__FILE__), "oauth_china/multipart"))
require 'mime/types'
require 'net/http'
require 'cgi'

require File.expand_path(File.join(File.dirname(__FILE__), "oauth_china/multipart"))
require File.expand_path(File.join(File.dirname(__FILE__), "oauth_china/upload"))

module OauthChina

class OAuth

include Multipart
include Upload

CONFIG = {}

Expand Down
120 changes: 55 additions & 65 deletions lib/oauth_china/multipart.rb
Original file line number Diff line number Diff line change
@@ -1,73 +1,63 @@
module Multipart
# From: http://deftcode.com/code/flickr_upload/multipartpost.rb
# Helper class to prepare an HTTP POST request with a file upload
# Mostly taken from
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/113774
# WAS:
# Anything that's broken and wrong probably the fault of Bill Stilwell
# (bill@marginalia.org)
# NOW:
# Everything wrong is due to keith@oreilly.com
require 'rubygems'
require 'mime/types'
require 'net/http'
require 'cgi'

class Param
attr_accessor :k, :v
def initialize(k, v)
@k = k
@v = v
module OauthChina

module Multipart

class Param
attr_accessor :k, :v
def initialize(k, v)
@k = k
@v = v
end

def to_multipart
#return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n"
# Don't escape mine...
return "Content-Disposition: form-data; name=\"#{k}\"\r\n\r\n#{v}\r\n"
end
end

def to_multipart
#return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n"
# Don't escape mine...
return "Content-Disposition: form-data; name=\"#{k}\"\r\n\r\n#{v}\r\n"
class FileParam
attr_accessor :k, :filename, :content
def initialize(k, filename, content)
@k = k
@filename = filename
@content = content
end

def to_multipart
#return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{filename}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n "
# Don't escape mine
return "Content-Disposition: form-data; name=\"#{k}\"; filename=\"#{filename}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n"
end
end
end
class MultipartPost

BOUNDARY = 'tarsiers-rule0000'
ContentType = "multipart/form-data; boundary=" + BOUNDARY

def set_form_data(req, params)
body, content_type = prepare_query(params)
req["Content-Type"] = content_type
req.body = body
req["Content-Length"] = body.bytesize
req
end

def prepare_query(params)
fp = []
params.each {|k,v|
if v.respond_to?(:read)
fp.push(FileParam.new(k, v.path, v.read))
v.close
else
fp.push(Param.new(k,v))
end
}
body = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--rn"
return body, ContentType
end

class FileParam
attr_accessor :k, :filename, :content
def initialize(k, filename, content)
@k = k
@filename = filename
@content = content
end

def to_multipart
#return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{filename}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n "
# Don't escape mine
return "Content-Disposition: form-data; name=\"#{k}\"; filename=\"#{filename}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n"
end
end
class MultipartPost

BOUNDARY = 'tarsiers-rule0000'
ContentType = "multipart/form-data; boundary=" + BOUNDARY

def set_form_data(req, params)
body, content_type = prepare_query(params)
req["Content-Type"] = content_type
req.body = body
req["Content-Length"] = body.bytesize
req
end

def prepare_query(params)
fp = []
params.each {|k,v|
if v.respond_to?(:read)
fp.push(FileParam.new(k, v.path, v.read))
v.close
else
fp.push(Param.new(k,v))
end
}
body = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--rn"
return body, ContentType
end


end
end
30 changes: 1 addition & 29 deletions lib/oauth_china/strategies/sina.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,7 @@ def upload_image(content, image_path, options = {})
upload("http://api.t.sina.com.cn/statuses/upload.json", options)
end

#NOTICE:
#各个微博字段名可能不统一
def upload(url, options)
url = URI.parse(url)
http = Net::HTTP.new(url.host, url.port)

req = Net::HTTP::Post.new(url.request_uri)
req = sign_without_pic_field(req, self.access_token, options)
req = set_multipart_field(req, options)

http.request(req)
end

#图片不参与签名
def sign_without_pic_field(req, access_token, options)
req.set_form_data(params_without_pic_field(options))
self.consumer.sign!(req, access_token)
req
end

#mutipart编码:http://www.ietf.org/rfc/rfc1867.txt
def set_multipart_field(req, params)
multipart_post = MultipartPost.new
multipart_post.set_form_data(req, params)
end

def params_without_pic_field(options)
options.except(:pic)
end


end
end
5 changes: 2 additions & 3 deletions lib/oauth_china/strategies/sohu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module OauthChina
class Sohu < OauthChina::OAuth

def initialize(*args)
#fuck sohu...!!
#搜狐微博的OAUTH 只支持将OAuth的认证参数放在HTTP的头部中。而且不可以带realm参数
self.consumer_options = {
:site => 'http://api.t.sohu.com',
Expand Down Expand Up @@ -31,9 +30,9 @@ def add_status(content, options = {})
self.post("http://api.t.sohu.com/statuses/update.json", options)
end

#TODO
def upload_image(content, image_path, options = {})
add_status(content, options)
options = options.merge!(:status => content, :pic => File.open(image_path, "rb")).to_options
upload("http://api.t.sohu.com/statuses/upload.json", options)
end

end
Expand Down
35 changes: 35 additions & 0 deletions lib/oauth_china/upload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module OauthChina
module Upload

#NOTICE:
#各个微博字段名可能不统一
def upload(url, options)
url = URI.parse(url)
http = Net::HTTP.new(url.host, url.port)

req = Net::HTTP::Post.new(url.request_uri)
req = sign_without_pic_field(req, self.access_token, options)
req = set_multipart_field(req, options)

http.request(req)
end

#图片不参与签名
def sign_without_pic_field(req, access_token, options)
req.set_form_data(params_without_pic_field(options))
self.consumer.sign!(req, access_token)
req
end

#mutipart编码:http://www.ietf.org/rfc/rfc1867.txt
def set_multipart_field(req, params)
multipart_post = Multipart::MultipartPost.new
multipart_post.set_form_data(req, params)
end

def params_without_pic_field(options)
options.except(:pic)
end

end
end
2 changes: 1 addition & 1 deletion lib/oauth_china/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module OauthChina
VERSION = "0.1.1"
VERSION = "0.2.0"
end

0 comments on commit 92f7c56

Please sign in to comment.