-
-
Save Epictetus/1740179 to your computer and use it in GitHub Desktop.
TumblrでAPIを使用して写真を取得するスクリプト。tumblrのapi_keyと、gemのthorライブラリが必要です。usageは、オプションなしでコマンドラインから起動すると表示されます。とりあえず雑なエラー処理を組み込んだので、完走はするはず(並列処理はしないので極端に遅いですが)。
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
#!/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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment