The folks at Linux Journal did an article on
using mplayer, a shell script,and some perl, to capture a live radio
feed for use in an iPod-esque
product.  I took the inspiration
to write this ruby code that does much thesame thing, except in this
case, we’re downloading some soccer matches I soI can watch in a Sharp
Zaurus when I’m not home.

ARGV.each do |a|
  # ARGV is loaded with the URLs to download, here we treat each URL one at a 
  # time, turning them into sane output filenames before we start the 
  # dirtywork.
  # Find the position of the proper of the feed’s name.
  position = a =~ /_tv_archive/
  team2 = a[position – 3,3]
  team1 = a[position – 6,3]
  gamedate = a[position – 13,6]
  outfile = gamedate + “-” + team1 + “-” + team2
  puts outfile # For my debugging sanity
  # Now, the dirty work
  puts `bash fetchscript.sh #{a} #{outfile}`

isn’t too much to see here that couldn’t have also done in perl, but
Idecided to do it in ruby, not for practice, and not because ruby does
itbetter (in this case, I think the equivalent in perl is faster,
benchmark itif you dare) but because this looks a lot friendlier and
easier to read.
To start from the top, I believe the perl equivalent is foreach (my arg)in @argv { block }.  While not tough to understand, I finda beauty in the language of ARGV.each do |a|, because although the.each reeks of unnecessary object oriented writing, it’s the
simplicity of the read that makes it forgivable.

ruby’s author just wanted a more predictable and object-orientedvariant
of perl, and if you’re going to have that, you have to be able to dothe
simple things that have no business being done in object
orientedlanguage, like the text processing above.

The arguements to this script are mms:// style URLs that’d be fed to mplayerone at a time.  The .each
iterator takes each one, one at a time,starting with some regex to
figure out the end of our superfluous numbersand junk that prefixes all
these kinds of feeds’ URLs.  The regex returnsthe numerical first
character position of ‘_tv_archive’ in the URL string.
The next
stanza splits out parts of the URL string for the three
letterabbreviations of the playing sides.  The third is the
date.  All are
consistently the same distance from the ‘tv_archive’ part of the URL, sothat stanza pretty much wrote itself.

we generate the name of the output file to be fed to the shell scriptin
the penultimate line.  That shell script calls mplayer to download
theURL, and mencoder to reencode it to the smaller screen size and
memoryfootprint necessary to watch in the Zaurus.

end closes the iterator block, bringing us back to the top for thenext URL until they’re all done.

Naturally, opinions are like assholes and there are still things I’d
use perl for instead.  Most of them involve its magic (weak)

Sorry I haven’t had too much to get myself
writing lately, though I’mworking on correcting that.  One of the
things I missed about my Handspringhandheld was the ability to write
blog entries virtually anytime I felt inspired to doso.  Once I
have the Zaurus working like I want it to work, I’ll be able tothat
again, and quite easily.

My condolences to all effected by
Hurricane Katrina.  I was happy to sign upto give blood and buy
donuts and cookies for the cause, and I’m thankful Ididn’t really know
anybody effected directly.

Hope everyone’s okay.

See you next time.


2 thoughts on “

  1. team2 = a[position – 3,3] I like that notation, On the otherhand I’ve been known to write and understand things like … $url=~m/(^.*)?([^?]*)$/; #(Splits a URL at the ‘?’)(And much worse, but my grep is folowing a shortcut to my other machine’s 25gb drive and getting stuck.)I really think regexp is a way of teaching a computer to be ADD.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s