This post originated from an RSS feed registered with Ruby Buzz
by Vincent Foley.
Original Post: RSS feed script for comics
Feed Title: Vincent Foley-Bourgon
Feed URL: http://www.livejournal.com/~gnuvince/data/rss
Feed Description: Vincent Foley-Bourgon - LiveJournal.com
I wrote a small Ruby script to make a RSS file with some cartoons I like to read, but which do not necessarily have RSS feeds. In this script I have: Penny-Arcade, PvP Online, User Friendly, Calvin and Hobbes, The Flying McCoys and Peanuts. If you want to add more, do it. I put this script in a launchd job on my Mac, and I subscribed to it in NetNewsWire. The new version of NNW has the possibility to subscribe to a script, however it never worked with this script when I tried, so I modified it to generate the XML file in the same folder where the script file is.
Here's the script:
#!/usr/local/bin/ruby
require "rubygems"
require "builder"
require "net/http"
DIR = File.dirname(__FILE__) + File::Separator
$xml = File.open(DIR + "comics.xml", "w")
module Comics
def penny_arcade
s = Net::HTTP.get(URI.parse("http://www.penny-arcade.com/view.php3"))
s.split("\n").grep(/img.penny-arcade.com/)[0].strip[0..-6]
end
def pvponline
s = Net::HTTP.get(URI.parse("http://www.pvponline.com"))
s = s.split("\r\n").find { |ss| ss =~ Regexp.new("archive/#{Time.now.year}") }
i1 = s.index("<IMG")
i2 = s.index("</CENTER>")
"<img src=\"http://www.pvponline.com/#{s[i1...i2]}"
end
def calvin_and_hobbes
s = Net::HTTP.get(URI.parse("http://www.ucomics.com/calvinandhobbes/"))
s = s.split("\n").find { |ss| ss =~ /\/ch\// }
i1 = s.index("http")
i2 = s.index("gif") + 3
"<img src=\"#{s[i1...i2]}\">"
end
def user_friendly
year_month = Time.now.strftime("%y%b").downcase
s = Net::HTTP.get(URI.parse("http://www.userfriendly.org"))
s = s.split("\n").find { |ss| ss =~ Regexp.new(year_month) }
i1 = s.index("<IMG")
i2 = s.index("gif") + 5
s[i1...i2]
end
def flying_mccoys
s = Net::HTTP.get(URI.parse("http://www.ucomics.com/theflyingmccoys/"))
s.split("\n").find { |ss| ss =~ Regexp.new("fmc/#{Time.now.year}") }.strip
end
def peanuts
s = Net::HTTP.get(URI.parse("http://www.comics.com/comics/peanuts/"))
s = s.split("\r\n").find { |ss| ss =~ /\d\.gif.+Today's/ }
i1 = s.index("comics")
i2 = s.index("gif") + 3
"<img src=\"http://www.comics.com/#{s[i1...i2]}\">"
end
end
class ComicCollection
attr_reader :arr
def initialize
@arr = [ ]
end
def add(title, url, description)
@arr << { "title" => title,
"url" => url,
"desc" => description }
end
def add_array(arr)
arr.each { |a| add(*a) }
end
end
def build_rss_feed(comics)
xml = Builder::XmlMarkup.new(:target => $xml, :indent => 2)
xml.rss do
xml.channel do
xml.title("Comics")
xml.link("http://localhost")
xml.description("Vincent's comics")
xml.language "en-us"
xml.ttl "40"
comics.each do |comic|
xml.item do
xml.title("#{comic['title']} for: #{Time.now.strftime('%Y-%m-%d')}")
xml.description(comic["desc"])
xml.pubDate(Time.now.strftime("%a, %d %b %Y %T %Z"))
xml.guid(comic["url"])
xml.link(comic["url"])
xml.category(comic["title"])
end
end
end
end
end
def main
include Comics
cc = ComicCollection.new
comics = [["Penny-Arcade",
"http://www.penny-arcade.com",
penny_arcade],
["PvP Online",
"http://www.pvponline.com",
pvponline],
["Calvin and Hobbes",
"http://www.ucomics.com/calvinandhobbes/",
calvin_and_hobbes],
["User Friendly",
"http://www.userfriendly.org",
user_friendly],
["The Flying McCoys",
"http://www.ucomics.com/theflyingmccoys/",
flying_mccoys],
["Peanuts",
"http://www.comics.com/comics/peanuts/",
peanuts]]
cc.add_array(comics)
build_rss_feed(cc.arr)
$xml.close
end
if $0 == __FILE__
main
end
P.S: Damn it pisses me off to have to edit the post because of the HTML tags in the script. Shouldn't text in <pre> tags ignore those?