Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Post Baseball standings to Slack. This code is awful, I don't know JavaScript, but it works. You probably need to figure out the emoticons for each team in your Slack.
var token = 'your-token-here'; // from https://erikberg.com/api for wildcard
var agent = 'Your/1.0 <user@agent>';
var fetch = require('node-fetch');
var hooks = {'https://hooks.slack.com/services/…': '#channel',
'https://hooks.slack.com/services/…': '#channel'};
var team_table = {
"los-angeles-dodgers": ":dodgerslogo:",
"san-francisco-giants": ":giants:",
"colorado-rockies": ":rockies:",
"san-diego-padres": ":padres:",
"arizona-diamondbacks": ":diamondbacks:",
"pittsburgh-pirates": ":pirates:",
"chicago-cubs": ":cubs:",
"cincinnati-reds": ":reds:",
"milwaukee-brewers": ":brewers:",
"st-louis-cardinals": ":cardinalslogo:",
"washington-nationals": ":nationals:",
"new-york-mets": ":mets:",
'miami-marlins': ':marlins:',
'atlanta-braves': ':braves:',
'philadelphia-phillies': ':phillies:',
'seattle-mariners': ':mariners:',
'houston-astros': ':astros:',
'los-angeles-angels': ':angels:',
'texas-rangers': ':rangers:',
'oakland-athletics': ':oaklandas:',
'detroit-tigers': ':tigers:',
'cleveland-indians': ':indians:',
'chicago-white-sox': ':whitesox:',
'kansas-city-royals': ':royals:',
'minnesota-twins': ':twins:',
'baltimore-orioles': ':orioles:',
'boston-red-sox': ':redsox:',
'new-york-yankees': ':yankees:',
'tampa-bay-rays': ':rays:',
'toronto-blue-jays': ':bluejays:',
};
fetch('https://erikberg.com/mlb/standings.json', {
headers: {
'User-Agent': agent
}
}).then(function(res) {
return res.json();
}).then(function(json) {
var standings = json["standing"];
var divisions = {
"AL-E": "", "AL-C": "", "AL-W": "",
"NL-E": "", "NL-C": "", "NL-W": "",
};
for (var teamIdx in standings) {
var team = standings[teamIdx];
var division = team["conference"] + "-" + team["division"];
divisions[division] += team_table[team["team_id"]] + " (" + team["games_back"].toFixed(1) + ") "
}
return divisions;
}).then(function(divisions) {
for (hook in hooks) {
var slack = require('slack-notify')(hook);
var channel = hooks[hook];
slack.alert({
channel: channel,
text: 'Standings update!',
icon_emoji: ':baseball:',
username: 'baseball-standings',
fields: divisions
})
}
})
fetch('https://erikberg.com/mlb/wildcard.json', {
headers: {
'Authorization': 'BEARER ' + token,
'User-Agent': agent
}
}).then(function(res) {
return res.json();
}).then(function json(json) {
var standings = json["standing"];
var divisions = {
"AL": "", "NL": ""
};
for (var teamIdx in standings) {
var team = standings[teamIdx];
var division = team["conference"]
divisions[division] += team_table[team["team_id"]] + " (" + team["games_back"].toFixed(1) + ") "
}
return divisions;
}).then(function(divisions) {
// Disable wildcard until later.
return;
for (hook in hooks) {
var slack = require('slack-notify')(hook);
var channel = hooks[hook];
slack.alert({
channel: channel,
text: 'Wildcard update!',
icon_emoji: ':baseball:',
username: 'baseball-standings',
fields: divisions
})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.