Created
September 15, 2014 01:39
-
-
Save murachue/ae5adbcd4bdfbfbaeb92 to your computer and use it in GitHub Desktop.
もうすぐ終わるTwitPicのAPIをさわってみるテスト。rubyっぽく書けるrest clientライブラリあるのかな。
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
# coding: utf-8 | |
# usage: fetchall.rb <username> | |
CA_BUNDLE_FILE = "ca-bundle.pem" | |
require "json" | |
require "net/http" | |
require "net/https" | |
require "cgi" | |
require "stringio" | |
class ExHttp | |
class Part | |
def initialize parent, name | |
@parent = parent | |
@name = name | |
end | |
def get name, *args | |
return @parent.get @name + "/" + name.to_s, *args | |
end | |
def method_missing name, *args | |
if args.size == 0 | |
return Part.new(self, name.to_s) | |
else | |
return get name, *args | |
end | |
end | |
end | |
def initialize http, basepath, ext | |
@http = http | |
@basepath = basepath || "" | |
@ext = ext | |
end | |
def self.get_then_save http, path | |
fullurl = "http" + (http.use_ssl? ? "s" : "") + "://" + http.address + ":" + http.port.to_s + path | |
fn = CGI.escape(fullurl) | |
if File.exist? fn | |
$stderr.puts "I: Using cache #{fullurl}" | |
# FIXME using Net::HTTP's internal methods... | |
#res = open(fn, "rb") { |ff| # note: raise some exception in read_nonblock... | |
res = StringIO.open(File.binread(fn), "rb") { |ff| | |
f = Net::BufferedIO.new ff | |
r = Net::HTTPResponse.read_new f | |
#r.decode_content = "" | |
#r.uri = "" | |
r.reading_body(f, true) { # FIXME assuming req.response_body_permitted? == true | |
yield r if block_given? | |
} | |
r | |
} | |
return res | |
else | |
$stderr.puts "I: Fetching #{fullurl}" | |
res = http.get path | |
open(fn, "wb") { |f| | |
f.print "HTTP/#{res.http_version} #{res.code} #{res.message}\r\n" | |
res.each_header { |k,v| | |
next if k.downcase == "transfer-encoding" | |
f.print "#{k}: #{v}\r\n" | |
} | |
f.print "\r\n" | |
f.write res.body | |
} | |
sleep 1 # rest the server... | |
return res | |
end | |
end | |
def get name, *args | |
qs = "" | |
if 0 < args.size | |
qs = "?" + args.last.map{|k, v| "#{k}=#{CGI.escape v.to_s}" }.join("&") | |
end | |
path = @basepath + "/" + name + @ext + qs | |
res = self.class.get_then_save @http, path | |
return res | |
end | |
def method_missing name, *args | |
if args.size == 0 | |
return Part.new(self, name.to_s) | |
else | |
return get name, *args | |
end | |
end | |
end | |
http = Net::HTTP.new("api.twitpic.com", 443) | |
http.use_ssl = true | |
http.ca_file = CA_BUNDLE_FILE | |
http.verify_mode = OpenSSL::SSL::VERIFY_PEER | |
http.verify_depth = 5 | |
http.start { | |
api = ExHttp.new(http, "/2", ".json") | |
images = [] | |
loop.each_with_index { |_,i| | |
res = api.users.show(username: ARGV[0], page: (i + 1)) | |
p res | |
break if res.code == "404" | |
res.value | |
jsn = JSON.parse(res.body) | |
images += jsn["images"] | |
} | |
puts "#{images.size} images" | |
http_cf = Net::HTTP.new("d3j5vwomefv46c.cloudfront.net", 443) | |
http_cf.use_ssl = true | |
http_cf.ca_file = CA_BUNDLE_FILE | |
http_cf.verify_mode = OpenSSL::SSL::VERIFY_PEER | |
http_cf.verify_depth = 5 | |
http_cf.start { | |
images.each { |e| | |
#http://api.twitpic.com/2/media/show.json?id={short_id} | |
res = api.media.show(id: e["short_id"]) # passing "id" as id cause "Image not found"... | |
p res | |
res.value | |
jsn = JSON.parse(res.body) | |
#https://d3j5vwomefv46c.cloudfront.net/photos/large/{id}.{type}?{timestamp?} | |
res = ExHttp.get_then_save http_cf, "/photos/large/#{jsn["id"]}.#{jsn["type"]}" | |
p res | |
res.value | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment