Skip to content

Instantly share code, notes, and snippets.

@henrik
Created December 4, 2010 14:08
Show Gist options
  • Save henrik/728203 to your computer and use it in GitHub Desktop.
Save henrik/728203 to your computer and use it in GitHub Desktop.
Simple summary-to-full-feed proxy for a RSS feed, for gf. Ruby CGI.
#!/usr/bin/env ruby
# Simple summary-to-full-feed proxy for a single, hard-coded feed.
# Caches each entry by GUID and timestamp.
FEED = "http://mylittleboudoir.com/feed/"
CACHE_DIR = "/tmp"
require "open-uri"
require "rubygems"
require "nokogiri"
feed = Nokogiri::XML(open(FEED))
feed.css('item').each do |item|
guid = item.at('guid').text[/\d+/]
time = Time.parse(item.at('pubDate').text).to_i
cache_file = File.join(CACHE_DIR, "cache_#{guid}_#{time}")
if File.exist?(cache_file)
full_entry = File.read(cache_file)
else
url = URI.parse(item.at('link').text)
raise "Link is not HTTP" unless url.is_a?(URI::HTTP)
html = Nokogiri::HTML(open(url))
full_entry = html.at('.entry').inner_html
full_entry.sub!(/<div id="iLikeThis-.*\z/m, '') # Remove crap at end.
File.open(cache_file, 'w') { |f| f.write full_entry }
end
desc_node = item.at('description')
desc_node.content = full_entry
end
puts "Content-Type: text/xml; charset=utf-8"
puts
puts feed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment