Ruby: Turn bash.org quotes into a fortune file

The following code turns XML quotes from bash.org (a popular IRC quotes server) into a fortune-cookie file.
Handy for using with ../emacs/flashcard.el and my ../emacs/flashcard-config.el, which pops up a fortune
every time I get a correct answer.

bash-org-to-fortune.rb:

require 'rss/1.0'
require 'cgi'
require 'net/http'
host = Net::HTTP.new('bash.org', 80)
if ARGV[0] then
   resp, data = host.get('http://bash.org/xml/?top&below=' + ARGV[0], nil)
else
   resp, data = host.get('http://bash.org/xml/?top', nil)
end
parsed = RSS::Parser.parse(data, false)
parsed.items.each { |x| puts CGI::unescapeHTML(x.description.gsub('
', "\n")); puts "%\n" }

Call like this:

ruby bash-org-to-fortune.rb > bash; strfile bash; fortune bash

# or to get the top quotes with score < 1000
ruby bash-org-to-fortune.rb 1000 > bash; strfile bash; fortune bash

On Technorati: , ,

  • John Bigboote

    Looks like this no longer works:

    /usr/lib/ruby/1.8/rss/rexmlparser.rb:24:in `_parse': This is not well formed XML (RSS::NotWellFormedError)
    Missing end tag for ‘hr’ (got “body”)
    Line:
    Position:
    Last 80 unconsumed characters:
    from /usr/lib/ruby/1.8/rss/parser.rb:163:in `parse’
    from /usr/lib/ruby/1.8/rss/parser.rb:78:in `parse’
    from bash-org-to-fortune.rb:10

  • http://sachachua.com Sacha Chua

    Yes, for some reason bash.org/xml gives a Permission Denied error. Try it against http://feeds.feedburner.com/latest-bash-quotes instead.