Skip to content

Instantly share code, notes, and snippets.

@ragekit
Created July 11, 2013 13:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ragekit/5975512 to your computer and use it in GitHub Desktop.
Save ragekit/5975512 to your computer and use it in GitHub Desktop.
Easy way to backup your tumblr likes
require "oauth"
require 'json'
require 'uri'
#CONFIG :
oauthConsumer = "consumerkey"
oauthSecret ="consumersecret"
tumblrUsername="mail"
tumblrPass="password"
#the folder you want to save images relative to script
savefolder = "images/"
# BEGIN SCRIPT :
@consumer = OAuth::Consumer.new oauthConsumer,
oauthSecret,
{:site =>'https://www.tumblr.com'}
@access_token = @consumer.get_access_token nil,
{},
{ :x_auth_mode => 'client_auth',
:x_auth_username => tumblrUsername,
:x_auth_password => tumblrPass
}
offset = 0
fileNb=0
if !FileTest::directory?(savefolder)
Dir::mkdir(savefolder)
end
while true
response = @access_token.get("http://api.tumblr.com/v2/user/likes?offset="+offset.to_s)
body = JSON.load response.body
#puts body;
puts "----------------------------"
if body["response"]["liked_posts"].empty? == true
break
end
likes = body["response"]["liked_posts"]
likes.each do |like|
if like["type"] == "photo"
folder = like["blog_name"]+"/"
photoset = like["photos"]
fileNb+=1
photoset.each do |photos|
#might be several photos in like
uri = URI(photos["original_size"]["url"])
filename = uri.path[1,uri.path.length-1]
if filename.rindex("/") != nil then
filename = filename[filename.rindex("/")+1, filename.length]
end
if(!File.exist?(savefolder+folder+filename)) then
puts "downloading file " + fileNb.to_s + " : " + filename
resp = Net::HTTP.get_response(uri)
if resp.code != "200"
then puts resp.code
end
#check if directory exists
if !FileTest::directory?(savefolder+folder)
Dir::mkdir(savefolder+folder)
end
open(savefolder+folder+filename, "wb") { |file|
file.write(resp.body)
}
end
end
end
end
offset +=20
end
puts "complete !"
@ragekit
Copy link
Author

ragekit commented Jul 11, 2013

Require ruby and the oauth gem

How : use

gem install oauth

if it fails (ex : on osx, with the default ruby) :

sudo gem install oauth

then :

cd /path/to/where/you/wanna/save/images
ruby tumblrLikeBackup.rb

@sarahmp
Copy link

sarahmp commented Jan 6, 2014

I apologize in advance because I'm new to scripting...I've installed oauth, created my directories, got an API key (I THINK I did that correctly). But, where exactly do I store the actual tumblrLikeBackup.rb script? Locally? On my machine? I know, it's a dumb question, I'm sorry. I did try running it locally, in the same directory as I've set the image path to, and it fails with a message of "400 Bad Request (OAuth::Unauthorized)"

@ragekit
Copy link
Author

ragekit commented Feb 11, 2014

Sorry, i'm just seeing your message now (idunno why i don't have mail reminder set up for gist).

Yeah, you save the script locally.
Are you sure you changed the infos line 7 to 11 with yours ?

@d4ni31
Copy link

d4ni31 commented Feb 4, 2015

Yes. Same error for me. Wasn't sure what to put for "Default callback URL" tho

@nilsbyte
Copy link

nilsbyte commented Apr 3, 2016

/Library/Ruby/Gems/2.0.0/gems/oauth-0.5.1/lib/oauth/consumer.rb:217:in `token_request': 400 Bad Request (OAuth::Unauthorized)
    from /Library/Ruby/Gems/2.0.0/gems/oauth-0.5.1/lib/oauth/consumer.rb:106:in `get_access_token'

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