Skip to content

Instantly share code, notes, and snippets.

@Snarp
Created July 20, 2016 09:29
Show Gist options
  • Save Snarp/a48cea85cb68f14d758d7d9fcff25e99 to your computer and use it in GitHub Desktop.
Save Snarp/a48cea85cb68f14d758d7d9fcff25e99 to your computer and use it in GitHub Desktop.
Ruby module for backing up/archiving public Tumblr data.
require 'typhoeus'
require 'json'
module TungleClient
EXAMPLE_API_KEY = 'fuiKNFp9vQFvjLNvx4sUwti4Yb5yGutBN4Xh10LXZhhRKjWlV4'
BASE_BLOG_URL = 'https://api.tumblr.com/v2/blog'
BASE_TAG_URL = 'https://api.tumblr.com/v2/tagged'
# NOTE: ssl_verifypeer is turned off here due to eternal font of Windows
# SSL glitches.
def self.request(url, params={})
params[:api_key] = EXAMPLE_API_KEY unless params[:api_key]
Typhoeus::Request.new(
url,
method: :get,
ssl_verifypeer: false,
followlocation: true,
params: params
)
end
# -- FETCH HASHES
#
#
def self.posts(blog_id, params={})
JSON::parse(posts_json(blog_id, params))['response']
end
def self.blog_info(blog_id, params={})
JSON::parse(blog_info_json(blog_id, params))['response']
end
def self.tagged(tag, params={})
JSON::parse(tagged_json(tag, params))['response']
end
# -- FETCH JSON
#
#
def self.posts_json(blog_id, params={})
request(posts_api_url(blog_id), params).run.body
end
def self.blog_info_json(blog_id, params={})
request(blog_info_api_url(blog_id), params).run.body
end
def self.tagged_json(tag, params={})
params[:tag] = tag
request(tagged_api_url, params).run.body
end
# -- FETCH AVATAR
#
#
# Returns the effective url, not the contents of the file.
def self.avatar(blog_id, params={})
request(avatar_api_url(blog_id), params).run.options[:effective_url]
end
# -- URL FORMATTING
#
#
def self.posts_api_url(blog_id)
blog_base_api_url(blog_id) + '/posts'
end
def self.blog_info_api_url(blog_id)
blog_base_api_url(blog_id) + '/info'
end
def self.avatar_api_url(blog_id)
blog_base_api_url(blog_id) + '/avatar'
end
def self.blog_base_api_url(blog_id)
'https://api.tumblr.com/v2/blog/' + blog_id
end
def self.tagged_api_url
'https://api.tumblr.com/v2/tagged'
end
end

TUNGLE_CLIENT "DOCUMENTATION"

This is like tumblr_client except it doesn't do OAuth and thus is easier to configure, but only accesses public data, making it appropriate for making backups or archiving tags.

The API key supplied here appears in the examples in the official Tumblr API V2 documentation, and you can your own in the params hash if they pull it, or you just feel like doing that or something, I don't care. I am many hundreds of years old.

The methods accept all the parameters described in the appropriate sections in the Tumblr API V2 documentation:

Below are some very clear examples of how to use this module. These examples could not possibly be any clearer.

INDIVIDUAL BLOGS

Get a hash representing the first 20 posts from demo.tumblr.com (except it's only got 7 posts total):

TungleClient::posts('demo.tumblr.com')

Get the first 5 posts tagged 'doge' from snarp.tumblr.com:

TungleClient::posts('snarp.tumblr.com', tag: 'doge', limit: 5)

Get the second 5 posts tagged 'doge' from snarp.tumblr.com:

TungleClient::posts('snarp.tumblr.com', tag: 'doge', limit: 5, offset: 5)

Get all 10 of those doge posts again, this time with notes and reblog info:

TungleClient::posts('snarp.tumblr.com', tag: 'doge', limit: 10,  notes_info: true, reblog_info: true)

SITE-WIDE TAGS

Get the first 20 posts tagged 'doge' site-wide:

TungleClient::tagged('doge')

Get the latest posts tagged 'homestuck' as of 11:59 PM, October 25th 2011:

TungleClient::tagged('homestuck', before: 1319587199)

OTHER STUFF

Gets the userinfo for demo.tumblr.com:

TungleClient::blog_info('demo.tumblr.com')['blog']

Gets the url for the avatar for demo.tumblr.com:

TungleClient::avatar('demo.tumblr.com')

JSON

Get demo.tumblr.com posts as a JSON string rather than a hash:

TungleClient::posts_json('demo.tumblr.com')['posts']

And the userinfo as a JSON string rather than a hash:

TungleClient::blog_info_json('demo.tumblr.com')

And exactly two posts from the 'bats' tag:

TungleClient::tagged_json('bats', limit: 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment