Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
OWL API parsing for SFS 2019 season schedule
const moment = require('moment')
const momentTz = require('moment-timezone')
const getWeek = (timestamp, stage) => {
// Use momentjs to get number of weeks since start of stage
return ' ??? '
}
const getOpponent = competitors => {
const opponents = competitors.filter(competitor => {
return competitor.abbreviatedName !== 'SFS'
})
return opponents[0].abbreviatedName
}
const getSide = competitors => {
if (competitors[0].abbreviatedName === 'SFS') {
return 'Away'
}
return 'Home'
}
const getVenue = timestamp => {
// if timestamp is within stage 4 week 5, then The Novo, else Blizzard Arena
return 'BA'
}
const formatMatches = matches => {
return matches.map(match => {
const {
competitors,
stage,
startDateTS: timestamp,
} = match
const matchMoment = moment(timestamp).tz('America/Los_Angeles')
return {
stage: stage,
week: getWeek(timestamp, stage),
date: matchMoment.format('MMM Do'),
day: matchMoment.format('ddd'),
time: matchMoment.format('h:mm a'),
timestamp,
opponent: getOpponent(competitors),
side: getSide(competitors),
venue: getVenue(timestamp),
}
})
}
module.exports = {
formatMatches,
}
const fetch = require('node-fetch')
const { formatMatches } = require('./formatHelpers')
const parseApiData = fetchResponse => {
return fetchResponse.json()
.then(json => json.data)
}
const isShockMatch = match => {
return match.competitors.some(competitor => {
return competitor.abbreviatedName === 'SFS'
})
}
// `addStageToMatch` implements poor man's currying
const addStageToMatch = stageId => {
return (match) => {
const stage = stageId + 1
return { ...match, stage }
}
}
const getShockMatches = json => {
return json.stages.reduce((matches, stage) => {
const shockMatches = stage.matches
.filter(isShockMatch)
.map(addStageToMatch(stage.id))
return matches.concat(shockMatches)
}, [])
}
const convertMatchesToRows = formattedMatches => {
return formattedMatches.map(match => {
const {
stage,
week,
date,
day,
time,
opponent,
side,
venue,
} = match
outputLine = `|${stage}|${week}|${date}|${day}|${time}`
outputLine += `|${opponent}|${side}|${venue}|`
return outputLine
})
}
const printOutput = formattedMatches => {
const headers = '|Stage|Week|Date|Day|Time|Opponent|Side*|Venue|'
const divider = '|:-:|:-:|:-|:-:|:-|:-:|:-:|:-:|'
const outputLines = [
headers,
divider,
...convertMatchesToRows(formattedMatches),
]
outputLines.forEach(line => {
console.log(line)
})
}
fetch('https://api.overwatchleague.com/schedule')
.then(parseApiData)
.then(getShockMatches)
.then(formatMatches)
.then(printOutput)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment