hockeybar
Version:
A bot used to automate the updating of hockey league subreddit markdown
80 lines (65 loc) • 2.67 kB
JavaScript
const FOX_API_KEY='jE7yBJVRNAwdDesMgTzTXUUSx1It41Fq';
const request = require("request");
const jsdom = require("jsdom");
const {
JSDOM
} = jsdom;
const cheerio = require('cheerio');
const teams = require('../teams');
const ScheduleBuilder = require('./schedule');
var GameStatsBuilder = class GameStatsBuilder {
constructor(team) {
this.team = team;
var currentDate = new Date();
this.comparisonMonth = currentDate.getMonth() + 1;
this.comparisonDay = currentDate.getDate();
this.comparisonYear = currentDate.getFullYear();
this.isPreSeason = true;
if (this.comparisonMonth === 10 && this.comparisonDay === 4 && this.comparisonYear === 2017) {
this.isPreSeason = false;
}
this.scheduleBuilder = new ScheduleBuilder(this.team, this.isPreSeason);
}
getCurrentGame() {
return new Promise((resolve, reject) => {
this.scheduleBuilder.parse().then((data) => {
for (var i = 0; i < data.length; i++) {
var game = data[i];
var dateComps = game.date.split('/');
var month = parseInt(dateComps[0]);
var day = parseInt(dateComps[1]);
if (month === this.comparisonMonth && day === this.comparisonDay) {
this.getCurrentGameStats(game).then(gameStats => {
resolve(gameStats);
});
return;
}
}
resolve(null);
})
});
}
getCurrentGameStats(game) {
var gameId = game.boxScoreUrl.split('=')[1];
return new Promise((resolve, reject) => {
request({
uri: `https://api.foxsports.com/sportsdata/v1/live/nhl/games/${gameId}.json?version=183&apikey=${FOX_API_KEY}`
}, function (error, response, body) {
if (error && response.statusCode !== 200) {
reject('Error when contacting server');
}
//var virtualConsole = new jsdom.VirtualConsole();
//var dom = new JSDOM(body, virtualConsole);
//var gameData = self.parseTeamSchedule(dom.serialize());
resolve(body);
});
});
}
parseGameData(gameData) {
var $ = cheerio.load(gameData);
var currentScore = $('.wisbb_bsResultDetails');
var scoreSummaryTable = $('.wisbb_bsHeaderScoringSummary');
var eventsTable = $('.wisbb_bsrrEventSummaryTable');
}
}
module.exports = GameStatsBuilder;