Skip to content

Commit

Permalink
update scoreboard to fetch today.json
Browse files Browse the repository at this point in the history
  • Loading branch information
henrygd committed Feb 2, 2024
1 parent 69452ad commit e712704
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ const cache = new ExpiryMap(30 * 60 * 1000)
// set scores cache expiry to 3 min
const scoreboardCache = new ExpiryMap(3 * 60 * 1000)

// formats date for ncaa.com url
const urlDateFormatter = new Intl.DateTimeFormat('en-CA', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
timeZone: 'America/New_York',
})

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////// ELYSIA //////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -64,21 +56,19 @@ export const app = new Elysia()
if (scoreboardCache.has(store.cacheKey)) {
return scoreboardCache.get(store.cacheKey)
}
const isFootball = params.sport === 'football'
// create url to fetch json

// get division from url
const division = params['*'].split('/')[0]
// football uses year / week numbers and P for playoffs
// other sports use date format yyyy/mm/dd
const urlDateMatcher = isFootball ? /\d{4}\/(\d{2}|P)/ : /\d{4}\/\d{2}\/\d{2}/

// find date in url
const urlDateMatcher = /(\d{4}\/\d{2}\/\d{2})|(\d{4}\/(\d{2}|P))/
let urlDate = params['*'].match(urlDateMatcher)?.[0]
if (!urlDate && isFootball) {
// football - use last playoff year if no date is provided
urlDate = `${new Date().getFullYear() - 1}/P`
}

// if not date in passed in url, fetch date from today.json
if (!urlDate) {
// others - use current date if no date is provided
urlDate = urlDateFormatter.format(new Date()).replaceAll('-', '/')
urlDate = await getTodayUrl(params.sport, division)
}

const url = `https://data.ncaa.com/casablanca/scoreboard/${params.sport}/${division}/${urlDate}/scoreboard.json`

// fetch data
Expand Down Expand Up @@ -115,6 +105,29 @@ console.log(`Server is running at ${app.server?.hostname}:${app.server?.port}`)
/////////////////////////////// FUNCTIONS ////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

/**
* Fetch proper url date for today from ncaa.com
* @param sport - sport to fetch
* @param division - division to fetch
*/
async function getTodayUrl(sport: string, division: string) {
// check cache
const cacheKey = `today-${sport}-${division}`
if (cache.has(cacheKey)) {
return cache.get(cacheKey)
}
console.log(`Fetching today.json for ${sport} ${division}`)
const req = await fetch(
`https://data.ncaa.com/casablanca/schedule/${sport}/${division}/today.json`
)
if (!req.ok) {
throw new NotFoundError(JSON.stringify({ message: 'Resource not found' }))
}
const data = await req.json()
cache.set(cacheKey, data.today)
return data.today as string
}

/**
* Fetch data from ncaa.com
* @param opts.path - path to fetch from ncaa.com
Expand Down

0 comments on commit e712704

Please sign in to comment.