-
Notifications
You must be signed in to change notification settings - Fork 5
/
node_helper.js
220 lines (191 loc) · 8.31 KB
/
node_helper.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
var NodeHelper = require('node_helper');
var moment = require('moment');
const fetch = require('node-fetch');
module.exports = NodeHelper.create({
// Gets automatically called when the module starts
start: function () {
console.log("MagicMirror-FootballLeagues Started...");
},
// Starts all data gathering background processes
startGatherData: function (leagues, showLogos, showTables, apiKey, id, showUnavailable) {
// First, gathers all available league ids, gracefully handles if a league doesn't exist
var self = this;
//Calculate maximum polling speed
var refreshTime = 60000 * leagues.length;
console.log("MagicMirror-FootballLeagues is refreshing every: " + refreshTime + "ms.")
//Check whether unavailable leagues should be shown.
if(showUnavailable) {
for(var j = 0; j < leagues.length; j++) {
var league = leagues[j];
var options = {
method: 'GET',
headers: {
'X-Auth-Token': apiKey
}
};
fetch('http://api.football-data.org/v2/competitions/' + league.toString(), options)
.then(res => res.text())
.then(function (body) {
var competition = JSON.parse(body);
// Second, start gathering background processes for available league
self.getMatches(league, apiKey, refreshTime, id);
if(showTables) {
self.getTable(league, apiKey, refreshTime, id);
}
if(showLogos) {
self.getTeamLogos(league, apiKey, id);
}
var cap = ""
if(competition.hasOwnProperty('name')) {
cap = competition.name
}
// Third, send notification that leagues exist
self.sendSocketNotification('LEAGUES' + id, {
name: cap,
id: league
});
});
}
}
else {
var options = {
method: 'GET',
headers: {
'X-Auth-Token': apiKey
}
};
// Fetch all available leagues
fetch('http://api.football-data.org/v2/competitions', options)
.then(res => res.text())
.then(function (body) {
var competitions = JSON.parse(body).competitions;
for(var i = 0; i < leagues.length; i++) {
for(var j = 0; j < competitions.length; j++) {
// Check if current league fits a competition, if it does, the league is available
if(competitions[j].id === leagues[i]) {
// Second, start gathering background processes for available league
self.getMatches(competitions[j].id, apiKey, refreshTime, id);
if(showTables) {
self.getTable(competitions[j].id, apiKey, refreshTime, id);
}
if(showLogos) {
self.getTeamLogos(competitions[j].id, apiKey, id);
}
// Third, send notification that leagues exist
self.sendSocketNotification('LEAGUES' + id, {
name: competitions[j].name,
id: competitions[j].id
});
break;
}
}
}
})
.catch(function (e) {
console.log("MagicMirror-FootballLeagues: Error fetching leagues, is your api key correct?");
console.log(e);
});
}
},
// Constantly asks for Matches and sends notifications once they arrive
getMatches: function (leagueId, apiKey, refreshTime, id) {
console.log("Getting Matches...")
var self = this;
var begin = moment().startOf('isoWeek').format('YYYY-MM-DD');
var end = moment().endOf('isoWeek').format('YYYY-MM-DD');
var options = {
method: 'GET',
headers: {
'X-Auth-Token': apiKey
}
};
fetch('http://api.football-data.org/v2/competitions/' + leagueId.toString() + '/matches?dateFrom=' + begin + '&dateTo=' + end, options)
.then(res => res.text())
.then(function (body) {
var data = JSON.parse(body);
var fix = data.matches;
self.sendSocketNotification('MATCHES' + id, {
leagueId: leagueId,
matches: fix
});
})
.catch(function (e) {
console.log("MagicMirror-FootballLeagues: Error with matches from: " + leagueId);
})
.finally(function (e) {
setTimeout(function () {
self.getMatches(leagueId, apiKey, refreshTime, id);
}, refreshTime);
});
},
// Constantly asks for LeagueTables and sends notifications once they arrive
getTable: function (leagueId, apiKey, refreshTime, id) {
var self = this;
var options = {
method: 'GET',
headers: {
'X-Auth-Token': apiKey
}
};
fetch('http://api.football-data.org/v2/competitions/' + leagueId.toString() + '/standings', options)
.then(res => res.text())
.then(function (body) {
var data = JSON.parse(body);
if(data.hasOwnProperty('standing'))
var tab = data.standing;
else
var tab = data.standings;
self.sendSocketNotification('TABLE' + id,
{
leagueId: leagueId,
table: tab
});
})
.catch(function (e) {
console.log("MagicMirror-FootballLeagues: Error with table: " + leagueId);
})
.finally(function (e) {
setTimeout(function () {
self.getTable(leagueId, apiKey, refreshTime, id);
}, refreshTime);
});
},
// Aquires TeamLogos
getTeamLogos: function (leagueId, apiKey, id) {
var self = this;
var options = {
method: 'GET',
headers: {
'X-Auth-Token': apiKey
}
};
fetch('http://api.football-data.org/v2/competitions/' + leagueId.toString() + '/teams', options)
.then(res => res.text())
.then(function (body) {
var teamLogos = {};
var data = JSON.parse(body);
if(!data.teams)
throw body;
for(var i = 0; i < data.teams.length; i++) {
try {
teamLogos[data.teams[i].id] = data.teams[i].crestUrl;
}
catch(e) {
console.log("Error with logo: " + data.teams[i].id + " - " + data.teams[i].name);
}
}
self.sendSocketNotification('LOGO' + id, { leagueId: leagueId, table: teamLogos });
})
.catch(function (e) {
console.log("MagicMirror-FootballLeagues: Error with logos: " + leagueId);
console.log("MagicMirror-FootballLeagues: Error loading leaguedata for: " + leagueId.toString());
console.log(e);
});
},
// Receives startup notification, containing config data
socketNotificationReceived: function (notification, payload) {
if(notification === 'CONFIG') {
this.startGatherData(payload.leagues, payload.showLogos, payload.showTables, payload.apiKey, payload.id, payload.showUnavailable);
}
}
});