Created
January 4, 2010 11:44
-
-
Save markoa/268475 to your computer and use it in GitHub Desktop.
Sketch for parsing JSON representation of a Tumblr blog.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Parse Tumblr JSON. | |
require 'rubygems' | |
require 'json' | |
require 'net/http' | |
class String | |
def strip_html | |
attribute_key = /[\w:_-]+/ | |
attribute_value = /(?:[A-Za-z0-9]+|(?:'[^']*?'|"[^"]*?"))/ | |
attribute = /(?:#{attribute_key}(?:\s*=\s*#{attribute_value})?)/ | |
attributes = /(?:#{attribute}(?:\s+#{attribute})*)/ | |
tag_key = attribute_key | |
tag = %r{<[!/?\[]?(?:#{tag_key}|--)(?:\s+#{attributes})?\s*(?:[!/?\]]+|--)?>} | |
self.gsub(tag, '').gsub(/\s+/, ' ').strip | |
end | |
end | |
stream = Net::HTTP.get 'blog.plakatt.com', '/api/read/json' | |
stream.gsub!("var tumblr_api_read = ", "") | |
stream = stream[0..stream.size-3] # strip ';\n' | |
js = JSON.parse stream | |
js.each do |item| | |
puts "======================" | |
puts item.class | |
puts item.first | |
if item.first == "posts" | |
posts = item.last | |
posts.each do |post| | |
puts "----------------------" | |
if post["type"] == "photo" | |
puts "Photo - " + post["photo-caption"].strip_html[0..31] + "…" | |
elsif post["type"] == "link" | |
puts post["link-text"] | |
elsif post["type"] == "regular" | |
puts post["regular-title"] | |
#puts post.inspect | |
end | |
puts post["url"] | |
puts Date.parse(post["date-gmt"]) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment