Dashboard Widgets and Python

So over the last week, I’ve had some fun with Dashboard Widgets and using Dashcode to do so. I was less-than-impressed when using Dashcode, just because there are a handful of tiny little quirks that don’t work correctly. Hopefully this stuff is fixed in the Leopard release. Anyways, you can see the result of the work under the Widgets tab in the site navigation.

MLB StandingsI created two Major League Baseball themed widgets. The first, MLB Standings, just displays the division standings for any of the 6 baseball divisions. It’s pretty basic, but it’s great to have quick access to. It’s actually something that I’ve wanted to create for a while now, but more on that later. The second widget, MLB Schedule, displays a five-day range centering around today and details either the score of a finished game or the time and TV station of a yet-to-be-played game. It was a little tougher to create, but turns out to be quite handy. I’ve got a few more features to add, too. I want to be able to choose how many days are shown in MLB Schedule, between 3, 5, 7 and 9. But, resizing the widget to fit requires more work in the way of background images. And I’ve also already added support for multiple instances.

The way these widgets work is actually pretty interesting. I did some searching around a while ago for an RSS feed or some XML page which updates with baseball information, but found absolutely nothing. (I’m still disappointed that this doesn’t exist.) So, in order to get this working, the widgets would have to parse a standard HTML page; screen scraping as it’s called. Although I am slightly familiar with screen scraping (using PHP, it’s how I display the download count for my applications on Macupdate and Version Tracker), I couldn’t find a Javascript based code base to start from. It seemed that the language just wasn’t meant for it.

MLB Schedule

And then, I learned Python and it’s amazing. I’ve already written a handful of scripts that were easy to write and fun to play with (which I’ll probably post later). And since every installation of Tiger comes with Python installed, it’s acceptable to use in the widgets. So there are Python scripts in each of these widgets, which do the actual parsing of ESPN webpages and pass all of the data back to the widget’s Javascript, where it is formatted for outputting. The scripts are simple enough and were fairly easy to write, so it’s a great combination.

Then, last night, I came up with another great way to use Python and Dashboard widgets, but in a slightly different way. Last christmas, my folks bought me a Slingbox that they keep at their house in the San Francisco Bay Area, allowing me to watch their DirecTV box from my house. It’s great because I don’t get San Francisco Giants games on local television, so I can watch them there.

One of the issues we have with the Slingbox is determining if my parents can use the television or if I’m watching it remotely. I believe there is a light on the physical box indicating one way or the other, but ours is hidden away, so that doesn’t help. So, what I did was write a small Python script that runs on my machine, the server in this scenario. It checks to see if my SlingPlayer application is open and posts to loganrockmore.com/slingbox a date/time stamp of the last check and a 0 or 1, based on if I’m watching or not. Here’s the script:

#! /usr/bin/python

# upload the status of wether the SlingPlayer application is open to the server

from commands import getoutput
from ftplib import FTP
from datetime import datetime

# see if SlingPlayer is running
if 0 < len( getoutput("ps -ax | grep [S]lingPlayer") ):
    status = 1
else:
    status = 0

# create status file
file = open("/tmp/slingboxstatus", 'w')
file.write( str(datetime.today()) + "n" + str(status) )
file.close()
file = open("/tmp/slingboxstatus")

# ftp the status to the server
ftp = FTP('ftp.loganrockmore.com')
ftp.login('##loginname##', '##password##')
ftp.storlines("STOR index.html", file)

Using unix’s crontab functionality, I’ve set it up to run every ten minutes, so I don’t even have to think about it. It can easily be done by opening a Terminal window, entering crontab -e, entering in the line */10 * * * * /Users/logan/Scripts/slingbox.py in the text editor, saving, and then running crontab -l to verify the changes. (The only thing I’m concerned about is wether FTPing every 10 minutes to my server is really a good idea.)

Slingbox WidgetThen, I created a very small Dashboard widget, which simply checks that status. Every time the Dashboard is open, the widget gets the information from online, formats it and prints it out. So, my parents can run this widget on their Mac and quickly check if I’m watching the Slingbox or not. You can download the widget in this zip file. So, yeah. Pretty cool stuff.



One Response to “Dashboard Widgets and Python”
  1. orient ????? ??????
    November 11th, 2014 at 8:00 pm

    orient ????? ??????…

    Logan Rockmore Design :: Blog » Blog Archive » Dashboard Widgets and Python…

You can follow any responses to this entry through the RSS 2.0 feed. Responses are currently closed, but you can trackback from your own site.

:: RSS Feed :: Archive :: Categories :: Admin