If I had your schedule...

Last nights tangent back into the Ruby world got me motivated to look a little deeper into my Ruby closet. One of the tidbits I found was this little script I hacked together last fall. It’s also somewhat related to my tiny little script from last night, but in this case I’m using the mechanize gem instead of simple screen scraping with the net/http library I mentioned last night…why you ask? Well in this particular case, the data I want to get at is behind a login, and I need to keep that session alive to get at the data I want…so mechanize just makes that simple. OK so what does this script actually do and why did I build it? To answer that I have to first give you a little background. Last season I just missed the playoffs in one of my fantasy football leagues, and as is usually the case when that happens, I wanted to gripe and complain. But being the ‘fantasy football expert’ of the league (simply because I’m in a magazine or two), I knew it wouldn’t be good enough to just say “If I had your schedule or if I was in your division, I would have walked into the playoffs backwards and blindfolded”…I needed to have some hard data before I shared my gripes with the league. So this script was built to swap my team with any other specified team in the league to see how I would have fared…the idea being that we would all have had the same rosters and started the same people for a given week regardless of what our actual schedules were (this isn’t really the case, but I think it’s a fair enough assumption for making my argument). With all of that in mind, here’s the simple script I threw together (with username/passwords changed to protect the guilty of course):

# program to compare schedules on commissioneronline (if I played another team’s schedule) # 1. log into commissioneronline.com # 2. get the details for each team # 3. change around to see results require 'rubygems’ require 'mechanize’ agent = WWW::Mechanize.new page = agent.get('http://www.commissioneronline.com’) form = page.form('frmLogonFF’) form.fields.find {|f| f.name == 'logon’}.value = 'mycommissionerusername’ form.fields.find {|f| f.name == 'password’}.value = 'mycommissionerpassword’ page = agent.submit(form) forteam, againstteam = {}, {} 12.times do |t|   team = t + 1   puts “checking team #{team}”   forteam[“#{team}”] = []   againstteam[“#{team}”] = []   followed = false   page.links.each do |link|     if link.href == “../League/tmHome.asp?tid=#{team}” && followed == false       followed = true       teamdet = agent.click link       show = false       teamdet.search(“//td”).each do |td|         if td.to_s.downcase.include?(“@home”) && !td.to_s.downcase.include?(“bodytext”)           show = true         end         if show           if td.to_s.downcase.include?(“&nbsp;”) && td.to_s.downcase.include?(“ - ”)             line = td.to_s             startspot = line.index(“&nbsp;”) + 6             endspot = line.index(“&nbsp;”, startspot)             temp = line[startspot, endspot - startspot]             if temp != nil               dashspot = temp.index(“ - ”)               yours = temp[0, dashspot]               theirs = temp[dashspot + 3, temp.length - dashspot]               forteam[“#{team}”].push(yours.to_f)               againstteam[“#{team}”].push(theirs.to_f)             end           end         end       end     end   end end # now I have all the for and against values for every team; so I can do the swap out comp1 = ARGV[0] comp2 = ARGV[1] team1 = [] count, wins, loss = 0, 0, 0 forteam[comp1].each do |points|   # for each score they got, see how they would have matched against other team   if points == againstteam[comp2][count]     # this is the week they played each other; so keep result     if points > forteam[comp2][count]       team1.push(“win #{points} - #{forteam[comp2][count]}”)       wins += 1     else       team1.push(“loss #{points} - #{forteam[comp2][count]}”)       loss += 1     end   elsif points > againstteam[comp2][count]     # they would have won this game     team1.push(“win #{points} - #{againstteam[comp2][count]}”)     wins += 1   elsif points < againstteam[comp2][count]     # they would have lost this game     team1.push(“loss #{points} - #{againstteam[comp2][count]}”)     loss += 1   end   # see how you would have matched against their opp end puts “team #{comp1} would have #{wins} wins and #{loss} losses if they played team #{comp2} schedule.”

Since it was purely for supporting (or debunking) my own little gripes I didn’t want to spend a lot of time putting this together – I wrote and tested it all in about an hour last year. And of course I didn’t comment it or get as fancy with the design or features as I might if I thought I would be reusing or expanding it (or even sharing it like this – guess that’s a lesson learned!). In the end, it worked well enough for what I wanted…and in case you are curious, it did support my thought that had I played most other schedules in the league, I would have made the playoffs…but at the same time, it also showed me that had I played the same schedule as the guy who actually won my division played, I wouldn’t have been anywhere near the playoffs – and had he played my schedule he would have been undefeated! Yikes. Of course I’ve kept that bit of information to myself until now (I guess time does heal some wounds…well that and it’s just about time to draft again this year and so I can finally start forgetting about last year and looking towards this one to avenge myself).

This post has received 40 loves.


ARCHIVE OF POSTS



This is the personal blog of Kevin Marshall (a.k.a Falicon) where he often digs into side projects he's working on for digdownlabs.com and other random thoughts he's got on his mind.

Kevin has a day job as CTO of Veritonic and is spending nights & weekends hacking on Share Game Tape. You can also check out some of his open source code on GitHub or connect with him on Twitter @falicon or via email at kevin at falicon.com.

If you have comments, thoughts, or want to respond to something you see here I would encourage you to respond via a post on your own blog (and then let me know about the link via one of the routes mentioned above).