6194 comments
2357 subscribers
Follow me on Twitter (@sachac)
Subscribe! Feed reader E-mail

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

Short URL: http://sachachua.com/blog/p/3194
  • 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.

On This Day...

  • 2012: Investing time into building sketchnotes as a business — Something shifted, and I was curious about what it was. I’ve been drawing a whole lot more than I did [...]
  • 2011: Transcript: Blogging (Part 15): Tools to help you get started — Hat-tip to Holly Tse for organizing this interview! At the end of the blog series, I’ll put them all together [...]
  • 2010: Writing about lots of different kinds of things — The first change off the bat is to just start writing more varied material and see what sticks, an approach [...]
  • 2009: The shy connector’s guide to business travel — As an introvert, I often find business travel quite stressful. I know I should be making better use of the [...]
  • 2008: Notes from today’s Drupal hacking — Avatar selection/upload during registration: Oddly enough, avatar_selection doesn’t handle this. You need the reg_with_pic module, too. Shared and per-site file directories: [...]
  • 2007: Finding out if I’m overscheduled — If you’re anything like me, your task list is long and growing, but the time you have is just as fixed. [...]
  • 2005: Two years ago — Was it really just two years ago today that I learned that free tech support gets you so much more than [...]
  • 2005: To write well, one must have leisure — To write well, one must have leisure: leisure to read, leisure to think, to talk things over, to talk oneself [...]
  • 2005: Squidoo now on public beta — Squidoo opened its doors to the public today. Open beta. Try it out if you’re into Web 2.0 stuff. I’m personally [...]
  • 2005: Secret Santa exchanges the Web way — Elfster makes it easy to set up Secret Santa gift exchanges. It automates signing up, drawing people, and even anonymously asking questions. [...]
  • 2005: From elsewhere: Linux: a social experiment — Linux advocate disguised as panhandler gives CDs away. Interesting social results: most people don’t read signs, and panhandling is fairly [...]
  • 2004: Panic, panic — MIT application coming up soon. Dec 15. Nearly forgot about it. Embarrassing, as I’m into personal information management. At least my statement [...]
  • 2004: Demo tomorrow — Objective: Give Prof. Chignell some ideas about Planner so that he can introduce me to the wearable computing and personal information management [...]
  • 2004: Philippine websites ranked — From the Philippine PHP mailing list: here’s a site that lists the philippine’s top sites by alexa. the one on cebu-online doesn’t [...]
  • 2003: Burrito attempt flop. — Writing off attempt to make burritos, as lettuce makes burrito soggy. Must figure out how to do it properly. In meantime, [...]
  • 2003: Fortuitious events — Found my keys in pocket of khaki jacket. Extremely fortunate as discovered upon entering Ateneo that no classes meant buildings were closed [...]
  • 2003: Training — The normal arrangement is HRD goes into a contract with the instructor, but since it’s a short course, we’ll just send [...]
  • 2003: Problems with lphdisk — lphdisk does not seem to recognize my hibernation partition. Here is the output of fdisk -l /dev/hda: Disk /dev/hda: 20.0 GB, 20003880960 [...]