From 5a28fc50e42d74226fefda03d4efa352e44e912f Mon Sep 17 00:00:00 2001 From: evandcoleman Date: Tue, 12 Oct 2021 16:33:13 -0400 Subject: [PATCH] update --- dist/MLB.js | 65 ++++++++++++++++++++++++++++++++++++++++++++--------- src/MLB.js | 65 ++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 108 insertions(+), 22 deletions(-) diff --git a/dist/MLB.js b/dist/MLB.js index c5ef568..7a55086 100644 --- a/dist/MLB.js +++ b/dist/MLB.js @@ -45,7 +45,7 @@ __webpack_require__.a(module, async (__webpack_handle_async_dependencies__) => { -const scriptVersion = 14; +const scriptVersion = 15; const sourceRepo = "evandcoleman/scriptable"; const scriptName = "MLB"; @@ -551,19 +551,24 @@ function getOutsImages(game) { async function fetchTeam(team) { let game; - let days = 0; - while (!game && days < 7) { - let scoreboard = await fetchScoreboard(days); - const games = scoreboard.filter(game => { - const away = game.teams.away.team.abbreviation; - const home = game.teams.home.team.abbreviation; + // Find a game within 14 days for the provided team + game = await fetchGameWithinDays(14, { team }); - return team === away || team === home; - }); + // If the provided team has no upcoming games, pick the first game + // that's currently in-progress + if (!game) { + game = await fetchGameWithinDays(14, { inProgress: true }); + } - game = games[0]; - days += 1; + // Just get the first game in the list + if (!game) { + game = await fetchGameWithinDays(14); + } + + // Get the last game of the provided team + if (!game) { + game = await fetchGameWithinDays(180, { team, backwards: true }); } const isHome = game.teams.home.team.abbreviation === team; @@ -574,6 +579,44 @@ async function fetchTeam(team) { }; } +async function fetchGameWithinDays(maxDays, options) { + var game = null; + let days = options?.backwards == true ? maxDays - 1 : 0; + + while (!game && days < maxDays && days >= 0) { + let scoreboard = await fetchScoreboard(days); + var games = []; + + if (options?.team) { + games = scoreboard.filter(game => { + const away = game.teams.away.team.abbreviation; + const home = game.teams.home.team.abbreviation; + + return options.team === away || options.team === home; + }); + } else if (options?.inProgress) { + games = scoreboard.filter(game => { + const { isPlaying } = getFormattedStatus(game); + + return isPlaying; + }); + } else if (scoreboard.length > 0) { + games = scoreboard; + } + + game = games[0]; + + + if (options?.backwards == true) { + days -= 1; + } else { + days += 1; + } + } + + return game; +} + async function fetchScoreboard(inDays) { const df = new DateFormatter(); df.dateFormat = "yyyy-MM-dd"; diff --git a/src/MLB.js b/src/MLB.js index 6229fab..65c927f 100644 --- a/src/MLB.js +++ b/src/MLB.js @@ -33,7 +33,7 @@ import Cache from './lib/cache'; import Updater from './lib/updater'; import * as http from './lib/http'; -const scriptVersion = 14; +const scriptVersion = 15; const sourceRepo = "evandcoleman/scriptable"; const scriptName = "MLB"; @@ -539,19 +539,24 @@ function getOutsImages(game) { async function fetchTeam(team) { let game; - let days = 0; - while (!game && days < 7) { - let scoreboard = await fetchScoreboard(days); - const games = scoreboard.filter(game => { - const away = game.teams.away.team.abbreviation; - const home = game.teams.home.team.abbreviation; + // Find a game within 14 days for the provided team + game = await fetchGameWithinDays(14, { team }); - return team === away || team === home; - }); + // If the provided team has no upcoming games, pick the first game + // that's currently in-progress + if (!game) { + game = await fetchGameWithinDays(14, { inProgress: true }); + } - game = games[0]; - days += 1; + // Just get the first game in the list + if (!game) { + game = await fetchGameWithinDays(14); + } + + // Get the last game of the provided team + if (!game) { + game = await fetchGameWithinDays(180, { team, backwards: true }); } const isHome = game.teams.home.team.abbreviation === team; @@ -562,6 +567,44 @@ async function fetchTeam(team) { }; } +async function fetchGameWithinDays(maxDays, options) { + var game = null; + let days = options?.backwards == true ? maxDays - 1 : 0; + + while (!game && days < maxDays && days >= 0) { + let scoreboard = await fetchScoreboard(days); + var games = []; + + if (options?.team) { + games = scoreboard.filter(game => { + const away = game.teams.away.team.abbreviation; + const home = game.teams.home.team.abbreviation; + + return options.team === away || options.team === home; + }); + } else if (options?.inProgress) { + games = scoreboard.filter(game => { + const { isPlaying } = getFormattedStatus(game); + + return isPlaying; + }); + } else if (scoreboard.length > 0) { + games = scoreboard; + } + + game = games[0]; + + + if (options?.backwards == true) { + days -= 1; + } else { + days += 1; + } + } + + return game; +} + async function fetchScoreboard(inDays) { const df = new DateFormatter(); df.dateFormat = "yyyy-MM-dd";