#!/usr/bin/env ruby ## tumblr dashboard => RSS generator ## by jamie wilkinson require 'rubygems' require 'mechanize' require 'rss/maker' email = "you@example.com" password = "secret" pages = 3 feed_title = "tumblr dashboard for #{email}" class String def strip_html(allowed = ['a','img','p','br','i','b','u','ul','li']) str = self.strip || '' str.gsub(/<(\/|\s)*[^(#{allowed.join('|') << '|\/'})][^>]*>/,'') end end ## start agent = WWW::Mechanize.new agent.user_agent_alias = "Mac FireFox" agent.get("http://www.tumblr.com/login") form = agent.page.forms[0] form.email = email form.password = password agent.submit(form) ## go back to dashboard posts = [] (1..pages).each { |i| i = '' if i == 1 agent.get("http://www.tumblr.com/dashboard/#{i}") start = (i == '' ? 1 : 0) # 1st post on 1st page isn't a real post posts += agent.page.search('#posts li.post')[start..-1] sleep 2 } ## generate RSS content = RSS::Maker.make("2.0") { |m| m.channel.title = feed_title m.channel.link = "http://www.tumblr.com/dashboard" m.channel.description = "Latest from yr tumblr feed" # m.items.do_sort = true # sort items by date posts.each { |post| i = m.items.new_item title = post.search('.username').innerHTML.strip_html([]).gsub(/:$/,'') i.title = title i.link = post.search('a')[0].attributes['href'] # just use whatever link is first i.description = post.search('.post_container') # ghetto, should strip some stuff # i.date = Time.now # they don't give us a time, bastards } } ## write to disk # destination = "tumblr-dashboard-rss.xml" # File.open(destination,"w") { |f| # f.write(content) # } ## output now puts "Content-Type: application/rss+xml\n\n" puts content