public
Created — forked from t-nagamatsu/tumblr_v2.rb

TumblrでAPIを使用して写真を取得するスクリプト。tumblrのapi_keyと、gemのthorライブラリが必要です。usageは、オプションなしでコマンドラインから起動すると表示されます。とりあえず雑なエラー処理を組み込んだので、完走はするはず(並列処理はしないので極端に遅いですが)。

  • Download Gist
tumblr_v2.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#!/usr/bin/env ruby
# coding: utf-8
 
require 'rubygems'
require 'open-uri'
require 'json'
require 'thor'
 
class Tumblr < Thor
desc "get_photos [BASE_HOSTNAME] [API_KEY]", "Download all photos by selected base-hostname (api-key required)"
def get_photos(base_hostname, api_key)
#create folder (adhoc support custom-domain, recently)
folder_name = base_hostname.gsub(/\./, "")
Dir.mkdir(folder_name) unless FileTest.exist?(folder_name)
 
#get posts count
info_url = "http://api.tumblr.com/v2/blog/#{base_hostname}/info?api_key=#{api_key}"
begin
info_json_hash = JSON::parse(open(info_url).read)
rescue
raise "first access to api is fail. please retry a little bit of late."
end
# max count
max_posts_count = info_json_hash["response"]["blog"]["posts"]
 
 
#regexs (for support 1280) and (file extension name)
regex_std = /.*\/(.+?)o1_75sq\.jpg/
regex_ext = /(.*png$|.*jpg$)/
 
image_url_set = []
 
# extract photo urls
0.step(max_posts_count, 20) do |paging_num|
posts_url = "http://api.tumblr.com/v2/blog/#{base_hostname}/posts?type=photo&api_key=#{api_key}&offset=#{paging_num}"
begin
json_hash = JSON::parse(open(posts_url).read)
rescue
redo
# I think that go infinity loop maybe low percent.
end
posts = json_hash["response"]["posts"]
 
posts.each do |post|
# hummmm?
raw_url = post["photos"][0]["alt_sizes"][-1]["url"]
# generate filename from raw_url if i can
filename = $1 if regex_std =~ raw_url
image_url = "http:\/\/#{base_hostname}\/#{post["type"]}\/1280\/#{post["id"]}\/1\/#{filename}"
 
# set 500px's url when i can't get filename
image_url = post["photos"][0]["alt_sizes"][0]["url"] if filename == nil
image_url_set << image_url
end
end
 
# download file to folder
image_url_set.each do |image_url|
local_filename = image_url.split(/\//).last
local_filename += ".jpg" unless regex_ext =~ local_filename
File::open("#{folder_name}\/#{local_filename}", "wb") do |f|
begin
f.write(open(image_url).read)
rescue
p $!
end
end
end
end
end
 
Tumblr.start

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.