Created
July 11, 2013 13:38
-
-
Save ragekit/5975512 to your computer and use it in GitHub Desktop.
Easy way to backup your tumblr likes
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
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 !" |
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)"
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 ?
Yes. Same error for me. Wasn't sure what to put for "Default callback URL" tho
/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
Require ruby and the oauth gem
How : use
if it fails (ex : on osx, with the default ruby) :
then :
cd /path/to/where/you/wanna/save/images ruby tumblrLikeBackup.rb