-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
espn.js
145 lines (114 loc) · 3.81 KB
/
espn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const fetch = require('node-fetch');
const seasonStageMapping = {
1: 'PRE',
2: 'REG',
3: 'POST',
4: 'OFF',
};
const teamNameMapping = {
LAR: 'LA',
WSH: 'WAS',
};
const statisticTypes = [
'passingYards',
'rushingYards',
'receivingYards',
'totalTackles',
'sacks',
'kickoffYards',
'interceptions',
'passingTouchdowns',
'quarterbackRating',
'rushingTouchdowns',
'receptions',
'receivingTouchdowns',
'totalPoints',
'totalTouchdowns',
'puntYards',
'passesDefended'
];
function getFullUrl(apiVersion, path) {
return `https://site.api.espn.com/apis/site/${apiVersion}/sports/football/nfl${path}`;
}
function getPostGameStatus(period) {
return period > 4 ? 'final-overtime' : 'final';
}
function getInGameStatus(period) {
return period > 4 ? 'overtime' : period;
}
function getGameStatus(status = {}) {
if (status.type?.state === 'pre') {
return 'pregame';
} else if (status.type?.name === 'STATUS_HALFTIME') {
return 'halftime';
} else if (status.type?.state === 'post') {
return getPostGameStatus(status.period);
}
return getInGameStatus(status.period);
}
function getTeamName(competitor = {}) {
const team = competitor.team?.abbreviation;
return teamNameMapping[team] || team;
}
function mapEventEntry(event = {}) {
const ongoing = !['pre', 'post'].includes(event.status?.type?.state);
const possessionTeamId = event.competitions?.[0]?.situation?.possession;
const possessionTeam = event.competitions?.[0]?.competitors?.find(c => c.id === possessionTeamId);
return {
timestamp: event.date,
status: getGameStatus(event.status),
remainingTime: ongoing && event.status?.displayClock,
ballPossession: getTeamName(possessionTeam),
inRedZone: event.competitions?.[0]?.situation?.isRedZone,
homeTeam: getTeamName(event.competitions?.[0]?.competitors?.[0]),
homeScore: event.competitions?.[0]?.competitors?.[0]?.score,
homeLogo: event.competitions?.[0]?.competitors?.[0]?.team?.logo,
awayTeam: getTeamName(event.competitions?.[0]?.competitors?.[1]),
awayScore: event.competitions?.[0]?.competitors?.[1]?.score,
awayLogo: event.competitions?.[0]?.competitors?.[1]?.team?.logo
};
}
async function getData() {
const response = await fetch(getFullUrl('v2', '/scoreboard'));
if (!response.ok) {
throw new Error('failed to fetch scoreboard');
}
const parsedResponse = await response.json();
const details = {
week: parsedResponse?.week?.number,
season: parsedResponse?.season?.year,
stage: seasonStageMapping[parsedResponse?.season?.type]
};
const events = parsedResponse?.events || [];
const scores = events.map(mapEventEntry).sort((a, b) => {
if (a.timestamp === b.timestamp) {
return 0;
}
return a.timestamp > b.timestamp ? 1 : -1
});
return { details, scores };
}
function mapPlayerEntry(player = {}) {
return {
value: player.displayValue,
name: player.athlete.fullName,
avatar: player.athlete.headshot.href,
team: getTeamName(player),
logo: player?.team?.logos?.[0]?.href,
};
}
async function getStatistics(type) {
if (!statisticTypes.includes(type)) {
throw new Error(`Unsupported statistic type: ${type}`);
}
const response = await fetch(getFullUrl('v3', '/leaders'));
if (!response.ok) {
throw new Error('failed to fetch scoreboard');
}
const parsedResponse = await response.json();
const category = parsedResponse?.leaders?.categories?.find(c => c.name === type);
const players = category?.leaders || [];
const leaders = players.map(mapPlayerEntry);
return leaders;
}
module.exports = { getData, getStatistics };