### Install ESPN Fantasy Football API via npm Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md This command installs the `espn-fantasy-football-api` package using npm, saving it as a dependency in your project. ```Shell npm install --save espn-fantasy-football-api ``` -------------------------------- ### Initialize ESPN Fantasy Football API Client Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md Demonstrates how to create a new instance of the `Client` class to interact with the ESPN Fantasy Football API for a specific league. This is the basic setup for public leagues. ```javascript import { Client } from 'espn-fantasy-football-api'; const myClient = new Client({ leagueId: 432132 }); ``` -------------------------------- ### Run Unit Tests with Jest via npm Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md This script starts the Jest test runner to execute all unit tests. It supports `--watch` for continuous testing and allows specifying a file inclusion pattern to run specific tests. ```Shell npm run test ``` -------------------------------- ### Calculate Optimal Fantasy Football Lineup for a Week Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md A detailed example script showcasing how to use the `espn-fantasy-football-api` to determine the best possible lineup for each team in a fantasy football league for a given week. It includes logic for handling different player positions and calculating total points. ```javascript const _ = require('lodash'); const { Client } = require('espn-fantasy-football-api/node'); const myClient = new Client({ leagueId: 12345, espnS2: 'YOUR_ESPN_S2', SWID: 'YOUR_SWID' }); class Psychic { static filterPosition(boxscorePlayer, position) { return ( boxscorePlayer.position === position || _.includes(boxscorePlayer.player.eligiblePositions, position) ); } static handleNonFlexPosition(lineup, position) { const players = _.filter(lineup, (player) => this.filterPosition(player, position)); const sortedPlayers = _.sortBy(players, ['totalPoints']); return _.last(sortedPlayers); } static analyzeLineup(lineup, score) { let bestSum = 0; const bestRoster = []; let numChanges = 0; const bestQB = this.handleNonFlexPosition(lineup, 'QB') bestRoster.push(bestQB.player.fullName); bestSum += bestQB.totalPoints; if (bestQB.position === 'Bench') { numChanges += 1; } const bestDefense = this.handleNonFlexPosition(lineup, 'D/ST') bestRoster.push(bestDefense.player.fullName); bestSum += bestDefense.totalPoints; if (bestDefense.position === 'Bench') { numChanges += 1; } const bestKicker = this.handleNonFlexPosition(lineup, 'K') bestRoster.push(bestKicker.player.fullName); bestSum += bestKicker.totalPoints; if (bestKicker.position === 'Bench') { numChanges += 1; } const flexPlayers = _.filter(lineup, (player) => this.filterPosition(player, 'RB') || this.filterPosition(player, 'WR') || this.filterPosition(player, 'TE') ); const sortedFlexPlayers = _.sortBy(flexPlayers, ['totalPoints']); const flexPos = { RB: 2, WR: 2, TE: 1, FLEX: 1 }; while (_.sum(_.values(flexPos)) && !_.isEmpty(sortedFlexPlayers)) { const player = sortedFlexPlayers.pop(); const acceptPlayer = () => { bestRoster.push(player.player.fullName); bestSum += player.totalPoints; if (player.position === 'Bench') { numChanges += 1; } } if (flexPos.RB && _.includes(player.player.eligiblePositions, 'RB')) { acceptPlayer(); flexPos.RB -= 1; } else if (flexPos.WR && _.includes(player.player.eligiblePositions, 'WR')) { acceptPlayer(); flexPos.WR -= 1; } else if (flexPos.TE && _.includes(player.player.eligiblePositions, 'TE')) { acceptPlayer(); flexPos.TE -= 1; } else if (flexPos.FLEX) { acceptPlayer(); flexPos.FLEX -= 1; } } return { bestSum, bestRoster, currentScore: score, numChanges }; } static runForWeek({ seasonId, matchupPeriodId, scoringPeriodId }) { const bestLineups = {}; return myClient.getBoxscoreForWeek({ seasonId, matchupPeriodId, scoringPeriodId }).then((boxes) => { _.forEach(boxes, (box) => { bestLineups[box.awayTeamId] = this.analyzeLineup(box.awayRoster, box.awayScore); bestLineups[box.homeTeamId] = this.analyzeLineup(box.homeRoster, box.homeScore); }); return bestLineups; }); } } Psychic.runForWeek({ seasonId: 2019, matchupPeriodId: 4, scoringPeriodId: 4 }).then((result) => { console.log(result); return result; }); ``` -------------------------------- ### Build and Serve Documentation with npm Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md This script first builds the documentation and then serves it locally, typically on port 8080. It's useful for previewing documentation changes during development. ```Shell npm run serve:docs ``` -------------------------------- ### Build Project Documentation with npm Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md This script generates the project's documentation files. It ensures that the latest documentation is available for reference. ```Shell npm run build:docs ``` -------------------------------- ### Build Project Module with npm Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md This script compiles the project's source code into a distributable module. It prepares the application for deployment or packaging. ```Shell npm run build ``` -------------------------------- ### Run Integration Tests with npm Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md This script specifically executes the integration tests for the project. Integration tests verify the interaction between different components of the application. ```Shell npm run test:integration ``` -------------------------------- ### Run All Lint Tasks with npm Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md This script executes all defined linting tasks across the project. It helps maintain code style consistency and identify potential errors or anti-patterns. ```Shell npm run lint ``` -------------------------------- ### Run All Clean Scripts with npm Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md This script executes all defined cleaning tasks within the project. It's a convenient way to remove generated files and reset the project to a clean state. ```Shell npm run clean ``` -------------------------------- ### Run Continuous Integration Tasks with npm Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md This script orchestrates a series of continuous integration tasks, including linting, unit tests, integration tests, and the main build process. It's designed to ensure code quality and functionality before deployment. ```Shell npm run ci ``` -------------------------------- ### Remove Documentation Folder with npm Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md This script removes the generated documentation folder. It's useful for clearing old documentation builds before generating new ones. ```Shell npm run clean:docs ``` -------------------------------- ### Remove Distribution Folder with npm Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md This script specifically deletes the 'dist' folder, which typically contains compiled or bundled output. It's used to clear previous build artifacts. ```Shell npm run clean:dist ``` -------------------------------- ### Import ESPN Fantasy Football API in JavaScript Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md This snippet demonstrates how to import the ESPN Fantasy Football API client in both ES6 and ES5 syntax for web and Node.js environments, including development builds. ```JavaScript // ES6 import { ... } from 'espn-fantasy-football-api'; // web import { ... } from 'espn-fantasy-football-api/node'; // node import { ... } from 'espn-fantasy-football-api/web-dev'; // web development build import { ... } from 'espn-fantasy-football-api/node-dev'; // node development build // ES5 const { ... } = require('espn-fantasy-football-api'); // web const { ... } = require('espn-fantasy-football-api/node'); // node const { ... } = require('espn-fantasy-football-api/web-dev'); // web development build const { ... } = require('espn-fantasy-football-api/node-dev'); // node development build ``` -------------------------------- ### Ensure Spelling Correctness with npm Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md This script performs a spelling check on project files. It helps maintain professional documentation and code comments. ```Shell npm run lint:spelling ``` -------------------------------- ### Ensure JavaScript Code Style with npm Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md This script specifically checks JavaScript code for style correctness. It helps enforce coding standards and improve code readability. ```Shell npm run lint:js ``` -------------------------------- ### ESPN Fantasy Football API Conventions Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md This section defines key parameters and their meanings used across the ESPN Fantasy Football API, including `leagueId`, `seasonId`, `matchupPeriod`, and `scoringPeriod`. ```APIDOC API Conventions: leagueId: type: integer description: The ID for your league. example: 387659 seasonId: type: integer description: The year in which the season was played. example: 2018 matchupPeriod: type: integer description: Refers to an entire match-up, including if the match-up lasts multiple weeks (not rare in playoff settings for smaller leagues). example: 3 (refers to the third matchup in your league) scoringPeriod: type: integer description: Refers to a single NFL week. Since most matchups are 1 week long, the scoringPeriod will typically match the matchupPeriod. However, for multi-week matchups, scoringPeriod allows one to get information about a specific week in the match-up (useful in multi-week playoff match-up). example: 3 (refers to the third week of the NFL season) notes: - A scoringPeriodId of 0 refers to the preseason before any games are played. - A scoringPeriodId of 18 refers to the end of the season. precedence: If both a matchupPeriod and a scoringPeriod are used, the scoringPeriod takes precedence. ``` -------------------------------- ### Authenticate ESPN Fantasy Football Client for Private Leagues Source: https://github.com/mkreiser/espn-fantasy-football-api/blob/main/README.md Explains how to configure the `Client` for private leagues by providing `espn_s2` and `SWID` cookies. These cookies are obtained from browser developer tools and are essential for accessing private league data in a NodeJS environment. ```javascript const client = new Client({ leagueId: 12345, espnS2: 'YOUR_ESPN_S2', SWID: 'YOUR_SWID' }); /* OR */ const myClient = new Client({ leagueId: 12345 }); myClient.setCookies({ espnS2: 'YOUR_ESPN_S2', SWID: 'YOUR_SWID' }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.