Created
May 9, 2013 16:54
-
-
Save sorah/5548793 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
Dir.chdir(File.dirname(__FILE__)) | |
require 'digest/sha2' | |
require 'uri' | |
require 'open-uri' | |
require 'json' | |
API_KEY = 'api_key' | |
TUMBLELOG = 'scrap.sorah.jp' | |
def get_photos(offset=0) | |
response = open("http://api.tumblr.com/v2/blog/#{TUMBLELOG}/posts/photo?api_key=#{API_KEY}&offset=#{offset}", 'r', &:read) | |
JSON.parse(response) | |
end | |
def generate_filename(post_url, img_url) | |
m = post_url.match(%r{/post/(\d+?)(\Z|/)}) | |
post_id = m ? m[1] : '' | |
img_ext = File.extname(URI.parse(img_url).path) | |
"#{post_id}_#{Digest::SHA256.hexdigest(img_url)}#{img_ext}" | |
end | |
downloaded = true | |
offset = 0 | |
while downloaded | |
downloaded = false | |
payload = get_photos(offset) | |
puts "============ Offset: #{offset}" | |
payload['response']['posts'].each do |item| | |
link = item['post_url'] | |
puts "=> #{link}" | |
img_urls = item['photos'].map { |_| _['original_size']['url'] rescue nil }.compact | |
img_urls.each do |url| | |
filename = generate_filename(link, url) | |
if File.exist?(filename) | |
next | |
end | |
puts "* #{url} => #{filename}" | |
system "curl", "--progress-bar", "-o", filename, url | |
downloaded = true | |
end | |
end | |
offset += 20 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment