Skip to content

Commit

Permalink
fix(nhlgt): data moved around between api endpoints
Browse files Browse the repository at this point in the history
ver(nhlgt): 2.1
ver(nhlapi): 0.2
ver(platform): 2024.10.6
  • Loading branch information
toddrob99 committed Oct 27, 2024
1 parent 90ded07 commit f4d52e5
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 62 deletions.
6 changes: 5 additions & 1 deletion bots/nhl_game_threads/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import praw

__version__ = "2.0.14"
__version__ = "2.1"

DATA_LOCK = threading.Lock()

Expand Down Expand Up @@ -283,6 +283,7 @@ def run(self):
self.log.info("IT'S GAME DAY!")
self.log.debug(f"Gathering initial data for gamePk [{gamePk}]...")
game = self.nhl.game(gamePk)
game_right_rail = self.nhl.game_right_rail(gamePk)
homeAway = (
"home"
if game["homeTeam"]["id"] == self.myTeam["id"]
Expand Down Expand Up @@ -347,6 +348,7 @@ def run(self):
"myTeam": gameTime,
},
"game": game,
"game_right_rail": game_right_rail,
"game_boxscore": game_boxscore,
"game_pbp": game_pbp,
"standings": standings,
Expand Down Expand Up @@ -1685,6 +1687,7 @@ def collect_data(self):
]
self.log.debug(f"Gathering data for gamePk [{self.allData['gamePk']}]...")
game = self.nhl.game(self.allData["gamePk"])
game_right_rail = self.nhl.game_right_rail(self.allData["gamePk"])
homeAway = (
"home"
if game["homeTeam"]["id"] == self.myTeam["id"]
Expand Down Expand Up @@ -1749,6 +1752,7 @@ def collect_data(self):
"myTeam": gameTime,
},
"game": game,
"game_right_rail": game_right_rail,
"game_boxscore": game_boxscore,
"game_pbp": game_pbp,
"todayOtherGames": todayOtherGames,
Expand Down
2 changes: 1 addition & 1 deletion bots/nhl_game_threads/pynhlapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
from .constants import APP_NAME
from .api import API

__version__ = "0.1.1"
__version__ = "0.2"

logger = logging.Logger(APP_NAME)
7 changes: 7 additions & 0 deletions bots/nhl_game_threads/pynhlapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ def game(self, game_pk, **kwargs):
if json:
return json

def game_right_rail(self, game_pk, **kwargs):
url = f"{constants.RIGHT_RAIL_ENDPOINT.format(game_pk=game_pk)}"
url = self.add_kwargs_to_url(url, kwargs)
json = self.get_json(url)
if json:
return json

