Monday, June 20, 2016

MLB.com Fantasy Baseball Score Checker

I've been hacking away on a project for my fantasy baseball league. MLB.com's fantasy baseball has its daily scoreboard in flash, so it doesn't work on mobile.

Not only did I want to check my score on mobile, I thought having easily accessible scores on mobile would make things more engaging for the whole league!

I wrote a python script to scrape values from MLB servers and display them in a simple interface:

This league has classy team names. 
The basic flow of the script is:
  1. Get the fantasy league schedule JSON data from this URL:
    http://www.mlb.com/fantasylookup/json/named.fb_index_schedule.bam?league_id=1514
    Note the league_id can be found with CTRL-F if you "view source" on your league homepage.
  2. Process the fantasy league schedule and build a big Python dictionary called this_week (I should have built a class - maybe that's on the todo list). This step figures out all of the fantasy team_id's that belong to your league_id.
    Also, build each team's roster based on the fantasy team roster data. The period_id comes from the schedule data, and the player_ids come from here:
    http://www.mlb.com/fantasylookup/json/named.fb_team_lineup.bam?&team_id=9972&period_id=13
  3. Figure out which games are being currently played in real life. This can be done by counting the number of folders containing matching regex gid_[0-9]{4}_[0-9]{2}_[0-9]{2}[a-z0-9_]* from this directory:
    http://gdx.mlb.com/components/game/mlb/year_2016/month_04/day_25/ (substitute today's date)
    Specifically, we are looking for the boxscore.json files for each game:
    http://gdx.mlb.com/components/game/mlb/year_2016/month_04/day_25/gid_2016_04_25_chamlb_tormlb_1/boxscore.json
    We are going to use these to calculate the fantasy points for each player's real-life stats.
  4. Grab every boxscore.json file for today's real-life games, read the data and build a monster dictionary called super_boxscore.
  5. Loop through all of the fantasy player rosters in the this_week variable,  and look up the player_ids in the super_boxscore variable. Calculate fantasy points for each player_id.
  6. Show the results!
I wrote most of this blog post a month ago, intending to add more features and make a richer blog post. Sadly, like many of my projects it has stalled at nowhere near completion and I have not put in any time over the last 4-5 weeks! 

But if you are running an MLB.com fantasy league and you want a better mobile interface for your members, check out my project on Github: https://github.com/b-neufeld/mlb-fantasy-checker 

No comments:

Post a Comment