Last active
August 29, 2015 14:05
-
-
Save yoko/4b1c0ec20acd80140b48 to your computer and use it in GitHub Desktop.
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
#!ruby | |
require "open-uri" | |
require "rubygems" | |
require "sequel" | |
require "tumblr_client" | |
require "mime/types" | |
blog = "example.tumblr.com" | |
path = "likes" | |
DB = Sequel.sqlite("#{blog}.db") | |
DB.create_table?(:likes) do | |
primary_key :id | |
String :name | |
Integer :post_id | |
String :url | |
DateTime :timestamp | |
end | |
Tumblr.configure do |config| | |
config.consumer_key = "" | |
config.consumer_secret = "" | |
config.oauth_token = "" | |
config.oauth_token_secret = "" | |
end | |
tumblr = Tumblr.new | |
offset = 0 | |
while true do | |
# Tumblr APIのoffset上限は1000 | |
#︎ https://groups.google.com/forum/#!topic/tumblr-api/exUSE_ySM1g | |
offset = [offset, 1000].min | |
posts = tumblr.blog_likes(blog, :offset => offset) | |
size = posts["liked_posts"].size | |
puts "#{offset} + #{size}" | |
if size == 0 | |
puts "end!" | |
break | |
end | |
likes = DB[:likes] | |
posts["liked_posts"].each do |post| | |
unless post["type"] == "photo" | |
puts "skip type #{post["type"]}" | |
next | |
end | |
if likes.where(:post_id => post["id"]).first | |
puts "already exists #{post['id']}" | |
next | |
end | |
post["photos"].each_with_index do |photo, i| | |
image_url = photo["original_size"]["url"] | |
puts image_url | |
begin | |
open(image_url) do |image| | |
extension = MIME::Types[image.content_type].first.sub_type.gsub("jpeg", "jpg") | |
file_name = "#{path}/#{post['timestamp']}_#{post['id']}_#{i}.#{extension}" | |
puts file_name | |
open(file_name, "wb") do |file| | |
file.write(image.read) | |
end | |
timestamp = Time.at(post["timestamp"]).strftime("%Y%m%d%H%M.%S") | |
system "touch -t #{timestamp} #{file_name}" | |
end | |
rescue OpenURI::HTTPError => e | |
puts "#{e.io.status[0]} #{post['id']}" | |
end | |
sleep 1 | |
end | |
likes.insert( | |
:name => post["blog_name"], | |
:post_id => post["id"], | |
:url => post["post_url"], | |
:timestamp => post["timestamp"], | |
) | |
end | |
if offset == 1000 | |
puts "1000!" | |
break | |
end | |
offset += size | |
sleep 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment