Something went wrong. Try again.
Terminal friendly sport schedule
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566import axios from 'axios';
const sportMap = { basketball: 'nba', hockey: 'nhl', football: 'nfl', baseball: 'mlb'};
// helper to get espn url given a team codeconst scheduleURL = (sport, team) => `https://site.api.espn.com/apis/site/v2/sports/${sport}/${sportMap[sport]}/teams/${team}/schedule`;
export const getESPN = async (sport, teamName, fullName) => { try { const espnResp = await axios.get(scheduleURL(sport, teamName));
const events = espnResp.data.events;
const startIndex = events.findIndex( event => !['STATUS_FINAL', 'STATUS_POSTPONED'].includes( event.competitions[0].status.type.name ) );
// when no active games return empty response if (startIndex === -1) return;
const gameRows = [...events].slice(startIndex, startIndex + 3);
const parsedRows = gameRows.map(game => { const title = game.shortName; const competition = game.competitions[0]; const status = competition.status.type.shortDetail;
const tvGames = competition.broadcasts.filter( broad => broad.type.shortName.toLowerCase() === 'tv' ); let broadcast; const nationalIndex = tvGames.findIndex( tv => tv.market.type.toLowerCase() === 'national' ); if (nationalIndex !== -1) { broadcast = tvGames[nationalIndex].media.shortName; } else { broadcast = 'Local Network'; }
return [title, status, broadcast].join('\n'); });
// edge of season fill cells with space so that it fits in grid while (parsedRows.length < 3) parsedRows.push(' ');
return { name: sport, games: parsedRows, team: fullName }; } catch (error) { console.log(`Error getting data for ${sport}`, error); return; }};