Skip to content

Instantly share code, notes, and snippets.

@Snarp
Created March 8, 2017 18:09
Show Gist options
  • Save Snarp/afe1fe65ebf0f6a2a256e5d05bb1bd33 to your computer and use it in GitHub Desktop.
Save Snarp/afe1fe65ebf0f6a2a256e5d05bb1bd33 to your computer and use it in GitHub Desktop.
Simple Tumblr backup client using tumblr_client and saving in YAML format, created as an example.
require 'tumblr_client'
require 'fileutils'
require 'yaml'
@client = Tumblr::Client.new({
:consumer_key => "FILL",
:consumer_secret => "THESE",
:oauth_token => "FIELDS",
:oauth_token_secret => "IN"
})
# You can get the consumer key and secret needed above by registering an
# application here:
# https://www.tumblr.com/oauth/apps
# and the OAuth data by feeding the key and secret into the API console, here:
# https://api.tumblr.com/console/
def archive_blog(blog, offset: 0, limit: 20, sleep_for: 0.5, path: nil, **params)
params.merge! offset: offset, limit: limit
path = File.join(Dir.home, "tumblr", blog) unless path
while true
page = @client.posts(blog, params)
if page['posts'].any?
puts "Fetched #{page['posts'].count} posts at offset #{params[:offset]}. Saving..."
save(page, path)
end
if page['posts'].count == params[:limit]
params[:offset] += params[:limit]
sleep sleep_for
else # If we got too small a block of posts, we're done:
params[:offset] += page['posts'].count
puts "There appear to be no further posts to fetch. Final post count: #{params[:offset]}"
File.write File.join(path, "metadata.yml"), page['blog'].to_yaml
return path
end
end
end
def save(page, path)
timestamp = Time.at(page['posts'].last['timestamp'])
filename = File.join path, "#{timestamp.strftime('%Y%m%d_%H%M%S')}.yml"
FileUtils.mkdir_p(path) unless Dir.exist?(path)
File.write(filename, page.to_yaml)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment