Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
get all the tags of a tumblr blog
#!/usr/bin/env ruby
require "json"
require "set"
require "net/http"
require 'uri'
def fetch(uri_str, limit = 10)
raise ArgumentError, 'HTTP redirect too deep' if limit == 0
response = Net::HTTP.get_response(URI.parse(uri_str))
case response
when Net::HTTPSuccess then response
when Net::HTTPRedirection then fetch(response['location'], limit - 1)
else
response.error!
end
end
blog_name="worldpaintings"
api_key=""
info_url="http://api.tumblr.com/v2/blog/#{blog_name}.tumblr.com/info?api_key=#{api_key}"
posts=JSON.parse(fetch(info_url).body)['response']['blog']['posts']
pages = posts / 20
tags = Set.new
url="http://api.tumblr.com/v2/blog/#{blog_name}.tumblr.com/posts?api_key=#{api_key}"
pages.times do |page|
offset = page * 20
json=fetch("#{url}&offset=#{offset}").body
posts=JSON.parse(json)['response']['posts']
posts.map { |post| post['tags'] }.flatten.each { |tag| tags << tag }
end
tags.each { |tag| puts tag }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.