forked from master117/MagicMirror-FootballLeagues
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
254 lines (224 loc) · 9.37 KB
/
Copy pathnode_helper.js
File metadata and controls
254 lines (224 loc) · 9.37 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
var NodeHelper = require('node_helper');
var request = require('request');
var moment = require('moment');
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("Refresh 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',
url: 'http://api.football-data.org/v2/competitions/' + league.toString(),
headers: {
'X-Auth-Token': apiKey
}
};
request(options,
function (error, response, 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',
url: 'http://api.football-data.org/v2/competitions',
headers: {
'X-Auth-Token': apiKey
}
};
// Request all available leagues
request(options,
function (error, response, 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;
}
}
}
});
}
},
// Constantly asks for Matches and sends notifications once they arrive
getMatches: function (leagueId, apiKey, refreshTime, id)
{
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',
url: 'http://api.football-data.org/v2/competitions/' + leagueId.toString() + '/matches?dateFrom=' + begin + '&dateTo=' + end,
headers: {
'X-Auth-Token': apiKey
}
};
request(options, function (error, response, body) {
try {
var data = JSON.parse(body);
var fix = data.matches;
}
catch (e)
{
console.log("Error with matches from: " + leagueId);
}
self.sendSocketNotification('MATCHES' + id, {
leagueId: leagueId,
matches: fix
});
setTimeout(function () {
self.getMatches(leagueId, apiKey, refreshTime);
}, refreshTime);
});
},
// Constantly asks for LeagueTables and sends notifications once they arrive
getTable: function (leagueId, apiKey, refreshTime, id)
{
var self = this;
var options = {
method: 'GET',
url: 'http://api.football-data.org/v2/competitions/' + leagueId.toString() + '/standings',
headers: {
'X-Auth-Token': apiKey
}
};
request(options, function (error, response, body)
{
try
{
var data = JSON.parse(body);
if (data.hasOwnProperty('standing'))
var tab = data.standing;
else
var tab = data.standings;
}
catch (e)
{
console.log("Error with table: " + leagueId);
}
self.sendSocketNotification('TABLE' + id,
{
leagueId: leagueId,
table: tab
});
setTimeout(function() {
self.getTable(leagueId, apiKey, refreshTime);
},
refreshTime);
});
},
// Aquires TeamLogos
getTeamLogos: function (leagueId, apiKey, id)
{
var self = this;
var options = {
method: 'GET',
url: 'http://api.football-data.org/v2/competitions/' + leagueId.toString() + '/teams',
headers: {
'X-Auth-Token': apiKey
}
};
request(options, function (error, response, body)
{
try
{
var teamLogos = {};
var data = JSON.parse(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);
}
}
}
catch (e)
{
console.log("Error with logos: " + leagueId);
}
self.sendSocketNotification('LOGO' + id,
{
leagueId: leagueId,
table: teamLogos
});
/*
for (var i = 0; i < data.teams.length; i++)
{
var idString = data.teams[i]._links.self.href;
idString = idString.substring(idString.lastIndexOf("/") + 1, idString.length);
teamIds.push(idString);
}*/
});
},
// 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);
}
}
});