Created
April 14, 2012 10:35
-
-
Save siyo/2383458 to your computer and use it in GitHub Desktop.
tumblrから画像ぶっこ抜くけど、もうあるやつはぶっこ抜かないやつ
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 -*- | |
# | |
# Tumblr photos backup | |
# | |
# | |
require 'rubygems' | |
require 'json' | |
require 'open-uri' | |
require 'fileutils' | |
require 'logger' | |
log = Logger.new(STDOUT) | |
log.level = Logger::INFO | |
abort "Usage: ruby #{$0} ACCOUNT OUTDIR [OFFSET]" if ARGV.size < 2 | |
user = ARGV.shift | |
outdir = ARGV.shift | |
offset = ARGV.shift || 0 | |
unless FileTest.exist? outdir | |
FileUtils.mkdir_p outdir | |
end | |
REGEXP_SAVED_FILE = /tumblr\d+_(\w+).\w+/ | |
saved_files = Dir.glob("#{outdir}/*.*").select{|e| REGEXP_SAVED_FILE =~ e} | |
saved_keys = saved_files.map{|e| e.match(REGEXP_SAVED_FILE)[1] } | |
url = "http://#{user}.tumblr.com/api/read/json" | |
query = {:debug => 1, :type => 'photo', :num => 50} | |
start = offset.to_i | |
while(true) | |
query[:start] = start | |
uri = url + '?' + query.map{|k,v| "#{URI.encode(k.to_s)}=#{URI.encode(v.to_s)}"}.join("&") | |
json = JSON.load(open(uri)) | |
total = json['posts-total'].to_i | |
posts = json["posts"] | |
posts.each_with_index{|e,i| e[:i] = total - start - i} | |
posts.delete_if{|e| saved_keys.include? e["reblog-key"]} | |
if posts.empty? | |
log.info("finish!") | |
break | |
else | |
log.info("getting posts (%d/%d) ..." % [total - start - posts.size, total]) | |
end | |
posts.each{|post| | |
n_retry = 0 | |
type = 1280 | |
begin | |
key = post["reblog-key"] | |
src = post["photo-url-#{type}"] | |
dst = "#{outdir}/tumblr#{post[:i]}_#{key}#{File.extname(src)}" | |
log.debug("open #{src} / reblog-key:#{key}") | |
open(dst, "wb"){|f| | |
open(src){|data| | |
f.write(data.read) | |
} | |
} | |
log.info("saved " + dst) | |
rescue | |
if n_retry < 2 | |
# change photo-url-1280 -> 500 | |
type = 500 | |
if n_retry == 1 | |
log.warn("WATING FOR RETRY..."); | |
sleep(10) | |
end | |
n_retry += 1 | |
retry | |
end | |
log.error "NOT_SAVED: " + post["photo-url-500"] | |
end | |
} | |
start += query[:num] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment