Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Matchup / Scoring Period Dynamically #197

Closed
j-per opened this issue Sep 14, 2021 · 2 comments
Closed

Get Matchup / Scoring Period Dynamically #197

j-per opened this issue Sep 14, 2021 · 2 comments

Comments

@j-per
Copy link

j-per commented Sep 14, 2021

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
};`

@billy-ferris
Copy link

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.");
}
};

@mkreiser
Copy link
Owner

Added to the League object in #231 (v1.3.0).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants