Skip to content

Instantly share code, notes, and snippets.

@joshwand
Last active May 6, 2019 11:23
Show Gist options
  • Save joshwand/5402126 to your computer and use it in GitHub Desktop.
Save joshwand/5402126 to your computer and use it in GitHub Desktop.
download your liked photos from tumblr
require 'tumblr_client'
require 'mechanize'
require 'date'
require 'yaml'
require 'uri'
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
HISTORY_FILE = File.join(File.dirname(__FILE__), "previous.yml")
previous_dled_ids = YAML.load(File.open(HISTORY_FILE)) rescue []
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 if like['type'] == 'photo' and !previous_dled_ids.include?(like['id'])
end
end
end
if likes.empty?
p "no new likes!"
exit 0
end
puts "#{likes.count} new 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
like["photos"].each do |photo|
url = photo["original_size"]["url"]
filename = "#{like["blog_name"]}-#{like["slug"]}-"
filename += "#{i}-" if i > 0
filename += File.basename(URI.parse(url).path.split('?')[0])
if File.exists?("#{directory}/#{filename}")
puts "Already have #{url}"
already_had += 1
else
begin
puts "Saving photo #{url}"
file = Mechanize.new.get(url)
file.save_as("#{directory}/#{filename}")
rescue Mechanize::ResponseCodeError => e
puts "Error #{e.response_code} getting file for #{url}"
end
end
i += 1
previous_dled_ids << like['id']
end
end
rescue Exception => e
puts "unhandled exception:, #{$!}"
end
p "closing thread"
}
end
threads.each{|t| t.join }
YAML.dump(previous_dled_ids, File.open(HISTORY_FILE, "w"))
@yurijmi
Copy link

yurijmi commented Feb 17, 2016

Kudos for using Ruby

@Neofaucheur
Copy link

@pfoax as far as I can tell it's because tumblr no longer support SSLv3 (which is normal, since it was definitively dropped and replaced by TLS) and tumblr_client still try to use it.

@andrscrrn
Copy link

I created a javascript version of this and it's called tumblr-lks-downldr.You can use it as an NPM module (https://www.npmjs.com/package/tumblr-lks-downldr) or as a CLI tool (https://www.npmjs.com/package/tumblr-lks-downldr-cli). I would suggest the second one if you just one to download the liked posts without writing any code.

Please let me know if that works.

@digudc
Copy link

digudc commented Sep 6, 2016

hello geeks,
i am don't know nothing about ruby and programming in windows. could anybody please help me with step by step instructions to get my liked photos
i have downloaded ruby and notepate++ and saved this above script . as ruby format and run from admin cmd console .. after i get this error message

c:>"Users\xxxxx\Desktop\tumblr-likes-downloader.rb"
C:/Ruby23-x64/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require' : cannot load such file -- tumblr_client (LoadError) from C:/Ruby23-x64/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55 :inrequire'
from C:/Users/xxxxxx/Desktop/tumblr-likes-downloader.rb:1:in `

'

Many thanks ..

@martinoskam
Copy link

martinoskam commented Jan 8, 2017

Not pretending to have any major knowledge about ruby or programming in general. However, some questions above are pretty basic so i’ll write down the steps i took to make this work on my mac. I used the fork zbas made since i had more than 1000 likes to download.

  • Register a tumblr app to get the consumer_key and the consumer_secret.
    You can do this here: www.tumblr.com/oauth/apps
  • Use the in browser tumblr_api console tumblr provides to get the oauth_token and oath_token_secret for my tumblr account.
    This can be had here: https://api.tumblr.com/console/
  • Paste all of the above in the script fork of zbas.
  • install the latest version of ruby using homebrew
    Command in terminal in iMac: brew install ruby
  • Clone the git repository for the required gem tumblr_client to get the before functionality since this is only contained in the master version.
    Command in terminal on iMac: git clone https://github.com/tumblr/tumblr_client.git
  • Build the tumblr_client gem from the cloned repository.
    Command in terminal on iMac(in the repository directory): gem build tumblr_client.gemspec
  • Install both the generated tumblr_client gem and the required mechanize gem.
    Commands in terminal on iMac(in the repository directory to pick the correct client):
    gem install tumblr_client-0.8.5.gem
    gem install mechanize
  • run the ruby script in terminal using the command:
    ruby tumblr-likes-downloader.rb

Many thanks to all who contributed and mbas for his fork.

Copy link

ghost commented Jan 13, 2017

I am getting this error (Mac OS Sierra):

tumblr-likes-downloader.rb:37:in <main>': undefined method each' for nil:NilClass (NoMethodError)

Any advice?

@neuro-sys
Copy link

If you are not able to run this, here I made one as well:

https://github.com/neuro-sys/tumblr-downloader-client

@gonejack
Copy link

If any one like node.js, I made one too https://github.com/gonejack/tumblr-likes-downloader

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment