forked from mcl8on/MMM-PGA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESPN.js
206 lines (139 loc) · 6.79 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const moment = require('moment');
const request = require('request');
module.exports = {
url: "https://site.web.api.espn.com/apis/site/v2/sports/golf/leaderboard?league=pga",
//url: "https://site.api.espn.com/apis/site/v2/sports/golf/leaderboard?event=401219795",
urlTournamentList: "https://www.espn.com/golf/schedule/_/tour/pga?_xhr=pageContent&offset=-04%3A00",
getTournamentData: function(callback){
request({
url: this.url,
method: 'GET'
}, (error, response, body) => {
console.log("MMM-PGA retrieving Tournament Data");
if (!error && response.statusCode == 200) {
var ESPNObj = JSON.parse(body).events;
//Build JSON object that will be passed to Client Side
var event = null;
//TODO quick fix for 10/27 must change eventually to suppourt multiple events at the same time
//For now there are two events one is cancelled and one is active so just search through and skip
//the cancelled event and sho for non cancelled event
for(j=0; j<ESPNObj.length; j++){
event = ESPNObj[j];
var eventStatus = event.status.type.name;
if (eventStatus != "STATUS_CANCELED"){
event = ESPNObj[j];
}
}
tournament = {};
//Tournament Details
tournament.name = event.shortName;
tournament.date = this.getEventDate(event.date, event.endDate);
tournament.location = this.getEventLocation(event);
tournament.statusCode = event.status.type.name;
tournament.status = event.competitions[0].status.type.detail;
tournament.purse = event.displayPurse;
tournament.defendingChamp = event.defendingChampion ? event.defendingChampion.athlete.displayName : ""
tournament.currentRound = this.getCurrentRound(event);
tournament.playoff = false;
//Load the Players for the tournament
tournament.players = [];
if (tournament.statusCode != "STATUS_SCHEDULED"){
var espnPlayers = event.competitions[0].competitors;
for(var i in espnPlayers) {
var espnPlayer = espnPlayers[i];
if (espnPlayer.status.playoff) tournament.playoff = true;
tournament.players.push({
"name" : espnPlayer.athlete.displayName,
"position" : espnPlayer.status.position.displayName,
"posId" : parseInt(espnPlayer.status.position.id),
"flagHref" : espnPlayer.athlete.flag.href,
"score" : espnPlayer.statistics[0].displayValue,
"thru" : this.getPlayerThru(espnPlayer),
"roundScore": this.getRoundScore(espnPlayer, tournament.currentRound),
"id" : espnPlayer.athlete.id,
"sortOrder" : espnPlayer.sortOrder,
"playoff" : espnPlayer.status.playoff
});
}
}
//Function to send SocketNotification with the Tournament Data
callback(tournament);
} else {
console.log("MMM-PGA Error Loading Tournament Error Code:" + JSON.stringify(error) + " Status Code: " + response.statusCode );
}
});
},
getTournaments: function(numTournaments, callback){
request({
url: this.urlTournamentList,
method: 'GET'
}, (error, response, body) => {
console.log("MMM-PGA Retrieving Tounament List");
if (!error && response.statusCode == 200) {
var ESPNObj = JSON.parse(body).events;
//Only look at future Tournaments
ESPNObj = ESPNObj.filter(function (tournament){
return ((tournament.status == "pre")||(tournament.status == "in"));
});
tournaments = [];
for (i=0;i<ESPNObj.length;i++){
var tournament = ESPNObj[i];
tournaments.push({
"name" : tournament.name,
"date" : this.getEventDate(tournament.startDate,tournament.endDate),
"location" : tournament.locations[0].venue.fullName,
"purse" : this.setUndefStr(tournament.purse,"TBD"),
"defendingChamp" : this.setUndefStr(tournament.athlete.name)
});
}
callback(tournaments);
} else {
console.log("MMM-PGA Error Loading Tournaments Error Code:" + JSON.stringify(error) + " Status Code: " + response.statusCode );
}
});
},
getCurrentRound: function(event){
//logic to handle playoffs For now we only showing information pertaininhg to rounds in regulation
currentRound = event.competitions[0].status.period;
totalRounds = event.tournament.numberOfRounds;
return (currentRound<=totalRounds)?currentRound:totalRounds;
},
getEventDate: function(start, end){
var startDate = moment(start, "YYYY-MM-DD HH:mm Z").local().format("MMM D");
var endDate = moment(end, "YYYY-MM-DD HH:mm Z").local().format("MMM D");
return startDate + " - " + endDate;
},
getEventLocation: function(event){
var course = event.courses[0];
var city = this.setUndefStr(course.address.city);
var state = this.setUndefStr(course.address.state);
var appendstring =", ";
if (city.length == 0 || state.length ==0){
appendstring = "";
}
return course.name+ " " + city + appendstring + state;
},
getRoundScore: function (player,round){
var roundScore = "-";
var linescore = player.linescores[round-1];
if (!(typeof linescore == 'undefined' || linescore == null)){
roundScore = linescore.displayValue;
}
return roundScore;
},
getPlayerThru: function (player) {
var displayValue = player.status.displayValue;
var append = (player.status.startHole == "1") ? "" : "*";
if (typeof displayValue == 'undefined' || displayValue == null) {
return player.status.displayThru + append;
}
var teeTime = moment(displayValue, "YYYY-MM-DD HH:mm:ss Z");
if (teeTime.isValid()) {
return teeTime.local().format("h:mm a") + append;
}
return displayValue;
},
setUndefStr: function(obj, defStr = ""){
return (typeof obj == "undefined")?defStr:obj;
}
};