MLB Standings Geeklet

Guys, baseball season is days away from beginning, and I’m stoked. As a result, I’ve made something for you!

I’ve talked about GeekTool in the past. I’ve made it do some pretty fancy things. I haven’t had it installed since I got my new 13” MacBook Pro. I just haven’t had a need for it. This week, I was looking at the website for my beloved Orioles 1, I noticed that the standings table looked like it would be easy to parse. Took a look at the website for other MLB teams, and realized I could easily make a portable GeekTool script to give me the standings for any MLB division.

Here’s the end result:

It did take some fiddling to get the output right, though. Let’s start at the beginning.

Geeklet

I’m going to assume that if you’re reading this far, you have GeekTool installed [2]. You’ll need to create a new Shell Geeklet. The Geeklet will run the following command:

  curl -s http://baltimore.orioles.mlb.com/index.jsp?c_id=bal | sed '
  /table\ class=\"standings_data_table\"/,/<\/table>/ !d
  // !d
  s///g
  s///g
  s/<\/a><\/td>//g
  s//,/g
  s/<\/td>//g
  s/<\/tr>//g
  /^$/ d' > /tmp/standings.txt
  more /tmp/standings.txt | /usr/local/bin/mlbstandings.sh

You notice the first line? Replace the URL with the homepage URL of your favorite team. For example, if I were a Tigers fan, my first line would read:

 curl -s http://detroit.tigers.mlb.com/index.jsp?c_id=det | sed '

]("/index.jsp?c_id=[a-z]{2,3}")

[For those of you who haven’t seen this done before, cURL is a command which downloads the pure HTML of a website among a lot of other things. The command then pipes the output to SED which parses the HTML file for a specific section, grabs that section, and cleans it up 2.

Set the refresh to something much longer than 5 seconds. Too frequently refreshing a Geeklet is one of the major reasons that people complain about GeekTools memory and CPU usage. I set mine to refresh every 8,640 seconds (or ten times a day).

The AWK Script

The last line of the above Geeklet is

 more /tmp/standings.txt | /usr/local/bin/mlbstandings.sh

More outputs a text file line-by-line. This command pipes the output to a script (which I called mlbstandings.sh). We have to create the script.

Here’s the script’s code:

  #!/bin/bash -f
  awk '
  BEGIN     {
        FS=","
        fmtheader = "%13s\t %3s\t %3s\t %5s\t %3s\n"
        fmtdata = "%13s\t %3d\t %3d\t %5s\t%3s\n"
        printf(fmtheader, "Team", "W", "L", "PCT", "GB ");
        } \
        { 
        printf(fmtdata, $1, $2, $3, $4, $5);
        }   \
  END   {}      
  '

Make sure that single-quote is there, at the end. Save this file as “mlbstandings.sh” to your /usr/local/bin/ directory, and you’re good to go. For some reason, Geektool didn’t find the script until I provided the full path, so I imagine you’ll have to do the same.

AWK is another UNIX language for parsing and displaying text files. It’s incredibly powerful 3, and I didn’t even come close to making it sweat.

You may have to disable Geektool (or at least the group that this Geeklet is in) to see the output. I’ve tested this with at least one team in every MLB division with dummy data, and everything formats properly. If you run into any bugs, just let me know. I’m loving this.

Both the Geeklet and Shell Script are available for download here, if you don’t mind taking the easy route.

Update: Fixed a couple discrepancies between my account, here, and the version on macosxtips.co.uk which could have caused confusion.


  1. Beloved, but ultimately sucky. Such is the life of an O’s fan.  ↩︎

  2. Big ups to Bruce Barnett’s SED Tutorials.  ↩︎

  3. Bruce Barnett, again, The Man.  ↩︎

*****
Written on