/
server.js
300 lines (275 loc) · 9.57 KB
/
server.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const http = require('http');
const fetch = require('node-fetch');
const express = require('express');
const graphqlHTTP = require('express-graphql');
const {makeExecutableSchema} = require('graphql-tools');
const {typeDefs, resolvers} = require('fpl-api-graphql');
const {fetchBootstrap, fetchEntry, fetchEntryHistory, fetchClassicLeague, fetchElementSummary, fetchLive, fetchEntryEvent} = require('fpl-api');
// build executable schema from typedefs and resolvers
const schema = makeExecutableSchema({typeDefs, resolvers});
let leagueId = null;
let loadedPlayerIds = [210166, 4984122, 2249091, 1159430, 126466, 404123, 130438, 1025143, 493380, 552453, 1260577, 1618273, 219691, 1259705, 3958980, 444051, 3034647, 531121, 2218701, 131342, 3524888, 3930276, 3126178, 737536, 18575, 1884253];
let currentRound = 0;
let allPlayersList = [];
// express app
const app = express();
// graphql
app.use(
'/graphql',
graphqlHTTP({
schema,
graphiql: true,
}),
);
//let app = express();
app.use(express.static('build'));
app.get('/api/getManagerList', function (req, res) {
leagueId = req.query.leagueId;
fetchClassicLeague(leagueId)
.then(values => {
loadedPlayerIds = values.map(p => p.entry);
res.type('application/json')
.send(loadedPlayerIds)
.end();
}).catch((error) => {
res.type('application/json')
.send(error)
.end();
});
});
// app.get('/api/getManagerList', function (req, res) {
// let leagueId = req.query.leagueId;
// fetch(`https://fantasy.premierleague.com/api/leagues-classic/${leagueId}/standings/?page_new_entries=1&page_standings=1&phase=1`, {
// method: 'GET',
// headers: {
// 'Content-Type': 'application/json',
// 'Accept': 'application/json',
// },
// })
// .then(r => r.json())
// .then(data => {
// // TODO legg inn nytt kall på side 2 av valgt liga hvis data.standings.has_next = true
// res.type('application/json')
// .send(data)
// .end();
// })
// .catch(error => {
// res.type('application/json')
// .send(error)
// .end();
// });
// });
app.get('/api/stats', function (req, res) {
allPlayersList = [];
fetchBootstrap()
.then(values => {
let stats = {};
values.events.forEach(round => {
if (round.is_current) currentRound = round.id;
Object.assign(stats, {
[round.id]: {
average_entry_score: round.average_entry_score,
chip_plays: round.chip_plays,
highest_score: round.highest_score,
top_element_info: round.top_element_info,
most_captained: round.most_captained,
most_vice_captained: round.most_vice_captained,
most_transferred_in: round.most_transferred_in,
is_current: round.is_current,
finished: round.finished
}
})
});
let allPlayers = {};
values.elements.forEach(player => {
allPlayersList.push(player.id);
allPlayers = {
...allPlayers,
[player.id]: {
first_name: player.first_name,
second_name: player.second_name,
web_name: player.web_name,
selected_by_percent: player.selected_by_percent,
form: player.form,
transfers_in_event: player.transfers_in_event,
transfers_out_event: player.transfers_out_event,
total_points: player.total_points,
in_dreamteam: player.in_dreamteam
}
}
});
stats = {...stats, allPlayers};
res.type('application/json')
.send(stats)
.end();
}).catch((error) => {
res.type('application/json')
.send(error)
.end();
});
});
app.get('/api/scores', function (req, res) {
let teams = req.query.teams.split(',');
Promise.all(teams.map(teamId => {
return fetchEntry(teamId)
.then(entry => {
return fetchEntryHistory(teamId)
.then(data => {
return Promise.resolve({...data, entry});
})
})
}))
.then(data => {
res.type('application/json')
.send(data)
.end();
})
.catch(error => {
res.type('application/json')
.send(error)
.end();
});
});
app.get('/api/playerscores', function (req, res) {
const playersTransferred = req.query.players.split(',');
Promise.all(playersTransferred.map(fetchElementSummary))
.then(values => {
const playerScores = values.reduce(function (prev, curr) {
prev[curr.id] = curr.history;
return prev;
}, {});
res.type('application/json')
.send(playerScores)
.end();
}).catch((error) => {
console.log('error ', error);
res.type('application/json')
.send(error)
.end();
});
});
app.get('/api/test', function (req, res) {
fetchLive(4)
.then(values => {
res.type('application/json')
.send(values)
.end();
}).catch((error) => {
res.type('application/json')
.send(error)
.end();
});
});
app.get('/api/testDirekte', function (req, res) {
fetch(`https://fantasy.premierleague.com/api/leagues-classic/104768/standings/`, {
method: 'GET',
headers: {
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
'Content-Type': 'application/json',
'Accept': 'application/json',
},
}).then(r => r.json())
.then(data => {
res.type('application/json')
.send(data)
.end();
})
.catch(error => {
console.log('getTest error: ', error);
res.type('application/json')
.send(error)
.end();
})
});
app.get('/api/getLeagueInfo', function (req, res) {
leagueId = req.query.leagueId;
fetch(`https://fantasy.premierleague.com/api/leagues-classic/${leagueId}/standings/`, {
method: 'GET',
headers: {
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
'Content-Type': 'application/json',
'Accept': 'application/json',
},
}).then(r => r.json())
.then(data => {
res.type('application/json')
.send(data)
.end();
})
.catch(error => {
console.log('getTest error: ', error);
res.type('application/json')
.send(error)
.end();
})
});
app.get('/api/getTransfers', function (req, res) {
let teams = req.query.teams.split(',');
Promise.all(teams.map(teamId =>
new Promise((resolve, reject) => {
setTimeout(function () {
fetch(`https://fantasy.premierleague.com/api/entry/${teamId}/transfers/`, {
method: 'GET',
headers: {
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
'Content-Type': 'application/json',
'Accept': 'application/json',
},
}).then(r => {
return resolve(r.json());
})
.catch(error => reject(error))
})
})
))
.then(data => {
res.type('application/json')
.send(data)
.end();
})
.catch(error => {
console.log('getTransfers error: ', error);
res.type('application/json')
.send(error)
.end();
})
});
app.get('/api/getLiveData', function (req, res) {
let round = req.query.round;
fetchLive(round)
.then(values => {
res.type('application/json')
.send(values)
.end();
}).catch((error) => {
res.type('application/json')
.send(error)
.end();
});
});
app.get('/api/getEntryPicks', function (req, res) {
let round = req.query.round;
let teams = req.query.teams.split(',');
Promise.all(teams.map(teamId => {
return fetchEntryEvent(teamId, round)
.then(values => { return {
...values,
entryId: teamId
}})
})).then(values => {
res.type('application/json')
.send(values)
.end();
}).catch((error) => {
res.type('application/json')
.send(error)
.end();
});
});
const server = http.createServer(app);
const port = process.env.PORT || 9999;
app.listen(port);
console.log("Server running on: localhost:", port);