Tuesday, October 31, 2006

yahoo fantasy basketball live scoring greasemonkey script

Updated 2007-Nov-04: Updated the script to calculate team totals and also maintain roster order as it appears on management page.

Updated 2006-Nov-05: I cleaned up the boxscore processing code to implement a feature my friend Simon requested: Viewing the live stats of other managers' teams. Be sure to update the script to use the new features.

I didn't write a baseball script until after the all-star break and I passed on writing a football script since it wouldn't save me much time. But tonight I finished a greasemonkey script that provides live scoring for yahoo's fantasy basketball leagues.

To use it, download the newest version of greasemonkey and then install the script. Yahoo provides live scoring until nov 11 so you've got plenty of time to dissect the code to make sure it's safe :) Don't worry, unless someone has hacked into my code repo (very unlikely since it's hosted by google), the script does not contain nefarious code. That's always my biggest concern when installing scripts written by others since it's so easy to place code then sends data to other remote servers. That said, the script is provided as is so if it drops Kobe or Lebron from your team then oops :) I'm only kidding...I've tested it on windows xp prof running firefox 2 and the latest version of greasemonkey and have had no problems.

Here's a screenshot of the script in action. Note that the bench players have a darker background color:

The logic of the script was similar to the baseball one. First, I parsed out the boxscore links and then I made an ajax request to retrieve the contents of each boxscore. Finally, I used xpath to find the stats for my player. The techniques I used for xpath and ajax are explained in detail here. Although the business logic for the script is similar, I employed a vastly different view for presentation. Instead of embedding the stats directly into the existing table cells like in the baseball script, I placed the basketball stats inside a modal window.

Using a modal window greatly simplified my code because I didn't have to keep track of each stat and its corresponding display cell. Instead, I could copy the row node from the boxscore and place it directly into my modal window without any extra processing. I also noticed that my fantasy football team's page on sunday night no longer had placeholders for stats. The modal will bypass this problem should yahoo implement something similar for fantasy basketball.

There are a couple of different methods to implement modals. The contents of a modal can be from an iframe or a hidden div. My script uses the hidden div technique since the stats are added to the DOM on the fly. If you're interested in implementing something similar, a must read is this blog posting on modal windows. I used that technique along with some others that I picked up while implementing modals at work. In fact, the autocentering code is from an enhanced, iframe-based modal library called submodal that my boss Todd has greatly enhanced.

One bug that took me a while to track down was that some players made multiple ajax requests. I added logging using greasemonkey's GM_log api and discovered it was related to my xpath expression:
//table[@id='statTable0']/tbody/tr/td[@class='player']/div/a[@href]

this expression finds all player profile links but it also found links to the 'breaking news' about the player. I fixed the problem by adding an extra check for the a tag: a[@class='name' and @href] ... problem solved :)

Enjoy the script!

6 comments:

jyeee said...

Great stuff!
Thanks!!!

Anonymous said...

It's great! Is there a way you could calculate fantasy points by adding on to this script? That would make it just like stattracker! Thanks!

pdxrocks said...
This comment has been removed by the author.
Anonymous said...

script is awesome... Thanks a lot.. only way something could beat this script if it became a complete stat tracker calculating avg and dispalying with ur matchups.. that will be asking for a lot.. i am happy with this.. thx

melinda said...

That's great. Thank you so much to share about this topic in this blog post. I like all comments in this post. Thanks for sharing. Keep it up.



olympics

generic cialis 20mg said...

Hi, well be sensible, well-all described