#!/usr/bin/env python

'''
This module gets the XML data that other functions use.
It checks if the data is cached first, and if not, 
gets the data from mlb.com
'''

import os
try:
    from urlib.request import urlopen
except:
    from urllib2 import urlopen

def get_scoreboard(year, month, day):
    '''
    Return the game file for a certain day matching certain criteria
    '''
    # add zeros if less than 10
    monthstr = str(month).zfill(2)
    daystr = str(day).zfill(2)
    # file
    filename = "gameday-data/year_%i/month_%s/day_%s/scoreboard.xml.gz" % (year, monthstr, daystr)
    file = os.path.join(os.path.dirname(__file__), filename)
    # check if file exits
    if os.path.isfile(file):
        data = file
    else:
        # get data if file does not exist
        data = urlopen("http://gd2.mlb.com/components/game/mlb/year_%i/month_%s/day_%s/scoreboard.xml" % (year, monthstr, daystr))
    return data

def get_box_score(game_id):
    '''
    Return the box score file of a game with matching id
    '''
    # get relevant information from game id
    year, month, day, rest = game_id.split('_', 3)
    # file
    filename = "gameday-data/year_%s/month_%s/day_%s/gid_%s/boxscore.xml" % (year, month, day, game_id)
    file = os.path.join(os.path.dirname(__file__), filename)
    # check if file exits
    if os.path.isfile(file):
        data = file
    else:
        # get data if file does not exist
        data = urlopen("http://gd2.mlb.com/components/game/mlb/year_%s/month_%s/day_%s/gid_%s/boxscore.xml" % (year, month, day, game_id))
    return data

@@ -191,18 +191,12 @@
  url
+l
 ib
-.request
+2
  imp
@@ -206,16 +206,27 @@
  urlopen
+, HTTPError
 %0Aexcept:
@@ -233,33 +233,40 @@
 %0A    from urllib
-2
+.request
  import urlopen%0A
@@ -264,16 +264,55 @@
  urlopen
+%0A    from urllib.error import HTTPError
 %0A%0Adef ge
@@ -816,32 +816,49 @@
  does not exist%0A
+        try:%0A    
         data = u
@@ -972,16 +972,129 @@
 aystr))%0A
+        except HTTPError:%0A            data = os.path.join(os.path.dirname(__file__), %22gameday-data/default.xml%22)%0A
     retu

Fix bug where remote url was not found