Skip to content

Commit

Permalink
Merge pull request #17 from unmonk/mlb
Browse files Browse the repository at this point in the history
added MLB
  • Loading branch information
saiemgilani authored Jun 20, 2023
2 parents 1a0bec4 + 63d47fa commit a1ca3fe
Show file tree
Hide file tree
Showing 8 changed files with 1,947 additions and 625 deletions.
1 change: 1 addition & 0 deletions app/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

module.exports.cfb = require('./services/cfb.service');
module.exports.mbb = require('./services/mbb.service');
module.exports.mlb = require('./services/mlb.service');
module.exports.nba = require('./services/nba.service');
module.exports.ncaa = require('./services/ncaa.service');
module.exports.nfl = require('./services/nfl.service');
Expand Down
287 changes: 287 additions & 0 deletions app/services/mlb.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
const axios = require("axios");
/**
* Operations for MLB.
*
* @namespace mlb
*/
module.exports = {
/**
* Gets the MLB game play-by-play data for a specified game.
* @memberOf mlb
* @async
* @function
* @param {number} id - Game id.
* @returns json
* @example
* const result = await sdv.mlb.getPlayByPlay(401472105);
*/
getPlayByPlay: async function (id) {
const baseUrl = "http://cdn.espn.com/core/mlb/playbyplay";
const params = {
gameId: id,
xhr: 1,
render: "false",
userab: 18,
};
const res = await axios.get(baseUrl, {
params,
});
return {
teams: res.data.gamepackageJSON.header.competitions[0].competitors,
id: res.data.gamepackageJSON.header.id,
plays: res.data.gamepackageJSON.plays,
competitions: res.data.gamepackageJSON.header.competitions,
season: res.data.gamepackageJSON.header.season,
boxScore: res.data.gamepackageJSON.boxscore,
seasonSeries: res.data.gamepackageJSON.seasonseries,
standings: res.data.gamepackageJSON.standings,
};
},
/**
* Gets the MLB game box score data for a specified game.
* @memberOf mlb
* @async
* @function
* @param {number} id - Game id.
* @returns json
* @example
* const result = await sdv.mlb.getBoxScore(401472105);
*/
getBoxScore: async function (id) {
const baseUrl = "http://cdn.espn.com/core/mlb/boxscore";
const params = {
gameId: id,
xhr: 1,
render: false,
device: "desktop",
userab: 18,
};
const res = await axios.get(baseUrl, {
params,
});
const game = res.data.gamepackageJSON.boxscore;
game.id = res.data.gameId;
return game;
},
/**
* Gets the MLB game summary data for a specified game.
* @memberOf mlb
* @async
* @function
* @param {number} id - Game id.
* @returns json
* @example
* const result = await sdv.mlb.getSummary(401472105);
*/
getSummary: async function (id) {
const baseUrl =
"http://site.api.espn.com/apis/site/v2/sports/baseball/mlb/summary";
const params = {
event: id,
};
const res = await axios.get(baseUrl, {
params,
});
return {
boxScore: res.data.boxscore,
gameInfo: res.data.gameInfo,
header: res.data.header,
teams: res.data.gamepackageJSON.header.competitions[0].competitors,
id: res.data.gamepackageJSON.header.id,
plays: res.data.gamepackageJSON.plays,
winProbability: res.data.winprobability,
leaders: res.data.leaders,
competitions: res.data.gamepackageJSON.header.competitions,
season: res.data.gamepackageJSON.header.season,
seasonSeries: res.data.gamepackageJSON.seasonseries,
standings: res.data.gamepackageJSON.standings,
};
},
/**
* Gets the MLB game PickCenter data for a specified game.
* @memberOf mlb
* @async
* @function
* @param {number} id - Game id.
* @returns json
* @example
* const result = await sdv.mlb.getPicks(401472105);
*/
getPicks: async function (id) {
const baseUrl =
"http://site.api.espn.com/apis/site/v2/sports/baseball/mlb/summary";
const params = {
event: id,
};
const res = await axios.get(baseUrl, {
params,
});
return {
id: parseInt(res.data.header.id),
gameInfo: res.data.gameInfo,
leaders: res.data.leaders,
header: res.data.header,
teams: res.data.header.competitions[0].competitors,
competitions: res.data.header.competitions,
winProbability: res.data.winprobability,
pickcenter: res.data.winprobability,
againstTheSpread: res.data.againstTheSpread,
odds: res.data.odds,
seasonSeries: res.data.seasonseries,
season: res.data.header.season,
standings: res.data.standings,
};
},
/**
* Gets the MLB schedule data for a specified date if available.
* @memberOf mlb
* @async
* @function
* @param {*} year - Year (YYYY)
* @param {*} month - Month (MM)
* @param {*} day - Day (DD)
* @returns json
* @example
* const result = await sdv.mlb.getSchedule(
* year = 2016, month = 04, day = 15
* )
*/
getSchedule: async function ({ year = null, month = null, day = null }) {
const baseUrl = `http://cdn.espn.com/core/mlb/schedule?dates=${year}${
parseInt(month) <= 9 ? "0" + parseInt(month) : parseInt(month)
}${parseInt(day) <= 9 ? "0" + parseInt(day) : parseInt(day)}`;
const params = {
xhr: 1,
render: false,
device: "desktop",
userab: 18,
};
const res = await axios.get(baseUrl, {
params,
});
return res.data.content.schedule;
},
/**
* Gets the MLB scoreboard data for a specified date if available.
* @memberOf mlb
* @async
* @function
* @param {*} year - Year (YYYY)
* @param {*} month - Month (MM)
* @param {*} day - Day (DD)
* @param {number} limit - Limit on the number of results @default 300
* @returns json
* @example
* const result = await sdv.mlb.getScoreboard(
* year = 2019, month = 11, day = 16
* )
*/
getScoreboard: async function ({
year = null,
month = null,
day = null,
limit = 300,
}) {
const baseUrl = `http://site.api.espn.com/apis/site/v2/sports/baseball/mlb/scoreboard?dates=${year}${
parseInt(month) <= 9 ? "0" + parseInt(month) : parseInt(month)
}${parseInt(day) <= 9 ? "0" + parseInt(day) : parseInt(day)}`;
const params = {
limit,
};
const res = await axios.get(baseUrl, {
params,
});
return res.data;
},
/**
* Gets the team standings for the MLB.
* @memberOf mlb
* @async
* @function
* @param {number} year - Season
* @param {string} group - acceptable group names: 'league','conference','division'
* @returns json
* @example
* const yr = 2016;
* const result = await sdv.mlb.getStandings(year = yr);
*/
getStandings: async function ({
year = new Date().getFullYear(),
group = "league",
}) {
const groupId = group === "league" ? 1 : group === "conference" ? 2 : 3;
const baseUrl = `https://site.web.api.espn.com/apis/v2/sports/baseball/mlb/standings`;
const params = {
region: "us",
lang: "en",
contentorigin: "espn",
season: year,
type: 1,
level: groupId,
};
const res = await axios.get(baseUrl, {
params,
});
return res.data;
},
/**
* Gets the list of all MLB teams their identification info for ESPN.
* @memberOf mlb
* @async
* @function
* @returns json
* @example
* const result = await sdv.mlb.getTeamList();
*/
getTeamList: async function () {
const baseUrl =
"http://site.api.espn.com/apis/site/v2/sports/baseball/mlb/teams";
const params = {
limit: 1000,
};

const res = await axios.get(baseUrl, {
params,
});

return res.data;
},
/**
* Gets the team info for a specific MLB team.
* @memberOf mlb
* @async
* @function
* @param {number} id - Team Id
* @returns json
* @example
* const teamId = 16;
* const result = await sdv.mlb.getTeamInfo(teamId);
*/
getTeamInfo: async function (id) {
const baseUrl = `http://site.api.espn.com/apis/site/v2/sports/baseball/mlb/teams/${id}`;

const res = await axios.get(baseUrl);
return res.data;
},
/**
* Gets the team roster information for a specific MLB team.
* @memberOf mlb
* @async
* @function
* @param {number} id - Team Id
* @returns json
* @example
* const teamId = 16;
* const result = await sdv.mlb.getTeamPlayers(teamId);
*/
getTeamPlayers: async function (id) {
const baseUrl = `http://site.api.espn.com/apis/site/v2/sports/baseball/mlb/teams/${id}`;
const params = {
enable: "roster",
};
const res = await axios.get(baseUrl, {
params,
});
return res.data;
},
};
Loading

1 comment on commit a1ca3fe

@vercel
Copy link

@vercel vercel bot commented on a1ca3fe Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.