def getAwayShots(): for game in pbpmodels.Game.objects.all().order_by("gamePk"): with transaction.atomic(): print game.gamePk j = json.loads(api_calls.get_game(game.gamePk)) ld = j["liveData"] lineScore = ld["linescore"] game.awayShots = lineScore["teams"]["away"]["shotsOnGoal"] game.save()
def getMissedShots(): count = 0 games = [] saves = [] start = datetime.now() for game in pbpmodels.Game.objects.all(): if game.gameState not in ["1", "2", "8", "9"]: count += 1 try: j = json.loads(api_calls.get_game(game.gamePk)) ld = j["liveData"] # Plays homeMissed = 0 awayMissed = 0 tpbp = pbpmodels.PlayByPlay.objects.filter( gamePk_id=game.gamePk) pbp = {} for t in tpbp: pbp[t.eventId] = t teamid = None missed = None for play in ld["plays"]["allPlays"]: eventId = play["about"]["eventIdx"] if eventId in pbp and "team" in play: pbp[eventId].team_id = play["team"]["id"] saves.append(pbp[eventId]) if play["result"]["eventTypeId"] == "MISSED_SHOT": if play["team"]["id"] == game.homeTeam_id: homeMissed += 1 else: awayMissed += 1 game.homeMissed = homeMissed game.awayMissed = awayMissed games.append(game) except Exception as e: print e print "ISSUE WITH " + str(game.gamePk) if count % 100 == 0: print count, datetime.now() - start with transaction.atomic(): for g in games: g.save() for s in saves: s.save() games = [] saves = [] print count, datetime.now() - start for g in games: g.save() for s in saves: s.save()
def getMissedShots(): count = 0 games = [] saves = [] start = datetime.now() for game in pbpmodels.Game.objects.all(): if game.gameState not in ["1", "2", "8", "9"]: count += 1 try: j = json.loads(api_calls.get_game(game.gamePk)) ld = j["liveData"] # Plays homeMissed = 0 awayMissed = 0 tpbp = pbpmodels.PlayByPlay.objects.filter(gamePk_id=game.gamePk) pbp = {} for t in tpbp: pbp[t.eventId] = t teamid = None missed = None for play in ld["plays"]["allPlays"]: eventId = play["about"]["eventIdx"] if eventId in pbp and "team" in play: pbp[eventId].team_id = play["team"]["id"] saves.append(pbp[eventId]) if play["result"]["eventTypeId"] == "MISSED_SHOT": if play["team"]["id"] == game.homeTeam_id: homeMissed += 1 else: awayMissed += 1 game.homeMissed = homeMissed game.awayMissed = awayMissed games.append(game) except Exception as e: print e print "ISSUE WITH " + str(game.gamePk) if count % 100 == 0: print count, datetime.now() - start with transaction.atomic(): for g in games: g.save() for s in saves: s.save() games = [] saves = [] print count, datetime.now() - start for g in games: g.save() for s in saves: s.save()
def findTeam(): game_data = {} count = 0 playerlist = pbpmodels.PlayerGameStats.objects.order_by("game") plength = len(playerlist) for player in playerlist: count += 1 if count % 100 == 0: print count, plength gamePk = player.game.gamePk if gamePk not in game_data: print gamePk j = json.loads(api_calls.get_game(gamePk)) j = j["liveData"] game_data[gamePk] = {} allplayers = ["goalies", "skaters", "onIce", "scratches"] game_data[gamePk]["away"] = set() game_data[gamePk]["home"] = set() game_data[gamePk]["homegoalies"] = j["boxscore"]["teams"]["home"][ "players"] game_data[gamePk]["awaygoalies"] = j["boxscore"]["teams"]["away"][ "players"] game_data[gamePk]["awayteam"] = j["boxscore"]["teams"]["away"][ "team"]["id"] game_data[gamePk]["hometeam"] = j["boxscore"]["teams"]["home"][ "team"]["id"] game_data[gamePk]["period"] = j["linescore"]["currentPeriod"] for p in allplayers: game_data[gamePk]["away"].update( j["boxscore"]["teams"]["away"][p]) game_data[gamePk]["home"].update( j["boxscore"]["teams"]["home"][p]) """if player.player.id in game_data[gamePk]["away"]: player.team_id = game_data[gamePk]["awayteam"] elif player.player.id in game_data[gamePk]["home"]: player.team_id = game_data[gamePk]["hometeam"] else: print player.player.id, game_data[gamePk]["away"] print player.player.id, game_data[gamePk]["home"] raise Exception player.save()""" for gamePk in game_data: gd = game_data[gamePk] checkGoalies(gd["homegoalies"], gamePk, gd["hometeam"], gd["period"]) checkGoalies(gd["awaygoalies"], gamePk, gd["awayteam"], gd["period"])
def findTeam(): game_data = {} count = 0 playerlist = pbpmodels.PlayerGameStats.objects.order_by("game") plength = len(playerlist) for player in playerlist: count += 1 if count % 100 == 0: print count, plength gamePk = player.game.gamePk if gamePk not in game_data: print gamePk j = json.loads(api_calls.get_game(gamePk)) j = j["liveData"] game_data[gamePk] = {} allplayers = ["goalies", "skaters", "onIce", "scratches"] game_data[gamePk]["away"] = set() game_data[gamePk]["home"] = set() game_data[gamePk]["homegoalies"] = j["boxscore"]["teams"]["home"]["players"] game_data[gamePk]["awaygoalies"] = j["boxscore"]["teams"]["away"]["players"] game_data[gamePk]["awayteam"] = j["boxscore"]["teams"]["away"]["team"]["id"] game_data[gamePk]["hometeam"] = j["boxscore"]["teams"]["home"]["team"]["id"] game_data[gamePk]["period"] = j["linescore"]["currentPeriod"] for p in allplayers: game_data[gamePk]["away"].update(j["boxscore"]["teams"]["away"][p]) game_data[gamePk]["home"].update(j["boxscore"]["teams"]["home"][p]) """if player.player.id in game_data[gamePk]["away"]: player.team_id = game_data[gamePk]["awayteam"] elif player.player.id in game_data[gamePk]["home"]: player.team_id = game_data[gamePk]["hometeam"] else: print player.player.id, game_data[gamePk]["away"] print player.player.id, game_data[gamePk]["home"] raise Exception player.save()""" for gamePk in game_data: gd = game_data[gamePk] checkGoalies(gd["homegoalies"], gamePk, gd["hometeam"], gd["period"]) checkGoalies(gd["awaygoalies"], gamePk, gd["awayteam"], gd["period"])
def ingest_pbp(): players = {} playid = 0 tplayers = pmodels.Player.objects.all() for t in tplayers: players[t.id] = t count = 0 update = [] for game in pbpmodels.Game.objects.all().order_by("gamePk"): allshootouts = [] allperiods = [] allplaybyplay = [] allplayers = [] allpgss = [] count += 1 if count % 100 == 0: print count, game.gamePk if game.gameState not in ["1", "2", "8", "9"]: homeMissed = 0 awayMissed = 0 try: j = json.loads(api_calls.get_game(game.gamePk)) homeSkaters = j["liveData"]["boxscore"]["teams"]["home"]["skaters"] homeGoalies = j["liveData"]["boxscore"]["teams"]["home"]["goalies"] homeOnIce = j["liveData"]["boxscore"]["teams"]["home"]["onIce"] homeScratches = j["liveData"]["boxscore"]["teams"]["home"]["scratches"] awaySkaters = j["liveData"]["boxscore"]["teams"]["away"]["skaters"] awayGoalies = j["liveData"]["boxscore"]["teams"]["away"]["goalies"] awayOnIce = j["liveData"]["boxscore"]["teams"]["away"]["onIce"] awayScratches = j["liveData"]["boxscore"]["teams"]["away"]["scratches"] homeIds = set(homeSkaters + homeGoalies + homeOnIce + homeScratches) awayIds = set(awaySkaters + awayGoalies + awayOnIce + awayScratches) gd = j["gameData"] # Player Info pinfo = gd["players"] for sid in pinfo: # I swear that's not a Crosby reference iid = int(sid.replace("ID", "")) if iid not in players: if iid in homeIds: team = game.homeTeam elif iid in awayIds: team = game.awayTeam else: print iid, homeIds, awayIds raise Exception player = ingest_player(pinfo[sid], team.id) players[player.id] = player # liveData ld = j["liveData"] lineScore = ld["linescore"] # Plays for play in ld["plays"]["allPlays"]: about = play["about"] pplayers = play["players"] result = play["result"] coordinates = play["coordinates"] p = pbpmodels.PlayByPlay() p.id = playid p.gamePk = game p.eventId = about["eventId"] p.eventIdx = about["eventIdx"] p.period = about["period"] p.periodTime = about["periodTime"] p.dateTime = about["dateTime"] p.playType = result["eventTypeId"] p.playDescription = result["description"] if "team" in play: p.team_id = play["team"]["id"] if result["eventTypeId"] == "MISSED_SHOT": if play["team"]["id"] == game.homeTeam_id: homeMissed += 1 else: awayMissed += 1 if "secondaryType" in result: if p.playType == "PENALTY": p.penaltyType = result["secondaryType"] else: p.shotType = result["secondaryType"] if p.playType == "PENALTY": p.penaltySeverity = result["penaltySeverity"] p.penaltyMinutes = result["penaltyMinutes"] if "strength" in result: p.strength = result["strength"]["code"] if "x" in coordinates: p.xcoord = coordinates["x"] p.ycoord = coordinates["y"] p.homeScore = lineScore["teams"]["home"]["goals"] p.awayScore = lineScore["teams"]["away"]["goals"] allplaybyplay.append(p) assist_found = False for pp in pplayers: poi = pbpmodels.PlayerInPlay() poi.play_id = playid poi.game = game poi.player_id = pp["player"]["id"] if assist_found is True and pbphelper.get_player_type(pp["playerType"]) == 6: poi.player_type = 16 poi.eventId = about["eventId"] poi.game_id = game.gamePk update.append(poi) else: poi.player_type = pbphelper.get_player_type(pp["playerType"]) if poi.player_type == 6: assist_found = True allplayers.append(poi) playid += 1 # Update gameData except Exception as e: print e print "1ISSUE WITH " + str(game.gamePk) try: pass #pbpmodels.Shootout.objects.bulk_create(allshootouts) #pbpmodels.GamePeriod.objects.bulk_create(allperiods) #pbpmodels.PlayByPlay.objects.bulk_create(allplaybyplay) #pbpmodels.PlayerInPlay.objects.bulk_create(allplayers) #pbpmodels.PlayerGameStats.objects.bulk_create(allpgss) except Exception as e: print e print "2ISSUE WITH " + str(game.gamePk) print "updating existing assists" for u in update: existing = pbpmodels.PlayerInPlay.objects.filter(play__eventId=u.eventId, player_id=u.player_id, game_id=u.game_id).update(player_type=u.player_type)
def compile_info(game): # DELETE OLD PlayerGameFilterStats.objects.filter(game_id=game).delete() GoalieGameFilterStats.objects.filter(game_id=game).delete() pbp = [x.__dict__ for x in PlayByPlay.objects.raw(queries.playbyplayquery, [game, ])] if len(pbp) > 0: homeTeam = pbp[0]["homeTeam_id"] awayTeam = pbp[0]["awayTeam_id"] playerteams = list(PlayerGameStats.objects.values("team__abbreviation", "team_id", "player_id", "player__fullName", "player__primaryPositionCode").filter(game_id=game)) goalieteams = GoalieGameStats.objects.values("team__abbreviation", "team_id", "player_id", "player__fullName", "player__primaryPositionCode").filter(game_id=game) missing = PlayerOnIce.objects.filter(game_id=game).exclude(player_id__in=[x["player_id"] for x in playerteams]).exclude(player_id__in=[x["player_id"] for x in goalieteams]) if missing.count() > 0: j = json.loads(api_calls.get_game(game)) home_id = j["liveData"]["boxscore"]["teams"]["home"]["team"]["id"] homeTeamModel = Team.objects.get(id=home_id) away_id = j["liveData"]["boxscore"]["teams"]["away"]["team"]["id"] awayTeamModel = Team.objects.get(id=away_id) homeScratches = j["liveData"]["boxscore"]["teams"]["home"]["scratches"] awayScratches = j["liveData"]["boxscore"]["teams"]["away"]["scratches"] found = set() for miss in missing: # player was on ice, but NHL lists them as scratched and gives them no end of game stats. lovely. if miss.player_id in found: continue found.add(miss.player_id) pdata = {} if miss.player_id in homeScratches: pteam_id = homeTeamModel.id pteam_abbreviation = homeTeamModel.abbreviation elif miss.player_id in awayScratches: pteam_id = awayTeamModel.id pteam_abbreviation = awayTeamModel.abbreviation else: raise Exception("This player does not exist {}".format(miss.player_id)) pgs = PlayerGameStats() pgs.team_id = pteam_id pgs.game_id = game # "team__abbreviation", "team_id", "player_id", "player__fullName", "player__primaryPositionCode" pgs.player_id = miss.player_id pgs.save() pdata["team__abbreviation"] = pteam_abbreviation pdata["team_id"] = pteam_id pdata["player_id"] = miss.player_id pdata["player__fullName"] = miss.player.fullName pdata["player__primaryPositionCode"] = miss.player.primaryPositionCode playerteams.append(pdata) p2t = {} for p in playerteams: p2t[p["player_id"]] = [p["team__abbreviation"], p["team_id"], 0, p["player__fullName"], p["player__primaryPositionCode"]] for p in goalieteams: p2t[p["player_id"]] = [p["team__abbreviation"], p["team_id"], 1, p["player__fullName"], p["player__primaryPositionCode"]] pip_data = PlayerInPlay.objects.values("player_type", "play_id", "player__fullName", "player__primaryPositionCode", "player_id").filter(play_id__in=[x["id"] for x in pbp]) pipdict = {} for poi in pip_data: if poi["play_id"] not in pipdict: pipdict[poi["play_id"]] = [] if poi["player_id"] in p2t: poi["player__fullNameTeam"] = poi["player__fullName"] + " (" + p2t[poi["player_id"]][0] + ")" pipdict[poi["play_id"]].append(poi) poi_data = PlayerOnIce.objects.values("player_id", "play_id", "player__lastName", "player__primaryPositionCode").filter(play_id__in=[x["id"] for x in pbp]) poidict = {} for poi in poi_data: poi["team_id"] = p2t[poi["player_id"]][1] if poi["play_id"] not in poidict: poidict[poi["play_id"]] = [] poidict[poi["play_id"]].append(poi) order = ["L", "C", "R", "D", "G"] for play in poidict: poidict[play] = sorted(poidict[play], key=lambda x: order.index(x["player__primaryPositionCode"])) for play in pbp: play["periodTimeString"] = str(play["periodTime"])[:-3] if play["id"] in pipdict: play["players"] = pipdict[play["id"]] else: play["players"] = [] if play["id"] in poidict: play["onice"] = poidict[play["id"]] else: play["onice"] = [] with transaction.atomic(): total = len(constants.TEAMSTRENGTHS_CHOICES) * len(constants.SCORESITUATION_CHOICES) * len(constants.PERIOD_CHOICES) count = 0 for strength in constants.TEAMSTRENGTHS_CHOICES: s = strength[0] for scoresituation in constants.SCORESITUATION_CHOICES: ss = scoresituation[0] for period in constants.PERIOD_CHOICES: p = period[0] stats = team.get_stats(pbp, homeTeam, awayTeam, p2t, s, ss, p) if stats[homeTeam]["toiseconds"] != 0: calc_team_stats(stats, game, p, s, ss, homeTeam, awayTeam) if stats[awayTeam]["toiseconds"] != 0: calc_team_stats(stats, game, p, s, ss, awayTeam, homeTeam) pstats = player.get_stats(pbp, homeTeam, awayTeam, p2t, s, ss, p) gstats = player.get_goalie_stats(pbp, homeTeam, awayTeam, p2t, s, ss, p) for teamid in pstats: for playerid in pstats[teamid]: pstat = pstats[teamid][playerid] if pstat["toiseconds"] > 0: calc_player_stats(pstat, playerid, game, teamid, p, s, ss) elif pstat["toiseconds"] < 0: raise Exception("NHL why...") for pid in gstats: gstat = gstats[pid] if gstat['toiseconds'] > 0: calc_goalie_stats(gstat, pid, game, p, s, ss)
def ingest_pbp(): players = {} playid = 0 tplayers = pmodels.Player.objects.all() for t in tplayers: players[t.id] = t count = 0 update = [] for game in pbpmodels.Game.objects.all().order_by("gamePk"): allshootouts = [] allperiods = [] allplaybyplay = [] allplayers = [] allpgss = [] count += 1 if count % 100 == 0: print count, game.gamePk if game.gameState not in ["1", "2", "8", "9"]: homeMissed = 0 awayMissed = 0 try: j = json.loads(api_calls.get_game(game.gamePk)) homeSkaters = j["liveData"]["boxscore"]["teams"]["home"]["skaters"] homeGoalies = j["liveData"]["boxscore"]["teams"]["home"]["goalies"] homeOnIce = j["liveData"]["boxscore"]["teams"]["home"]["onIce"] homeScratches = j["liveData"]["boxscore"]["teams"]["home"]["scratches"] awaySkaters = j["liveData"]["boxscore"]["teams"]["away"]["skaters"] awayGoalies = j["liveData"]["boxscore"]["teams"]["away"]["goalies"] awayOnIce = j["liveData"]["boxscore"]["teams"]["away"]["onIce"] awayScratches = j["liveData"]["boxscore"]["teams"]["away"]["scratches"] homeIds = set(homeSkaters + homeGoalies + homeOnIce + homeScratches) awayIds = set(awaySkaters + awayGoalies + awayOnIce + awayScratches) gd = j["gameData"] # Player Info pinfo = gd["players"] for sid in pinfo: # I swear that's not a Crosby reference iid = int(sid.replace("ID", "")) if iid not in players: if iid in homeIds: team = game.homeTeam elif iid in awayIds: team = game.awayTeam else: print iid, homeIds, awayIds raise Exception player = ingest_player(pinfo[sid], team.id) players[player.id] = player # liveData ld = j["liveData"] lineScore = ld["linescore"] # Plays for play in ld["plays"]["allPlays"]: about = play["about"] pplayers = play["players"] result = play["result"] coordinates = play["coordinates"] p = pbpmodels.PlayByPlay() p.id = playid p.gamePk = game p.eventId = about["eventId"] p.eventIdx = about["eventIdx"] p.period = about["period"] p.periodTime = about["periodTime"] p.dateTime = about["dateTime"] p.playType = result["eventTypeId"] p.playDescription = result["description"] if "team" in play: p.team_id = play["team"]["id"] if result["eventTypeId"] == "MISSED_SHOT": if play["team"]["id"] == game.homeTeam_id: homeMissed += 1 else: awayMissed += 1 if "secondaryType" in result: if p.playType == "PENALTY": p.penaltyType = result["secondaryType"] else: p.shotType = result["secondaryType"] if p.playType == "PENALTY": p.penaltySeverity = result["penaltySeverity"] p.penaltyMinutes = result["penaltyMinutes"] if "strength" in result: p.strength = result["strength"]["code"] if "x" in coordinates: p.xcoord = coordinates["x"] p.ycoord = coordinates["y"] p.homeScore = lineScore["teams"]["home"]["goals"] p.awayScore = lineScore["teams"]["away"]["goals"] allplaybyplay.append(p) assist_found = False for pp in pplayers: poi = pbpmodels.PlayerInPlay() poi.play_id = playid poi.game = game poi.player_id = pp["player"]["id"] if assist_found is True and fancystats.player.get_player_type(pp["playerType"]) == 6: poi.player_type = 16 poi.eventId = about["eventId"] poi.game_id = game.gamePk update.append(poi) else: poi.player_type = fancystats.player.get_player_type(pp["playerType"]) if poi.player_type == 6: assist_found = True allplayers.append(poi) playid += 1 # Update gameData except Exception as e: print e print "1ISSUE WITH " + str(game.gamePk) try: pass #pbpmodels.Shootout.objects.bulk_create(allshootouts) #pbpmodels.GamePeriod.objects.bulk_create(allperiods) #pbpmodels.PlayByPlay.objects.bulk_create(allplaybyplay) #pbpmodels.PlayerInPlay.objects.bulk_create(allplayers) #pbpmodels.PlayerGameStats.objects.bulk_create(allpgss) except Exception as e: print e print "2ISSUE WITH " + str(game.gamePk) print "updating existing assists" for u in update: existing = pbpmodels.PlayerInPlay.objects.filter(play__eventId=u.eventId, player_id=u.player_id, game_id=u.game_id).update(player_type=u.player_type)
def update_game(game, players): missing_players = set() allpgss = [] allperiods = [] allplaybyplay = [] allplayers = [] homeMissed = 0 awayMissed = 0 # Get live game data j = json.loads(api_calls.get_game(game.gamePk)) gd = j["gameData"] ld = j["liveData"] boxScore = ld["boxscore"] lineScore = ld["linescore"] # Update gameData game.dateTime = gd["datetime"]["dateTime"] if "endDateTime" in gd["datetime"]: game.endDateTime = gd["datetime"]["endDateTime"] if game.gameState == 1 and gd["status"]["codedGameState"] != 1: new_game = True else: new_game = False game.gameState = gd["status"]["codedGameState"] # Get linescore information game.homeScore = lineScore["teams"]["home"]["goals"] game.awayScore = lineScore["teams"]["away"]["goals"] game.homeShots = lineScore["teams"]["home"]["shotsOnGoal"] game.awayShots = lineScore["teams"]["away"]["shotsOnGoal"] # Get boxscore information if 'teamSkaterStats' not in boxScore['teams']['home']['teamStats']: return False home = boxScore["teams"]["home"]["teamStats"]["teamSkaterStats"] away = boxScore["teams"]["away"]["teamStats"]["teamSkaterStats"] game.homePIM = home["pim"] game.awayPIM = away["pim"] game.homePPGoals = home["powerPlayGoals"] game.awayPPGoals = away["powerPlayGoals"] game.homePPOpportunities = home["powerPlayOpportunities"] game.awayPPOpportunities = away["powerPlayOpportunities"] game.homeFaceoffPercentage = home["faceOffWinPercentage"] game.awayFaceoffPercentage = away["faceOffWinPercentage"] game.homeBlocked = home["blocked"] game.awayBlocked = away["blocked"] game.homeTakeaways = home["takeaways"] game.awayTakeaways = away["takeaways"] game.homeGiveaways = home["giveaways"] game.awayGiveaways = away["giveaways"] game.homeHits = home["hits"] game.awayHits = away["hits"] cperiod = 1 # Delete old data if pbpmodels.GamePeriod.objects.filter(game=game).count() > 0: pbpmodels.GamePeriod.objects.filter(game=game).delete() if pbpmodels.PlayerInPlay.objects.filter(game=game).count() > 0: pbpmodels.PlayerInPlay.objects.filter(game=game).delete() if pbpmodels.PlayByPlay.objects.filter(gamePk=game).count() > 0: pbpmodels.PlayByPlay.objects.filter(gamePk=game).delete() if pbpmodels.PlayerGameStats.objects.filter(game=game).count() > 0: pbpmodels.PlayerGameStats.objects.filter(game=game).delete() for period in lineScore["periods"]: p = pbpmodels.GamePeriod() p.game = game p.period = period["num"] if period["num"] > cperiod: cperiod = period["num"] if "startTime" in period: p.startTime = period["startTime"] if "endTime" in period: p.endTime = period["endTime"] p.homeScore = period["home"]["goals"] p.homeShots = period["home"]["shotsOnGoal"] p.awayScore = period["away"]["goals"] p.awayShots = period["away"]["shotsOnGoal"] allperiods.append(p) if lineScore["hasShootout"]: sinfo = lineScore["shootoutInfo"] try: s = pbpmodels.Shootout.objects.get(game=game) except: s = pbpmodels.Shootout() s.game = game s.awayScores = sinfo["away"]["scores"] s.awayAttempts = sinfo["away"]["attempts"] s.homeScores = sinfo["home"]["scores"] s.homeAttempts = sinfo["home"]["attempts"] s.save() homeSkaters = j["liveData"]["boxscore"]["teams"]["home"]["skaters"] homeGoalies = j["liveData"]["boxscore"]["teams"]["home"]["goalies"] homeOnIce = j["liveData"]["boxscore"]["teams"]["home"]["onIce"] homeScratches = j["liveData"]["boxscore"]["teams"]["home"]["scratches"] awaySkaters = j["liveData"]["boxscore"]["teams"]["away"]["skaters"] awayGoalies = j["liveData"]["boxscore"]["teams"]["away"]["goalies"] awayOnIce = j["liveData"]["boxscore"]["teams"]["away"]["onIce"] awayScratches = j["liveData"]["boxscore"]["teams"]["away"]["scratches"] homeIds = set(homeSkaters + homeGoalies + homeOnIce + homeScratches) awayIds = set(awaySkaters + awayGoalies + awayOnIce + awayScratches) gd = j["gameData"] # Player Info pinfo = gd["players"] for sid in pinfo: # I swear that's not a Crosby reference iid = int(sid.replace("ID", "")) if iid not in players: if iid in homeIds: team = game.homeTeam elif iid in awayIds: team = game.awayTeam else: raise Exception('iid {} not in players'.format(iid)) player = ingest_player(pinfo[sid], team.id) players[player.id] = player # liveData ld = j["liveData"] lineScore = ld["linescore"] # Plays playid = pbpmodels.PlayByPlay.objects.values('id').latest('id')['id'] + 1 homeScore = 0 awayScore = 0 for play in ld["plays"]["allPlays"]: about = play["about"] if "players" in play: pplayers = play["players"] else: pplayers = {} result = play["result"] coordinates = play["coordinates"] p = pbpmodels.PlayByPlay() p.id = playid p.gamePk = game p.eventId = about["eventId"] p.eventIdx = about["eventIdx"] p.period = about["period"] p.periodTime = about["periodTime"] p.dateTime = about["dateTime"] p.playType = result["eventTypeId"] p.playDescription = result["description"] if "team" in play: p.team_id = play["team"]["id"] if result["eventTypeId"] == "MISSED_SHOT": if play["team"]["id"] == game.homeTeam_id: homeMissed += 1 else: awayMissed += 1 if "secondaryType" in result: if p.playType == "PENALTY": p.penaltyType = result["secondaryType"] else: p.shotType = result["secondaryType"] if p.playType == "PENALTY": p.penaltySeverity = result["penaltySeverity"] p.penaltyMinutes = result["penaltyMinutes"] if "strength" in result: p.strength = result["strength"]["code"] if "x" in coordinates and "y" in coordinates: p.xcoord = coordinates["x"] p.ycoord = coordinates["y"] if result["eventTypeId"] == "GOAL": if play["team"]["id"] == game.homeTeam_id: homeScore += 1 else: awayScore += 1 p.homeScore = homeScore p.awayScore = awayScore allplaybyplay.append(p) assist_found = False for pp in pplayers: skip_play = False # For all those preseason players that don't exist! poi = pbpmodels.PlayerInPlay() poi.play_id = playid poi.game = game try: playerfound = pmodels.Player.objects.get(id=pp["player"]["id"]) except: try: if pp["player"]["id"] not in missing_players: print "API CALL" playerdata = json.loads( api_calls.get_player(pp["player"]["id"])) if len(playerdata.keys()) > 0: playerfound = ingest_player(playerdata) else: skip_play = True missing_players.add(pp["player"]["id"]) else: skip_play = True except: skip_play = True missing_players.add(pp["player"]["id"]) poi.player_id = pp["player"]["id"] if assist_found is True and fancystats.player.get_player_type( pp["playerType"]) == 6: poi.player_type = 16 poi.eventId = about["eventId"] poi.game_id = game.gamePk else: poi.player_type = fancystats.player.get_player_type( pp["playerType"]) if poi.player_type == 6: assist_found = True if skip_play is False: allplayers.append(poi) playid += 1 game.homeMissed = homeMissed game.awayMissed = awayMissed game.save() pbpmodels.GamePeriod.objects.bulk_create(allperiods) pbpmodels.PlayByPlay.objects.bulk_create(allplaybyplay) pbpmodels.PlayerInPlay.objects.bulk_create(allplayers) hp = boxScore["teams"]["home"]["players"] ap = boxScore["teams"]["away"]["players"] homegoalies = ld["boxscore"]["teams"]["home"]["players"] awaygoalies = ld["boxscore"]["teams"]["away"]["players"] hometeam = ld["boxscore"]["teams"]["home"]["team"]["id"] awayteam = ld["boxscore"]["teams"]["away"]["team"]["id"] goaliestats = [] goaliestats.extend( checkGoalies(homegoalies, game.gamePk, hometeam, cperiod)) goaliestats.extend( checkGoalies(awaygoalies, game.gamePk, awayteam, cperiod)) allpgss.extend( set_player_stats(hp, game.homeTeam, game, players, cperiod, new_game)) allpgss.extend( set_player_stats(ap, game.awayTeam, game, players, cperiod, new_game)) pbpmodels.PlayerGameStats.objects.bulk_create(allpgss) # Find any existing POI data and delete pbpmodels.PlayerOnIce.objects.filter(game=game).delete() # Get player on ice data eventIdxs = {} for pbp in allplaybyplay: periodTime = str(pbp.periodTime) if pbp.period not in eventIdxs: eventIdxs[pbp.period] = {} if periodTime not in eventIdxs[pbp.period]: eventIdxs[pbp.period][periodTime] = {} if fancystats.constants.events[pbp.playType] not in eventIdxs[ pbp.period][periodTime]: eventIdxs[pbp.period][periodTime][fancystats.constants.events[ pbp.playType]] = [ pbp.id, ] else: eventIdxs[pbp.period][periodTime][fancystats.constants.events[ pbp.playType]].append(pbp.id) hp = {} ap = {} for ps in allpgss: if ps.team_id == game.homeTeam_id: hp[ps.player.fullName.upper()] = ps.player_id else: ap[ps.player.fullName.upper()] = ps.player_id for gs in goaliestats: if gs.team_id == game.homeTeam_id: hp[gs.player.fullName.upper()] = gs.player_id else: ap[gs.player.fullName.upper()] = gs.player_id url = BASE + str(game.season) + "/PL0" + str(game.gamePk)[5:] + ".HTM" data = api_calls.get_url(url) soup = BeautifulSoup(data, 'html.parser') evens = soup.find_all('tr', attrs={'class': 'evenColor'}) count = 0 saved = [] for row in evens: backup_names = {} cols = row.find_all('td', recursive=False) fonts = row.find_all('font') if len(list(cols[3].strings)) >= 1: time = list(cols[3].strings)[0] if len(time) < 5: time = "0" + time for ele in fonts: if ele.has_attr("title"): title = ele.attrs["title"].split(" - ")[1] number = ele.text if number in backup_names: backup_names[number + "H"] = title else: backup_names[number] = title fcols = [ele.text.strip().replace("\n", "") for ele in cols] eventIdx = int(fcols[1]) playType = fcols[4] if eventIdx in eventIdxs and time in eventIdxs[ eventIdx] and playType in eventIdxs[eventIdx][time]: players = fcols[6:] away = players[0] home = players[1] away = [x[0:-1] for x in away.replace(u'\xa0', " ").split(" ")] home = [x[0:-1] for x in home.replace(u'\xa0', " ").split(" ")] awayNames = {} homeNames = {} for f in fonts: if "title" in f: title = f["title"].split(" - ")[-1] number = f.text if number in away and number not in awayNames: awayNames[number] = title else: homeNames[number] = title acount = 1 players = set() for anum in away: if len(anum) > 0: for play_id in eventIdxs[eventIdx][time][playType]: pbpdict = {} pbpdict["play_id"] = play_id pbpdict["game_id"] = game.gamePk anum = int(anum) try: player = getPlayer( ap, awayNames, anum, backup_names, True) #ap[awayNames[str(anum)]] if player not in players: players.add(player) pbpdict["player_id"] = player acount += 1 pbpp = pbpmodels.PlayerOnIce(**pbpdict) #pbpp.save() saved.append(pbpp) except: pass hcount = 1 for hnum in home: if len(hnum) > 0: for play_id in eventIdxs[eventIdx][time][playType]: pbpdict = {} pbpdict["play_id"] = play_id pbpdict["game_id"] = game.gamePk # fix yo formatting nhl dot com hnum = int( str(hnum).replace("=\"center\">", "").replace("C", "")) try: player = getPlayer(hp, homeNames, hnum, backup_names, False) if player not in players: players.add(player) pbpdict["player_id"] = player hcount += 1 pbpp = pbpmodels.PlayerOnIce(**pbpdict) #pbpp.save() saved.append(pbpp) except: pass # Remove so there are no duplicates, first entry will have the most data eventIdxs[eventIdx][time].pop(playType, None) pbpmodels.PlayerOnIce.objects.bulk_create(saved) if game.gameState in [6, 7, 8, "6", "7", "8"]: return True return False