def game_boxscore(self, game_pk, **kwargs):
url = f"{constants.GAME_BOXSCORE_ENDPOINT.format(game_pk=game_pk)}"
url = self.add_kwargs_to_url(url, kwargs)
Expand Down
1 change: 1 addition & 0 deletions bots/nhl_game_threads/pynhlapi/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
SCOREBOARD_ENDPOINT = API_URL + "/v1/score/{ymd}"
SCHEDULE_ENDPOINT = API_URL + "/v1/schedule/{ymd}"
GAME_ENDPOINT = API_URL + "/v1/gamecenter/{game_pk}/landing"
RIGHT_RAIL_ENDPOINT = API_URL + "/v1/gamecenter/{game_pk}/right-rail"
SEASONS_ENDPOINT = "https://api.nhle.com/stats/rest/en/season?sort=%5B%7B%22property%22:%22id%22,%22direction%22:%22DESC%22%7D%5D"
GAME_BOXSCORE_ENDPOINT = API_URL + "/v1/gamecenter/{game_pk}/boxscore"
GAME_PLAYBYPLAY_ENDPOINT = API_URL + "/v1/gamecenter/{game_pk}/play-by-play"
54 changes: 35 additions & 19 deletions bots/nhl_game_threads/templates/game_stats.mako
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,54 @@
data["myTeam"] if data["homeAway"] == "home"
else data["oppTeam"]
)
awayStats = data["game_boxscore"].get("awayTeam")
homeStats = data["game_boxscore"].get("homeTeam")
#awayStats = data["game_boxscore"].get("awayTeam")
#homeStats = data["game_boxscore"].get("homeTeam")
stats = {
x.get("category", "foo"): (x.get("awayValue", "-"), x.get("homeValue", "-")) for x in data["game_right_rail"].get("teamGameStats", [])
}
placeholder = ("-", "-")
%>\
${'##'} Game Stats
||SOG|FO%|PP|PIM|Hits|Blks|
|:--|:--|:--|:--|:--|:--|:--|
||SOG|FO%|PP|PIM|Hits|Blks|GA|TA|
|:--|:--|:--|:--|:--|:--|:--|:--|:--|
## Team
|[${awayTeam["commonName"]["default"]}](${data["teamSubs"][awayTeam["abbrev"]]})|${awayStats.get("sog", "-")}|${str(round(float(awayStats["faceoffWinningPctg"]))) + "%" if awayStats.get("faceoffWinningPctg") else "-"}|\
${awayStats.get("powerPlayConversion", "-")}|\
${awayStats.get("pim", "-")}|${awayStats.get("hits", "-")}|${awayStats.get("blocks", "-")}|
|[${homeTeam["commonName"]["default"]}](${data["teamSubs"][homeTeam["abbrev"]]})|${homeStats.get("sog", "-")}|${str(round(float(homeStats.get("faceoffWinningPctg", 0)))) + "%" if awayStats.get("faceoffWinningPctg") else "-"}|\
${homeStats.get("powerPlayConversion", "-")}|\
${homeStats.get("pim", "-")}|${homeStats.get("hits", "-")}|${homeStats.get("blocks", "-")}|
|[${awayTeam["commonName"]["default"]}](${data["teamSubs"][awayTeam["abbrev"]]})|\
${stats.get("sog", placeholder)[0]}|\
${(str(round(stats.get("faceoffWinningPctg", placeholder)[0] * 100)) + "%") if stats.get("faceoffWinningPctg") else "-"}|\
${stats.get("powerPlay", placeholder)[0]}|\
${stats.get("pim", placeholder)[0]}|\
${stats.get("hits", placeholder)[0]}|\
${stats.get("blockedShots", placeholder)[0]}|\
${stats.get("giveaways", placeholder)[0]}|\
${stats.get("takeaways", placeholder)[0]}|
|[${homeTeam["commonName"]["default"]}](${data["teamSubs"][homeTeam["abbrev"]]})|\
${stats.get("sog", placeholder)[1]}|\
${(str(round(stats.get("faceoffWinningPctg", placeholder)[1] * 100)) + "%") if stats.get("faceoffWinningPctg") else "-"}|\
${stats.get("powerPlay", placeholder)[1]}|\
${stats.get("pim", placeholder)[1]}|\
${stats.get("hits", placeholder)[1]}|\
${stats.get("blockedShots", placeholder)[1]}|\
${stats.get("giveaways", placeholder)[1]}|\
${stats.get("takeaways", placeholder)[1]}|

