def main():
    """
    Tuesday, October 28, 2008, and ended on Wednesday, April 15, 2009.
    The 2009 NBA Playoffs started on Saturday, April 18, 2009 and ran
    until Sunday, June 14
    """
    start_date = [2008, 10, 28]
    end_date = [2009, 5, 14]
    dates = initGamesFromEspn.genDateSeq(start_date, end_date)
    root_url = root_dict['NBA']
    dbs = createAndReturnNbaMdbs()
    games = dbs['games']
    shots = dbs['shots']
    for date in dates:
        if int(date) < 20090418:
            season = '20082009Regular'
        else:
            season = '20082009Playoffs'
        game_ids = initGamesFromEspn.retrieveEspnGameIdsForDay(date, root_url)
        tot_ids = len(game_ids)
        print("%s game ids retrieved for %s" % (tot_ids, date))
        for game_id in game_ids:
            print("Game ID: %s" % game_id)
            game = espngames.NBAGame(game_id, season=season)
            game.initFromEspn()
            game_info_dict = game.dataToDict()
            try:
                shot_info_list = game_info_dict.pop('shots')
                shots.MongoInsert(shot_info_list)
            except KeyError:
                print('No shot data for game %s' % game_id)
            games.MongoInsert(game_info_dict)
def main():
    """
    Tuesday, October 28, 2008, and ended on Wednesday, April 15, 2009.
    The 2009 NBA Playoffs started on Saturday, April 18, 2009 and ran
    until Sunday, June 14
    """
    start_date = [2008, 10, 28]
    end_date = [2009, 6, 14]
    dates = initGamesFromEspn.genDateSeq(start_date, end_date)
    root_url = root_dict['NBA']
    dbs = createAndReturnNbaMdbs()
    games = dbs['games']
    shots = dbs['shots']
    for date in dates:
        if int(date) < 20090418:
            season = '20082009Regular'
        else:
            season = '20082009Playoffs'
        game_ids = initGamesFromEspn.retrieveEspnGameIdsForDay(date, root_url)
        tot_ids = len(game_ids)
        print("%s game ids retrieved for %s" % (tot_ids, date))
        for game_id in game_ids:
            print("Game ID: %s" % game_id)
            game = espngames.NBAGame(game_id, season=season)
            game.initFromEspn()
            game_info_dict = game.dataToDict()
            try:
                shot_info_list = game_info_dict.pop('shots')
                shots.MongoInsert(shot_info_list)
            except KeyError:
                print('No shot data for game %s' % game_id)
            games.MongoInsert(game_info_dict)
def main(season_info):

    errs = []
    try:
        for season in season_info:
            s = season['Season']
            start_date = toDateFormat(season['Start'])
            end_date = toDateFormat(season['End'])
            playoff_start = int(season['PlayoffStart'])

            print('Retrieving data for the %s Season' % s)

            dates = initGamesFromEspn.genDateSeq(start_date, end_date)
            root_url = root_dict['NBA']
            dbs = createAndReturnNbaMdbs()
            games = dbs['games']
            shots = dbs['shots']
            for date in dates:
                try:
                    if int(date) < playoff_start:
                        stype = ' '.join([s, 'Regular'])
                    else:
                        stype = ' '.join([s, 'Playoffs'])
                    game_ids = initGamesFromEspn.retrieveEspnGameIdsForDay(date, root_url)
                    tot_ids = len(game_ids)
                    print("%s game ids retrieved for %s" % (tot_ids, date))
                    for game_id in game_ids:
                        try:
                            
                            print("Game ID: %s" % game_id)
                            query = games.query({'game_id' : str(game_id)}, {'game_id' : 1}, verbose=False)
                            if query.count() > 0:
                                print("Game already exists in db; skipping")
                            else:
                                game = espngames.NBAGame(game_id, season=stype)
                                game.initFromEspn()
                                game_info_dict = game.dataToDict()
                                try:
                                    shot_info_list = game_info_dict.pop('shots')
                                    shots.insert(shot_info_list)
                                except KeyError:
                                    print('No shot data for game %s' % game_id)
                                games.insert(game_info_dict)
                        except KeyboardInterrupt:
                            raise KeyboardInterrupt
                        except:
                            print('Error encounterd at document %s' % game_id)
                            new_error = {'game_id' : game_id,
                                         'err_type':sys.exc_info()[0],
                                         'err_msg':sys.exc_info()[1]}
                            errs.append(new_error)
                except KeyboardInterrupt:
                    raise KeyboardInterrupt
                except:
                    print('Error encountered at date %s' % date)
                    new_error = {'date' : date,
                                 'err_type':sys.exc_info()[0],
                                 'err_msg':sys.exc_info()[1]}
                    errs.append(new_error)
                        
    finally:
        with open('errorsRetrievingGameData02.pkl', 'w') as f1:
            pickle.dump(errs, f1)