Created
November 30, 2009 03:01
-
-
Save hisasann/245238 to your computer and use it in GitHub Desktop.
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
require "rubygems" | |
require "net/http" | |
require "rexml/document" | |
require 'twitter' | |
class RandomTumble | |
attr_accessor :id, :tumblelog, :posts, :url | |
def initialize(id) | |
@@uri = id + '.tumblr.com' | |
@id = id | |
end | |
def random_post_twitter(id, pass) | |
random() | |
post_twitter(id, pass) | |
end | |
def random | |
Net::HTTP.start(@@uri, 80) {|http| | |
request_tumblr(http, 0, 1, "photo") | |
rand_page = rand(@posts.attributes["total"].to_i - 1) | |
request_tumblr(http, rand_page, 1, "photo") | |
@posts.each {|post| | |
@url = post.attributes["url"] | |
} | |
} | |
end | |
def request_tumblr(http, start, num, type) | |
req = getReq() | |
req.set_form_data(getData(start, num, type)) | |
res = http.request(req) | |
doc = nil | |
if res.kind_of?(Net::HTTPSuccess) | |
doc = REXML::Document.new(res.body) | |
else | |
raise ResponseError.new(res) | |
end | |
if doc | |
@tumblelog = REXML::XPath.first(doc, "//tumblelog") | |
@posts = REXML::XPath.first(doc, "//posts") | |
end | |
end | |
def post_twitter(id, pass) | |
httpauth = Twitter::HTTPAuth.new(id, pass) | |
twit = Twitter::Base.new(httpauth) | |
twit.update(makeMessage()) | |
end | |
def getReq | |
return Net::HTTP::Post.new("/api/read") | |
end | |
def getData(start, num, type) | |
return { | |
"start" => start, | |
"num" => num, | |
"type" => type | |
} | |
end | |
def makeMessage | |
return @url | |
end | |
end | |
RandomTumble.new("hoge").random_post_twitter("id", "password") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment