Ruby: Turn quotes into a fortune file

The following code turns XML quotes from (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.


require 'rss/1.0'
require 'cgi'
require 'net/http'
host ='', 80)
if ARGV[0] then
   resp, data = host.get('' + ARGV[0], nil)
   resp, data = host.get('', nil)
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”)
    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

  • Yes, for some reason gives a Permission Denied error. Try it against instead.