<%
awayF = data["game_boxscore"].get("boxscore", {}).get("playerByGameStats", {}).get("awayTeam", {}).get("forwards", [])
awayF = data["game_boxscore"].get("playerByGameStats", {}).get("awayTeam", {}).get("forwards", [])
awayL = [x for x in awayF if x["position"] == "L"]
awayC = [x for x in awayF if x["position"] == "C"]
awayR = [x for x in awayF if x["position"] == "R"]
awayD = data["game_boxscore"].get("boxscore", {}).get("playerByGameStats", {}).get("awayTeam", {}).get("defense", [])
awayG = data["game_boxscore"].get("boxscore", {}).get("playerByGameStats", {}).get("awayTeam", {}).get("goalies", [])
awayD = data["game_boxscore"].get("playerByGameStats", {}).get("awayTeam", {}).get("defense", [])
awayG = data["game_boxscore"].get("playerByGameStats", {}).get("awayTeam", {}).get("goalies", [])
awaySkaters = awayF + awayD
awayOnIce_dict = data["game_pbp"].get("awayTeam", {}).get("onIce", [])
awayOnIce = [x.get("playerId") for x in awayOnIce_dict]
awaySkatersOnIce = [x for x in awaySkaters if x["playerId"] in awayOnIce]
awaySkatersOnBench = [x for x in awaySkaters if x["playerId"] not in awayOnIce]
homeF = data["game_boxscore"].get("boxscore", {}).get("playerByGameStats", {}).get("homeTeam", {}).get("forwards", [])
homeF = data["game_boxscore"].get("playerByGameStats", {}).get("homeTeam", {}).get("forwards", [])
homeL = [x for x in homeF if x["position"] == "L"]
homeC = [x for x in homeF if x["position"] == "C"]
homeR = [x for x in homeF if x["position"] == "R"]
homeD = data["game_boxscore"].get("boxscore", {}).get("playerByGameStats", {}).get("homeTeam", {}).get("defense", [])
homeG = data["game_boxscore"].get("boxscore", {}).get("playerByGameStats", {}).get("homeTeam", {}).get("goalies", [])
homeD = data["game_boxscore"].get("playerByGameStats", {}).get("homeTeam", {}).get("defense", [])
homeG = data["game_boxscore"].get("playerByGameStats", {}).get("homeTeam", {}).get("goalies", [])
homeSkaters = homeF + homeD
homeOnIce_dict = data["game_pbp"].get("homeTeam", {}).get("onIce", [])
homeOnIce = [x.get("playerId") for x in homeOnIce_dict]
Expand All @@ -52,17 +68,17 @@ ${homeStats.get("pim", "-")}|${homeStats.get("hits", "-")}|${homeStats.get("bloc
\
% for info in [(awayTeam, awaySkatersOnIce, awaySkatersOnBench, awayG, awayOnIce), (homeTeam, homeSkatersOnIce, homeSkatersOnBench, homeG, homeOnIce)]:
% if len(info[1]) or len(info[2]):
|[${info[0]["commonName"]["default"]}](${data["teamSubs"][info[0]["abbrev"]]}) Skaters|G|A|+/-|S|Blk|PIM|FO|TOI|
|[${info[0]["commonName"]["default"]}](${data["teamSubs"][info[0]["abbrev"]]}) Skaters|G|A|+/-|S|Blk|PIM|FO%|TOI|
|:--|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
% for p in info[1] + info[2]:
|^${p['position']} ${'**' if p in info[1] else ''}${playerLink(p)}${'**' if p in info[1] else ''}|\
${p.get("goals", "-")}|\
${p.get("assists", "-")}|\
${p.get("plusMinus", "-")}|\
${p.get("shots", "-")}|\
${p.get("sog", "-")}|\
${p.get("blockedShots", "-")}|\
${p.get("pim", "-")}|\
${p.get("faceoffs", "-")}|\
${(str(round(p.get("faceoffWinningPctg", 0.0) * 100)) + "%") if p.get("faceoffWinningPctg") else "-"}|\
${p.get("toi", "-")}|
% endfor
% endif
Expand Down
6 changes: 3 additions & 3 deletions bots/nhl_game_threads/templates/linescore.mako
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
data["myTeam"] if data["homeAway"] == "home"
else data["oppTeam"]
)
linescore = data["game"].get("summary", {}).get("linescore", {}).get("byPeriod", [])
linescore = data["game_right_rail"].get("linescore", {}).get("byPeriod", [])
ordDict = {1:{1:'1st',2:'2nd',3:'3rd',4:'OT',5:'SO'},2:{1:'1st',2:'2nd',3:'3rd',4:'OT',5:'SO'},3:{1:'1st',2:'2nd',3:'3rd',4:'OT1',5:'OT2',6:'OT3',7:'OT4',8:'OT5'}}
periodOrd = ordDict[data["game"]["gameType"]]
if linescore:
Expand All @@ -22,8 +22,8 @@
homeLine += f'{period["home"]}|'
headerLine += "|TOTAL|"
alignmentLine += ":--|:--|"
awayLine += f'|{data["game"].get("summary", {}).get("linescore", {}).get("totals", {}).get("away")}|'
homeLine += f'|{data["game"].get("summary", {}).get("linescore", {}).get("totals", {}).get("home")}|'
awayLine += f'|{data["game_right_rail"].get("linescore", {}).get("totals", {}).get("away")}|'
homeLine += f'|{data["game_right_rail"].get("linescore", {}).get("totals", {}).get("home")}|'
%>\
% if linescore:
${'##'} Linescore
Expand Down
62 changes: 31 additions & 31 deletions bots/nhl_game_threads/templates/post_thread.mako
Original file line number Diff line number Diff line change
Expand Up @@ -55,52 +55,52 @@ ${data["oppTeam"]["commonName"]["default"]}
% endif
%endif

% if data["game_boxscore"].get("gameVideo", {}).get("threeMinRecap") or data["game_boxscore"].get("gameVideo", {}).get("condensedGame") or data["game_boxscore"].get("boxscore", {}).get("gameReports"):
% if data["game_right_rail"].get("gameVideo", {}).get("threeMinRecap") or data["game_right_rail"].get("gameVideo", {}).get("condensedGame") or data["game_right_rail"].get("gameReports"):
${'##'} Game Videos/Summaries
% if data["game_boxscore"].get("gameVideo", {}).get("threeMinRecap"):
* [Three Minute Recap Video](${highlight_url(data["game_boxscore"]["gameVideo"]["threeMinRecap"])})
% if data["game_right_rail"].get("gameVideo", {}).get("threeMinRecap"):
* [Three Minute Recap Video](${highlight_url(data["game_right_rail"]["gameVideo"]["threeMinRecap"])})
% endif
% if data["game_boxscore"].get("gameVideo", {}).get("condensedGame"):
* [Condensed Game Video](${highlight_url(data["game_boxscore"]["gameVideo"]["condensedGame"])})
% if data["game_right_rail"].get("gameVideo", {}).get("condensedGame"):
* [Condensed Game Video](${highlight_url(data["game_right_rail"]["gameVideo"]["condensedGame"])})
% endif
% if data["game_boxscore"].get("boxscore", {}).get("gameReports"):
% if data["game_boxscore"]["boxscore"]["gameReports"].get("gameSummary"):
* [Game Summary](${data["game_boxscore"]["boxscore"]["gameReports"]["gameSummary"]})
% if data["game_right_rail"].get("gameReports"):
% if data["game_right_rail"]["gameReports"].get("gameSummary"):
* [Game Summary](${data["game_right_rail"]["gameReports"]["gameSummary"]})
% endif
% if data["game_boxscore"]["boxscore"]["gameReports"].get("eventSummary"):
* [Event Summary](${data["game_boxscore"]["boxscore"]["gameReports"]["eventSummary"]})
% if data["game_right_rail"]["gameReports"].get("eventSummary"):
* [Event Summary](${data["game_right_rail"]["gameReports"]["eventSummary"]})
% endif
% if data["game_boxscore"]["boxscore"]["gameReports"].get("playByPlay"):
* [Play by Play](${data["game_boxscore"]["boxscore"]["gameReports"]["playByPlay"]})
% if data["game_right_rail"]["gameReports"].get("playByPlay"):
* [Play by Play](${data["game_right_rail"]["gameReports"]["playByPlay"]})
% endif
% if data["game_boxscore"]["boxscore"]["gameReports"].get("faceoffSummary") or data["game_boxscore"]["boxscore"]["gameReports"].get("faceoffComparison"):
% if data["game_right_rail"]["gameReports"].get("faceoffSummary") or data["game_right_rail"]["gameReports"].get("faceoffComparison"):
* Face Off \
% if data["game_boxscore"]["boxscore"]["gameReports"].get("faceoffSummary"):
[Summary](${data["game_boxscore"]["boxscore"]["gameReports"]["faceoffSummary"]}) \
% if data["game_right_rail"]["gameReports"].get("faceoffSummary"):
[Summary](${data["game_right_rail"]["gameReports"]["faceoffSummary"]}) \
% endif
% if data["game_boxscore"]["boxscore"]["gameReports"].get("faceoffComparison"):
[Comparison](${data["game_boxscore"]["boxscore"]["gameReports"]["faceoffComparison"]})
% if data["game_right_rail"]["gameReports"].get("faceoffComparison"):
[Comparison](${data["game_right_rail"]["gameReports"]["faceoffComparison"]})
% endif
% endif
##% if data["game_boxscore"]["boxscore"]["gameReports"].get("rosters"):
##* [Rosters](${data["game_boxscore"]["boxscore"]["gameReports"]["rosters"]})
##% endif
% if data["game_boxscore"]["boxscore"]["gameReports"].get("shotSummary"):
* [Shot Summary](${data["game_boxscore"]["boxscore"]["gameReports"]["shotSummary"]})
% if data["game_right_rail"]["gameReports"].get("rosters"):
* [Rosters](${data["game_right_rail"]["gameReports"]["rosters"]})
% endif
% if data["game_boxscore"]["boxscore"]["gameReports"].get("shootoutSummary"):
* [Shootout Summary](${data["game_boxscore"]["boxscore"]["gameReports"]["shootoutSummary"]})
% if data["game_right_rail"]["gameReports"].get("shotSummary"):
* [Shot Summary](${data["game_right_rail"]["gameReports"]["shotSummary"]})
% endif
% if data["game_boxscore"]["boxscore"]["gameReports"].get("shiftChart"):
* [Shift Chart](${data["game_boxscore"]["boxscore"]["gameReports"]["shiftChart"]})
% if data["game_right_rail"]["gameReports"].get("shootoutSummary"):
* [Shootout Summary](${data["game_right_rail"]["gameReports"]["shootoutSummary"]})
% endif
% if data["game_boxscore"]["boxscore"]["gameReports"].get("toiAway") or data["game_boxscore"]["boxscore"]["gameReports"].get("toiHome"):
% if data["game_right_rail"]["gameReports"].get("shiftChart"):
* [Shift Chart](${data["game_right_rail"]["gameReports"]["shiftChart"]})
% endif
% if data["game_right_rail"]["gameReports"].get("toiAway") or data["game_right_rail"]["gameReports"].get("toiHome"):
* TOI Report: \
% if data["game_boxscore"]["boxscore"]["gameReports"].get("toiAway"):
[${awayTeam["commonName"]["default"]}](${data["game_boxscore"]["boxscore"]["gameReports"]["toiAway"]}) \
% if data["game_right_rail"]["gameReports"].get("toiAway"):
[${awayTeam["commonName"]["default"]}](${data["game_right_rail"]["gameReports"]["toiAway"]}) \
% endif
% if data["game_boxscore"]["boxscore"]["gameReports"].get("toiHome"):
[${homeTeam["commonName"]["default"]}](${data["game_boxscore"]["boxscore"]["gameReports"]["toiHome"]})
% if data["game_right_rail"]["gameReports"].get("toiHome"):
[${homeTeam["commonName"]["default"]}](${data["game_right_rail"]["gameReports"]["toiHome"]})
% endif
% endif
% endif
Expand Down
4 changes: 4 additions & 0 deletions bots/nhl_game_threads/templates/scratches.mako
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
awayScratches = data["game"].get("matchup", {}).get("gameInfo", {}).get("awayTeam", {}).get("scratches", None)
if not awayScratches:
awayScratches = data["game"].get("summary", {}).get("gameInfo", {}).get("awayTeam", {}).get("scratches", None)
if not awayScratches:
awayScratches = data["game_right_rail"].get("gameInfo", {}).get("awayTeam", {}).get("scratches", None)
if awayScratches is None:
awayScratches = []
homeScratches = data["game"].get("matchup", {}).get("gameInfo", {}).get("homeTeam", {}).get("scratches", None)
if not homeScratches:
homeScratches = data["game"].get("summary", {}).get("gameInfo", {}).get("homeTeam", {}).get("scratches", None)
if not homeScratches:
homeScratches = data["game_right_rail"].get("gameInfo", {}).get("homeTeam", {}).get("scratches", None)
if homeScratches is None:
homeScratches = []
Expand Down
12 changes: 6 additions & 6 deletions bots/nhl_game_threads/templates/skaters.mako
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
data["myTeam"] if data["homeAway"] == "home"
else data["oppTeam"]
)
awayF = data["game_boxscore"].get("boxscore", {}).get("playerByGameStats", {}).get("awayTeam", {}).get("forwards", [])
awayF = data["game_boxscore"].get("playerByGameStats", {}).get("awayTeam", {}).get("forwards", [])
awayL = [x for x in awayF if x["position"] == "L"]
awayC = [x for x in awayF if x["position"] == "C"]
awayR = [x for x in awayF if x["position"] == "R"]
awayD = data["game_boxscore"].get("boxscore", {}).get("playerByGameStats", {}).get("awayTeam", {}).get("defense", [])
awayG = data["game_boxscore"].get("boxscore", {}).get("playerByGameStats", {}).get("awayTeam", {}).get("goalies", [])
awayD = data["game_boxscore"].get("playerByGameStats", {}).get("awayTeam", {}).get("defense", [])
awayG = data["game_boxscore"].get("playerByGameStats", {}).get("awayTeam", {}).get("goalies", [])
awayOnIce = data["game_pbp"].get("awayTeam", {}).get("onIce", [])
homeF = data["game_boxscore"].get("boxscore", {}).get("playerByGameStats", {}).get("homeTeam", {}).get("forwards", [])
homeF = data["game_boxscore"].get("playerByGameStats", {}).get("homeTeam", {}).get("forwards", [])
homeL = [x for x in homeF if x["position"] == "L"]
homeC = [x for x in homeF if x["position"] == "C"]
homeR = [x for x in homeF if x["position"] == "R"]
homeD = data["game_boxscore"].get("boxscore", {}).get("playerByGameStats", {}).get("homeTeam", {}).get("defense", [])
homeG = data["game_boxscore"].get("boxscore", {}).get("playerByGameStats", {}).get("homeTeam", {}).get("goalies", [])
homeD = data["game_boxscore"].get("playerByGameStats", {}).get("homeTeam", {}).get("defense", [])
homeG = data["game_boxscore"].get("playerByGameStats", {}).get("homeTeam", {}).get("goalies", [])
homeOnIce = data["game_pbp"].get("homeTeam", {}).get("onIce", [])
def playerLink(p):
return f"[{p['name']['default']}](https://www.nhl.com/player/{p['playerId']})"
Expand Down
2 changes: 1 addition & 1 deletion redball/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env python

VERSION = "2024.10.5"
VERSION = "2024.10.6"

0 comments on commit f4d52e5

Please sign in to comment.