From 626219aa299608cc44d4ce0124d8838253de713b Mon Sep 17 00:00:00 2001 From: Parnic Date: Sun, 21 Apr 2024 20:36:01 -0500 Subject: [PATCH] Fix playoff series display in new API This playoff endpoint data was pulled from https://github.com/Zmalski/NHL-API-Reference?tab=readme-ov-file#playoff-information --- node_helper.js | 35 +++++++++++++++-------------------- templates/MMM-NHL.njk | 12 ++++++------ 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/node_helper.js b/node_helper.js index 1ad6a8af..ca2bf9e9 100644 --- a/node_helper.js +++ b/node_helper.js @@ -27,7 +27,7 @@ const Log = require('logger'); */ const NodeHelper = require('node_helper'); -const BASE_PLAYOFF_URL = 'https://statsapi.web.nhl.com/api/v1/tournaments/playoffs?expand=round.series'; +const BASE_PLAYOFF_URL = 'https://api-web.nhle.com/v1/playoff-series/carousel'; /** * Derived team details of a game from API endpoint for easier usage. @@ -253,16 +253,18 @@ module.exports = NodeHelper.create({ * @returns {object} Raw playoff data from API endpoint. */ async fetchPlayoffs() { - // TODO: Find playoff endpoints in new API - const response = await fetch(BASE_PLAYOFF_URL); + const year = new Date().getFullYear(); + const lastYear = year - 1; + const url = `${BASE_PLAYOFF_URL}/${lastYear}${year}`; + const response = await fetch(url); if (!response.ok) { - Log.error(`Fetching NHL playoffs failed: ${response.status} ${response.statusText}.`); + Log.error(`Fetching NHL playoffs from ${url} failed: ${response.status} ${response.statusText}.`); return; } const playoffs = await response.json(); - playoffs.rounds.sort((a, b) => a.number <= b.number ? 1 : -1); + playoffs.rounds.sort((a, b) => a.roundNumber <= b.roundNumber ? 1 : -1); return playoffs; }, @@ -398,18 +400,11 @@ module.exports = NodeHelper.create({ * * @param {object} rawTeam - Raw team information. * - * @param {object} game - Raw game information. - * * @returns {Game} Parsed game information. */ - parsePlayoffTeam(rawTeam, game) { + parsePlayoffTeam(rawTeam) { const team = this.parseTeam(rawTeam); - - if (game?.seriesStatus?.topSeedTeamId === team.id) { - team.score = game?.seriesStatus?.topSeedWins; - } else { - team.score = game?.seriesStatus?.bottomSeedWins; - } + team.score = rawTeam.wins; return team; }, @@ -465,16 +460,16 @@ module.exports = NodeHelper.create({ * @returns {Series} Parsed series information. */ parseSeries(series = {}) { - if (!series.matchupTeams || series.matchupTeams.length === 0) { + if (!series.bottomSeed || !series.topSeed) { return null; } return { - number: series.number, - round: series.round.number, + letter: series.seriesLetter, + round: series.roundNumber, teams: { - home: this.parsePlayoffTeam(series.matchupTeams, undefined), // TODO: Don't pass undefined to retrieve the correct score - away: this.parsePlayoffTeam(series.matchupTeams, undefined), // TODO: Don't pass undefined to retrieve the correct score + bottomSeed: this.parsePlayoffTeam(series.bottomSeed), + topSeed: this.parsePlayoffTeam(series.topSeed), } } }, @@ -533,7 +528,7 @@ module.exports = NodeHelper.create({ if (season.mode === 3 || games.length === 0) { const playoffData = await this.fetchPlayoffs(); - const playoffSeries = this.computePlayoffDetails(playoffData).filter(s => s.round >= playoffData.defaultRound); + const playoffSeries = this.computePlayoffDetails(playoffData).filter(s => s.roundNumber != playoffData.currentRound); this.sendSocketNotification('PLAYOFFS', playoffSeries); } diff --git a/templates/MMM-NHL.njk b/templates/MMM-NHL.njk index 4f492bbe..9155de57 100644 --- a/templates/MMM-NHL.njk +++ b/templates/MMM-NHL.njk @@ -80,27 +80,27 @@ {% if config.showNames %} - {{ series.teams.home.short }} + {{ series.teams.topSeed.short }} {% endif %} {% if config.showLogos %} + src="https://assets.nhle.com/logos/nhl/svg/{{ series.teams.topSeed.short }}_dark.svg"/> {% endif %} - {{ series.teams.home.score }} + {{ series.teams.topSeed.score }} : - {{ series.teams.away.score }} + {{ series.teams.bottomSeed.score }} {% if config.showLogos %} + src="https://assets.nhle.com/logos/nhl/svg/{{ series.teams.bottomSeed.short }}_dark.svg"/> {% endif %} {% if config.showNames %} - {{ series.teams.away.short }} + {{ series.teams.bottomSeed.short }} {% endif %}