Sunday, August 13, 2006

greasemonkey fantasy baseball script

update: 4/12/2007 Updated script for 2007 season. update: 8/21/2006 added HR and SB support and also optimized some xpath processing.

I'm a big sports fan and one of my favorite hobbies is playing fantasy sports games on yahoo. For the season's first couple of weeks, yahoo provides real time stats for all your players. But after that, they package this feature into their 'premium services'. The only way to get live scoring is to pay for it or to click on each box score link and then scan for your player. If the game has already ended, it's actually two clicks since the initial link takes you to the game recap.

This is can be quite time consuming if you manage multiple teams but you really have no choice. ...that is, no choice until now: This weekend, I wrote a greasemonkey script that iterates through all the boxscores and parses out your player's stats for the night. Once the script has finished running, you'll have live scoring!

I've written it for default baseball leagues but a football one should follow shortly once I get around to drafting my football team. here's a screenshot of how my players performed tonight:

You can download the script here: http://freebiestats.googlecode.com/svn/trunk/baseball.user.js

Feel free to send feature requests or bug reports here: http://code.google.com/p/freebiestats/issues/list

This is my first greasemonkey script and I'm quite impressed with the whole framework. After the page renders, greasemonkey kicks in and can manipulate any part of the dom. The flow of my script is as follows:

  1. get the box score link for each player
  2. visit that link and get the player's stats
  3. update page with the stats

Firefox has a kick ass xpath implementation so finding all the box score links was pretty easy. It helps that my xpath was pretty sharp from all the xml feed projects I've been doing at work. The xpath for the batters is:
//table[@id='statTable0']/tbody/tr/td[@class='player']/div/a[@href]

The only time consuming part when developing this script was figuring a way to visit and process the box score pages. I definitely wanted to create a dom for the box score page so that I could use the aforementioned kick ass xpath functionality but my first attempt using greasemonkey's GM_xmlhttpRequest function wasn't too promising. I had no luck transforming the raw html returned by GM_xmlhttpRequest to a dom using DomParser. This must be a common problem because a google search yielded this workaround in greaseblog. That solution:

var parser = new XPCNativeWrapper(window, "DOMParser").DOMParser();
alert(parser.parseFromString("", "text/xml"));
seemed a bit hackish and it would only work for xml documents. Since the box score page is not xhtml valid or even html valid for that matter, this was not a viable option.

My second attempt was to use an iframe whose src was the box score link. This seemed to work but then I didn't have permission to access the dom directly. I kept getting permission problems. I'm guessing that firefox disabled this feature to combat cross site scripting issues. If you really want to use an iframe, you'll have to use this solution from youngpup.

My solution was to go back to using GM_xmlhttpRequest to get the raw html of the box score, saving contents within the body tag into a string, and then creating a hidden div on the current page whose innerHTML was set to that string. The box score page was now part of the current page's dom and I was able to write xpath expressions.

after that problem was solved, everything else came together rather quickly. The script is definitely an EA, 0.1 version since it's tightly coupled to how yahoo publishes box scores and fantasy manager pages and HR and SB is not one of the available stats. Ultimately, I plan to resuse a lot of this code in other fantasy games and automatically determing the order of the stats on the management page and the box score page will be a common problem that will need to be solved.

I'm currently hosting this project using google code which has an issue tracker and uses subversion as its version control system. My javascript editor is intellij idea.

13 comments:

Anonymous said...

Dave.

I just installed your script and cant wait to try it out this evening.

This is awesome.

I hope you make one for fantasy football.

again very cool

Gonzo

Anonymous said...

I just tried it out and it worked. Great Job!

Gonzo.

Dave said...

glad you found it useful...I plan to create a football one once I have a little free time

Anonymous said...

Hello! I'm got 2 e-commerce sports sites and I'm looking for a scrolling sports score script to add to them. Can you be of any assistance?

rosen.bradley@gmail.com

Anonymous said...

I cant wait to see one for football either. Keep up the nice work.

Anonymous said...

Im forming a yahoo Basketball league right now...sure would be nice;)

z said...

when i opened or even saved the file it said that there was no location so it wouldnt work. What do i do to get it to work? (I have Windows XP Professional and Internet Exlporer)

Zach said...

I dont have firefox for the internet but can I still download a script server so I can open the file? do you have any reccomendations for script openers for internet explorer?

Dave said...

zach, this script only works with firefox web browser with the greasemonkey plugin. you can learn more about greasemonkey and how to get the script working at userscripts.org

Zach said...

do you think you would ever have the time to make it work for Internet Explorer? :)

Dave said...

it would be a nice programming challenge...perhaps give me a chance to use some BHO's but I don't think anyone would be comfortable installing a dll just to get free stats. so no IE version in the works zach.

Anonymous said...

Zach, why don't you just download firefox it is waaay better than that crappy stuff they call internet explorer

here is a link for it

this link leads to all the firefox downloads

http://www.mozilla.com/en-US/firefox/all.html

Anonymous said...

zach download firefox it is waaay better than the crap you call internet explorer

http://www.mozilla.com/en-US/firefox/all.html