Skip to content

Instantly share code, notes, and snippets.

@jmahoney
Created May 26, 2010 10:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jmahoney/414328 to your computer and use it in GitHub Desktop.
Save jmahoney/414328 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# tumblr-backup.rb
# downloads posts and related info from your a tumblr blog
# and saves them to the filesystem
# in yaml format
# Requires Mark Wunsch's awesome tumblr gem : http://github.com/mwunsch/tumblr
require "rubygems"
require "tumblr"
require "yaml"
require "fileutils"
#simply write a file to the disk
def save_file(dir, file_name, content)
FileUtils.mkdir_p(dir)
File.open(File.join(dir, file_name), "w") do |out|
out.write(content)
end
end
tumblelog_name = ARGV[0]
email = ARGV[1]
password = ARGV[2]
backup_dir = File.join(ARGV[3], tumblelog_name)
#get theme and info
puts "Authenticating"
tumblr = Tumblr.new(email, password).authenticate(true).perform
tumblelog = tumblr['tumblr']['tumblelog'].map{|t| t if t["name"] == tumblelog_name}.compact[0]
puts "Saving info"
save_file(backup_dir, "theme.html", tumblelog["theme_source"])
tumblelog.delete("theme_source")
save_file(backup_dir, "#{tumblelog_name}.yaml", tumblelog.to_yaml)
#get all the posts
if tumblelog["posts"].to_i > 0
puts "Found #{tumblelog["posts"]} posts"
reader = Tumblr::Reader.new(email, password)
(tumblelog["posts"].to_i / 50.0).ceil.times do |i|
start = i == 0 ? 0 : i * 50
if start + 50 > tumblelog["posts"].to_i
num = tumblelog["posts"]
else
num = start + 50
end
puts "Downloading #{start + 1} to #{num}"
response = reader.authenticated_read(tumblelog_name, {:num => 50, :start => start}).perform
posts = Tumblr::Reader.get_posts(response)
posts.each {|p| save_file(File.join(backup_dir, 'posts'), "#{p.post_id}.yaml", p.to_yaml)}
end
puts "Done"
else
puts "No posts found"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment