Skip to content

Instantly share code, notes, and snippets.

@joewright
Last active October 21, 2020 16:06
Show Gist options
  • Save joewright/a5a5369874444183153301b9972a7a8a to your computer and use it in GitHub Desktop.
Save joewright/a5a5369874444183153301b9972a7a8a to your computer and use it in GitHub Desktop.
Blaseball terminal stream (NodeJS)

Prerequisites

  • NodeJS 12+
  • Chalk for nice terminal colors

Running it

node blaseball-terminal.js
# run and keep the feed alive indefinitely
node blaseball-termina.js --follow
# >
# 5 - 1 | San Francisco Lovers🦇 @ Charleston Shoe Thieves: Strike, swinging. 0-1
# 8 - 2 | Miami Dale🦇 @ Chicago Firefighters: Beck Whitney batting for the Dale.
# 3 - 4 | Hawai'i Fridays @ Canada Moist Talkers: Game over.
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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment