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: , ,

Save to - del.icio.us - Digg it - reddit - StumbleUpon

RSS feed | Trackback URI

2 Comments »

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

 

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

 
Name (required)
E-mail (required - never shown publicly)
Website / Blog address:
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line=""> in your comment.

Subscribe without commenting

`

On This Day...