Last active
June 13, 2022 15:17
-
-
Save zacwest/251519ff6e3a4c2da96ee7469096e790 to your computer and use it in GitHub Desktop.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-guardians': ':guardians:', | |
'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: '<https://www.mlb.com/standings|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