|
const https = require('https'); |
|
// easier terminal colors |
|
const chalk = require('chalk'); |
|
|
|
const baseUrl = 'https://www.blaseball.com'; |
|
const streamData = `${baseUrl}/events/streamData`; |
|
const args = [process.argv[2], process.argv[3]]; |
|
let follow = args.includes('--follow'); |
|
let clear = args.includes('--clear'); |
|
|
|
run(); |
|
|
|
function run() { |
|
const req = https.get(streamData, res => { |
|
// track the current incoming message |
|
let currentMessage = ''; |
|
|
|
// something blew up |
|
res.on('error', err => { |
|
console.error('F'); |
|
console.error(err); |
|
}); |
|
|
|
// attempt to parse incoming JSON chunk |
|
res.on('data', chunk => { |
|
// deal with `data: ` prefix in blaseball data stream |
|
const msg = chunk.toString && chunk.toString('utf-8'); |
|
if (msg.startsWith('data: {')) { |
|
// drop the prefix |
|
clear && console.clear(); |
|
console.log(`Latest update: ${new Date().toJSON()}`); |
|
currentMessage = msg.split('data: ').pop(); |
|
} else if (currentMessage) { |
|
// append the rest of the chunked message |
|
currentMessage += msg; |
|
} |
|
|
|
try { |
|
// try to parse the current message |
|
let data = JSON.parse(currentMessage); |
|
|
|
// read the games in the data list |
|
let gamesSchedule = data.value.games.schedule; |
|
for (let game of gamesSchedule) { |
|
// form up teamnames with fancy hex colors |
|
let awayTeam = chalk.hex(game.awayTeamColor)(game.awayTeamName); |
|
// add a bat emoji to show who's doing the batting |
|
let inning = ''; |
|
if (!game.gameComplete && game.awayBatterName) { |
|
awayTeam += '🦇'; |
|
inning = ' | ˆ' + (game.inning + 1); |
|
} |
|
let homeTeam = chalk.hex(game.homeTeamColor)(game.homeTeamName); |
|
if (!game.gameComplete && game.homeBatterName) { |
|
homeTeam += '🦇'; |
|
inning = ' | ˬ' + (game.inning + 1); |
|
} |
|
// print the message |
|
|
|
let txt = `${game.awayScore} - ${game.homeScore}${inning} | ${awayTeam} @ ${homeTeam}: ${game.lastUpdate}`; |
|
console.log(txt); |
|
} |
|
// clear the parsed message |
|
currentMessage = ''; |
|
} catch (e) { |
|
// console.log('waiting on full response...'); |
|
} |
|
}); |
|
|
|
res.on('end', () => { |
|
if (follow) { |
|
run(); |
|
} |
|
}); |
|
}); |
|
|
|
// send the request |
|
req.end(); |
|
} |