Skip to content

Instantly share code, notes, and snippets.

@lucapette
Created December 3, 2011 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lucapette/1427448 to your computer and use it in GitHub Desktop.
Save lucapette/1427448 to your computer and use it in GitHub Desktop.
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