Skip to content

Instantly share code, notes, and snippets.

@danicra
Forked from joshwand/tumblr-likes-downloader.rb
Last active January 4, 2016 03:29
Show Gist options
  • Save danicra/8561999 to your computer and use it in GitHub Desktop.
Save danicra/8561999 to your computer and use it in GitHub Desktop.
require 'tumblr_client'
require 'mechanize'
Tumblr.configure do |config|
config.consumer_key = "consumer_key"
config.consumer_secret = "consumer_secret"
config.oauth_token = "oath_token"
config.oauth_token_secret = "token_secret"
end
THREADPOOL_SIZE = 4
SEGMENT_SIZE = 25
directory = "tumblr-likes"
client = Tumblr::Client.new
likes = client.likes
liked_count = likes["liked_count"]
puts liked_count
likes = []
(0...liked_count).each do |i|
if i==0 or i%SEGMENT_SIZE==0
p "getting #{SEGMENT_SIZE} more likes starting at #{i}: #{likes.count} thus far"
client.likes({:limit => SEGMENT_SIZE, :offset => i})["liked_posts"].each do |like|
likes << like
end
end
end
puts "#{likes.count} likes!"
# some of this code comes from https://github.com/jamiew/tumblr-photo-downloader
already_had = 0
threads = []
likes.each_slice(likes.count / THREADPOOL_SIZE ).each do |group|
threads << Thread.new {
begin
p "launching thread #{threads.size + 1}"
group.each do |like|
i = 0
if (like["type"]=="photo")
like["photos"].each do |photo|
url = photo["original_size"]["url"]
file = Mechanize.new.get(url)
filename = "#{like["blog_name"]}-#{like["slug"]}-"
filename += "#{i}-" if i > 0
filename += File.basename(file.uri.to_s.split('?')[0])
if File.exists?("#{directory}/#{filename}")
puts "Already have #{url}"
already_had += 1
else
puts "Saving photo #{url}"
file.save_as("#{directory}/#{filename}")
end
end
end
i += 1
end
rescue Mechanize::ResponseCodeError
puts "Error getting file, #{$!}"
end
p "closing thread"
}
end
threads.each{|t| t.join }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment