Skip to content

Commit

Permalink
Merge pull request #241 from mkreiser/readme
Browse files Browse the repository at this point in the history
Update README
  • Loading branch information
mkreiser authored Nov 4, 2023
2 parents 346f071 + 9a86033 commit 5a9f97c
Showing 1 changed file with 135 additions and 11 deletions.
146 changes: 135 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ A Javascript API client for both web and NodeJS that connects to the updated v3

## Features

* Supports pulling data from ESPN.
* Private league support (NodeJS version only, see [Important Notes](#important-notes)).
* Highly documented.
* Built for speed and efficiency with caching support.
* Built for extensibility by using ES6 classes.
* Supports pulling data from ESPN
* Private league support (NodeJS version only, see [Important Notes](#important-notes))
* Highly documented
* Built for speed and efficiency with caching support
* Built for extensibility by using ES6 classes

## Documentation Reference

Expand Down Expand Up @@ -90,14 +90,138 @@ const myClient = new Client({ leagueId: 432132 });

#### Working with Private Leagues

You'll need two cookies from ESPN: `espn_s2` and `SWID`. These are found at "Application > Cookies > espn.com" in the Chrome DevTools when on espn.com.
You need two cookies from ESPN: `espn_s2` and `SWID`. These are found at "Application > Cookies > espn.com" in the Chrome DevTools when on espn.com.

**Note**: As specified before, this functionality only works in NodeJS.

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

## Example Project Usage

The following script calculate the best possible lineup each team could have started for a week:

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

## Built With

[axios](https://github.com/axios/axios) - Promise based HTTP client.
Expand All @@ -114,7 +238,7 @@ myClient.setCookies({ espnS2: 'YOUR_ESPN_S2', SWID: 'YOUR_SWID' });

## Versioning

This project uses [Semantic Versioning](https://semver.org/). Until the 1.0.0 version is published, all major changes will be published with a minor version bump.
This project uses [Semantic Versioning](https://semver.org/).

## License

Expand All @@ -132,9 +256,9 @@ This is my first time writing OSS and picking a license. Feel free to reach out
| clean:dist | Removes the dist folder. |
| clean:docs | Removes the docs folder. |
| ci | Runs continuous integration tasks. Currently runs lint, unit and integration tests, and build. |
| lint | Ensures code style is correct. |
| lint | Runs all lint tasks |
| lint:js | Ensures code style is correct. |
| lint:spelling | Ensures spelling is correct. |
| serve:docs | Builds and serves docs. Defaults to port 8080. |
| test | Starts a jest test runner with access to all tests. Pass `--watch` to keep jest alive and watching for changes. Pass a string as a file inclusion pattern. |
| test:all | Runs the unit tests then the integration tests. |
| test | Starts a jest test runner with access to all unit tests. Pass `--watch` to keep jest alive and watching for changes. Pass a string as a file inclusion pattern. |
| test:integration | Runs the integration tests. |
| test:unit | Runs the unit tests. |

0 comments on commit 5a9f97c

Please sign in to comment.