Skip to content

Instantly share code, notes, and snippets.

@mwunsch
Forked from jmahoney/tumblr-backup.rb
Created June 3, 2010 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwunsch/423221 to your computer and use it in GitHub Desktop.
Save mwunsch/423221 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# Copyright (c) 2010 Joe Mahoney
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# 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 "ruby-debug"
#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