def getPlayersOnTeam(team): db.execute("SELECT * FROM players WHERE team = ?", (team.code,)) rows = db.fetchall() players = [] if rows is None or len(rows) < 1: return [] for player in rows: players.append(Player(*player)) return players
def getGame(team, gameday, game=1): db.execute("SELECT * FROM games WHERE (home_code = ? OR away_code = ?) AND date = ?", (team.code, team.code, gameday.strftime("%Y/%m/%d"))) rows = db.fetchall() if len(rows) == game and rows[game-1]["version"] == db_version: return Game(*rows[game-1]) try: file = urllib2.urlopen("http://gd2.mlb.com/components/game/mlb/year_%d/month_%02d/day_%02d/master_scoreboard.json" % (gameday.year, gameday.month, gameday.day)) except urllib2.HTTPError: # There's no file for this day. Maybe future, maybe too far in the past? return False # No games today? try: games = json.load(file)["data"]["games"]["game"] except KeyError: games = [] for game in games: if game["home_name_abbrev"] == team.code or game["away_name_abbrev"] == team.code: home_team = getTeam(game["home_name_abbrev"]) away_team = getTeam(game["away_name_abbrev"]) home_homers = [] away_homers = [] if "home_runs" in game: for player in game["home_runs"]["player"]: if player["team_code"] == home_team.schedule_code: home_homers.append(int(player["id"])) elif player["team_code"] == away_team.schedule_code: away_homers.append(int(player["id"])) data = dict( gameday = game["gameday"], start_time = game["time"], away_code = game["away_name_abbrev"], home_code = game["home_name_abbrev"], status = game["status"]["status"], version = db_version, away_r = game["linescore"]["r"]["away"], away_h = game["linescore"]["h"]["away"], away_e = game["linescore"]["e"]["away"], home_r = game["linescore"]["r"]["home"], home_h = game["linescore"]["h"]["home"], home_e = game["linescore"]["e"]["home"], winning_pitcher = game["winning_pitcher"]["id"] if "winning_pitcher" in game else None, losing_pitcher = game["losing_pitcher"]["id"] if "losing_pitcher" in game else None, save_pitcher = game["save_pitcher"]["id"] if "save_pitcher" in game else None, away_homers = "\n".join(away_homers), home_homers = "\n".join(home_homers), date = game["original_date"] ) game = Game(**data) #db.execute("INSERT INTO games (gameday, start_time, away_code, home_code, status, version, away_r, away_h, away_e, home_r, home_h, home_e, winning_pitcher, losing_pitcher, save_pitcher, away_homers, home_homers, date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", *data) return game return False