Closed j-per closed 1 year ago
I created an endpoint to fetch the league data from the base URL and pull from the scoringPeriodId there. Then inside my functions if I dont pass a week into them - I usually run some logic to pull this data.
const getTeams = async (week, year = seasonId) => { let scoringPeriod; if (!week) { scoringPeriod = await getCurrentWeek(); } else { scoringPeriod = week; } ... };
const fetchLeagueEndpoint = async () => {
const apiUrl = ${ESPN_FFL_ENDPOINT}/seasons/${seasonId}/segments/0/leagues/${LEAGUE_ID}
;
return axios
.get(apiUrl, {
headers: {
Cookie: SWID=${SWID}; espn_s2=${ESPN_S2}
,
},
})
.then((response) => {
console.info("Successfully fetched league endpoint");
return response.data;
})
.catch((error) => {
console.error("Error fetching league endpoint:\n", error);
throw Error("Error fetching league endpoint.");
});
};
const getCurrentWeek = async () => { try { const { scoringPeriodId } = await fetchLeagueEndpoint(); console.info("Successfully got current week: ", scoringPeriodId); return scoringPeriodId; } catch (error) { console.error("Error fetching current week:\n", error); throw Error("Error fetching current week."); } };
Added to the League object in #231 (v1.3.0
).
I wrote a little Discord Bot App that fetches box score data from our league. I'm currently returning scores based on a hard coded matchup/scoring period. Does anybody know how to get the matchup / scoring period dynamically based on the current week? Here is my code:
`const { Client } = require('espn-fantasy-football-api/node'); const { getName } = require('./player-mapping'); require('dotenv').config();
const leagueId = process.env.espn_leagueid; const espnS2 = process.env.espn_espnS2; const SWID = process.env.espn_swid; const seasonId = process.env.espn_seasonid;
const myClient = new Client({ leagueId });
myClient.setCookies({ espnS2, SWID });
async function getEspnData() { const getScores = await myClient.getBoxscoreForWeek({ seasonId, matchupPeriodId: 1, scoringPeriodId: 1 }); const matchupData = createEmbedFields(getScores); const embedded = embedData(matchupData); return embedded; }
function createEmbedFields(boxScore) { const matchupArrayData = boxScore.map(scores => { const awayTeamId = getName(scores.awayTeamId.toString()); const homeTeamId = getName(scores.homeTeamId.toString()); const awayPoints = scores.awayScore.toString(); const homePoints = scores.homeScore.toString(); return [ { name: awayTeamId, value: awayPoints, inline: true }, { name: homeTeamId, value: homePoints, inline: true }, { name: '\u200B', value: '\u200B' } ] }) .flat(); return matchupArrayData; }
function embedData(teamData) { const fflEmbed = { color: 0x0099ff, title: 'Current Scores', fields: teamData, timestamp: new Date(), }; return { embeds: [fflEmbed] } }
module.exports = { getEspnData, embedData };`