Flesh out Player model and write patch to populate players from sporting API

This commit is contained in:
2020-11-04 22:57:22 -06:00
parent 3befe69f30
commit 19567dcfb0
7 changed files with 160 additions and 5 deletions

62
app/SeedAPIData.patch.js Normal file
View File

@@ -0,0 +1,62 @@
const { Injectable } = require('flitter-di')
/**
* A one-time patch which populates the Player collection with
* all the players on all the active teams in the NFL using the
* sports data service's APIs.
*
* To run this, do it from "node flitter shell":
*
* @example
* Patch = require('./app/SeedAPIData.patch')
* patch = _di.make(Patch)
* await patch.run()
*
* @extends Injectable
*/
class SeedAPIDataPatch extends Injectable {
static get services() {
return [...super.services, 'models', 'sports_data', 'output']
}
/**
* Run the patch.
* @return {Promise<void>}
*/
async run() {
// Clear any existing data first
const Player = this.models.get('Player')
await Player.deleteMany()
this.output.success('Cleared existing player data!')
// Fetch all active teams
this.output.info('Fetching teams to patch data...')
const teams = await this.sports_data.get_active_teams()
this.output.info(`Fetched ${teams.length} teams.`)
let players = 0
// Fetch the players for each of the teams
for ( let i = 0; i < teams.length; i++ ) {
const team = teams[i]
this.output.info(`Fetching players for team ${i + 1} of ${teams.length} (${team.Key})...`)
const team_players = await this.sports_data.get_team_players(team.Key)
this.output.info(` (patching ${team_players.length} players)`)
// Insert Player documents for each of the player records
for ( const rec of team_players ) {
const player = Player.from_patch_data(rec)
player.patch_data.patch_team_name = team.FullName
player.patch_data.patch_team_key = team.Key
await player.save()
players += 1
}
this.output.success(` (patched player data)`)
}
this.output.success(`Patch complete. Created ${players} players.`)
}
}
module.exports = exports = SeedAPIDataPatch

View File

@@ -1,5 +1,6 @@
const { Model } = require('flitter-orm')
/*
* Player Model
* -------------------------------------------------------------
@@ -14,14 +15,56 @@ class Player extends Model {
*/
static get schema() {
return {
player_number: String,
player_name: String,
player_position: String,
team_name: String,
image_url: String,
patch_data: {
patch_team_id: Number,
patch_team_name: String,
patch_team_key: String,
player_id: Number,
draft_position: Number,
},
player_number: Number,
first_name: String,
last_name: String,
full_name: String,
position: String,
fantasy_position: String,
height: String,
weight: Number,
birthday: String,
experience: String,
experience_string: String,
age: Number,
photo_url: String,
}
}
static from_patch_data(data) {
const model_data = {
patch_data: {
patch_team_id: data.TeamID,
// patch_team_name,
// patch_team_key,
player_id: data.PlayerID,
draft_position: data.AverageDraftPosition,
},
player_number: data.Number,
first_name: data.FirstName,
last_name: data.LastName,
full_name: data.Name,
position: data.Position,
fantasy_position: data.FantasyPosition,
height: data.Height,
weight: data.Weight,
birthday: data.BirthDateString,
experience: data.Experience,
experience_string: data.ExperienceString,
age: data.Age,
photo_url: data.PhotoUrl
}
return new this(model_data)
}
async to_api() {
return {
player_number: this.player_number,

View File

@@ -0,0 +1,31 @@
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() {
return [...super.services, 'configs']
}
async get_team_players(team_key) {
return this.get_request(`Players/${team_key}`)
}
async get_active_teams() {
return this.get_request('Teams')
}
async get_request(path) {
const response = await axios.get(this.url(path))
return response.data
}
url(path) {
if ( path.startsWith('/') ) path = path.slice(1)
return `https://api.sportsdata.io/v3/nfl/scores/json/${path}?key=${this.configs.get('server.sports_data.api_key')}`
}
}
module.exports = exports = SportsDataService