2020-11-05 04:57:22 +00:00
|
|
|
const { Service } = require('flitter-di')
|
|
|
|
const axios = require('axios').default;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A service class for interacting with data from the SportsDataIO API.
|
|
|
|
*/
|
|
|
|
class SportsDataService extends Service {
|
|
|
|
static get services() {
|
2020-11-07 18:10:19 +00:00
|
|
|
return [...super.services, 'configs', 'models', 'utility']
|
|
|
|
}
|
|
|
|
|
|
|
|
async is_draft_stage() {
|
|
|
|
const Setting = this.models.get('models::setting')
|
2020-11-07 19:02:37 +00:00
|
|
|
return this.utility.infer(await Setting.get('in_draft_stage'))
|
2020-11-07 18:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async current_play_week() {
|
|
|
|
const Setting = this.models.get('models::setting')
|
2020-11-07 19:02:37 +00:00
|
|
|
return this.utility.infer(await Setting.get('current_week'))
|
2020-11-05 04:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async get_team_players(team_key) {
|
|
|
|
return this.get_request(`Players/${team_key}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
async get_active_teams() {
|
|
|
|
return this.get_request('Teams')
|
|
|
|
}
|
|
|
|
|
2020-11-07 18:10:19 +00:00
|
|
|
async get_request(path, base = 'scores') {
|
|
|
|
const response = await axios.get(this.url(path, base))
|
2020-11-05 04:57:22 +00:00
|
|
|
return response.data
|
|
|
|
}
|
|
|
|
|
2020-11-07 18:10:19 +00:00
|
|
|
async get_week_player_stats(week_num) {
|
|
|
|
return this.get_request(`PlayerGameProjectionStatsByWeek/${this.configs.get('server.sports_data.season')}/${week_num}`, 'projections')
|
|
|
|
}
|
|
|
|
|
|
|
|
url(path, base = 'scores') {
|
2020-11-05 04:57:22 +00:00
|
|
|
if ( path.startsWith('/') ) path = path.slice(1)
|
2020-11-07 18:10:19 +00:00
|
|
|
return `https://api.sportsdata.io/v3/nfl/${base}/json/${path}?key=${this.configs.get('server.sports_data.api_key')}`
|
2020-11-05 04:57:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = SportsDataService
|