Skip to content

Instantly share code, notes, and snippets.

@fixlr
Created September 18, 2009 19:41
Show Gist options
  • Save fixlr/189246 to your computer and use it in GitHub Desktop.
Save fixlr/189246 to your computer and use it in GitHub Desktop.
Build a custom RSS feed for Penny Arcade comics and news posts
#!/usr/bin/env ruby
require 'rubygems'
require 'hpricot'
require 'open-uri'
require 'rss/maker'
class PA
attr_accessor :date
def initialize(d = DateTime.parse("#{Date.today} 00:00:00"))
@date = d
end
def comic
@comic ||= get(comic_url).search("div.body img").first
end
def news
@news ||= get(news_url).search("div.body").first.inner_html
end
def title; "Penny-Arcade Comics"; end;
def description; "Guys, get with it. Let's have an official RSS feed with comics."; end;
def slug; "#{@date.year}/#{@date.month}/#{@date.day}"; end;
def base_url; "http://www.penny-arcade.com"; end;
def comic_url; "#{base_url}/comic/#{slug}/"; end;
def news_url; "#{base_url}/#{slug}/"; end;
private
def get(url)
open(url) { |f| Hpricot(f) }
end
end
pa = PA.new
content = RSS::Maker.make("2.0") do |m|
m.channel.title = pa.title
m.channel.link = "#{pa.base_url}/"
m.channel.description = pa.description
m.items.do_sort = true
i = m.items.new_item
i.title = pa.comic.attributes['alt']
i.link = pa.comic_url
i.date = pa.date
i.description = "#{pa.comic.to_html}<br /><br />#{pa.news}"
end
File.open('/var/www/pa.rss', "w") do |f|
f.write(content)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment