-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·215 lines (187 loc) · 5.04 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
import express from "express";
import axios from "axios";
import fs from "fs";
import bodyParser from "body-parser";
import * as dotenv from "dotenv";
import {
sendLeaderboard,
sendtxt,
sendFixture,
sendWinner,
sendThnx,
} from "./commands/sendtxt.js";
dotenv.config();
const server = express();
const hostname = "192.168.0.7";
const port = "3000";
server.set("view engine", "ejs");
server.use(express.static("public"));
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: true }));
const newLeaderboard = [];
const newFixture = [];
function addObjectToArray(name, numElements, array) {
const newObject = {
matchDay: name,
matches: [],
};
for (let j = 0; j < numElements; j++) {
newObject.matches.push({
matchId: array[j].mId,
matchName: `${array[j].htName} vs ${array[j].atName} `,
date: array[j].dateTime,
});
}
newFixture.push(newObject);
}
// PUT DATA ON A JSON FILE
function createJSONfile(location, data) {
fs.writeFile(location, JSON.stringify(data), function (err) {
if (err) {
return err;
}
console.log("The file was saved!");
});
}
// FETCH THE LEAG UE DATA FROM API
async function fetchLeaderboard() {
fs.readFile("./data/leaderBoard.json", "utf8", function (err, contents) {
if (err) {
console.log(err);
}
let data = JSON.parse(contents);
for (let i = 0; i < data.data.value.notation; i++) {
newLeaderboard[i] = {
teamName: data.data.value.rest[i].teamName,
overallPoints: data.data.value.rest[i].overallPoints,
currentGamedayPoints: data.data.value.rest[i].currentGamedayPoints,
};
}
createJSONfile("./data/newleaderboard.json", newLeaderboard);
});
}
async function fetchFixture() {
const apiUrl =
"https://gaming.uefa.com/en/uclfantasy/services/feeds/fixtures/fixtures_60_en.json";
const filePath = "./data/fixture.json";
downloadJSON(apiUrl, filePath);
}
async function fetchOldLeaderboard(url) {
const apiUrl = url;
const filePath = "./data/leaderBoard.json";
downloadJSON(apiUrl, filePath);
}
// FETCH THE LEAG UE DATA FROM API
async function simplifyFixture() {
fs.readFile("./data/fixture.json", "utf8", function (err, contents) {
if (err) {
console.log(err);
}
let data = JSON.parse(contents);
console.log(data.data.value.length);
for (let i = 0; i < data.data.value.length; i++) {
addObjectToArray(
data.data.value[i].mdId,
data.data.value[i].match.length,
data.data.value[i].match
);
}
createJSONfile("./data/newFixture.json", newFixture);
});
}
server.get("/", (req, res) => {
res.render("admin");
});
server.post("/mainpage", (req, res) => {
const admin = {
name: process.env.ADMIN || "admin",
pass: process.env.PASS || "12345",
};
let username = req.body.username;
let password = req.body.password;
if (username === admin.name && password === admin.pass) {
res.render("mainpage");
} else res.render("admin");
});
// MAINPAGE
server.get("/mainpage", (req, res) => {
res.redirect("/");
});
server.post("/apiOldPost", (req, res) => {
let url = req.body.url;
console.log(url)
downloadJSON(url, "./data/leaderBoard.json");
res.render("mainpage")
// fetchOldLeaderboard(url);
});
server.post("/apiPost", (req, res) => {
fetchLeaderboard();
res.render("mainpage")
});
server.post("/apiFixture", (req, res) => {
fetchFixture();
res.render("mainpage")
});
server.post("/apiPostFixture", (req, res) => {
simplifyFixture();
res.render("mainpage")
});
// FIXTURE API
server.get("/fixture", (req, res) => {
fs.readFile("./data/newFixture.json", "utf8", function (err, contents) {
if (err) {
console.log(err);
}
res.send(JSON.parse(contents));
});
});
// LEADERBOARD API
server.get("/ourFantasy", async (req, res) => {
fs.readFile("./data/newleaderboard.json", "utf8", function (err, contents) {
if (err) {
console.log(err);
}
res.json(JSON.parse(contents));
});
});
// SEND MESSAGES
server.post("/txt", async (req, res) => {
let text = req.body.text;
console.log(text);
sendtxt(text);
res.render("mainpage")
});
server.post("/leaderboard", async (req, res) => {
sendLeaderboard();
res.render("mainpage")
});
server.post("/sendFixture", async (req, res) => {
sendFixture();
res.render("mainpage")
});
export default function keepAlive() {
server.listen(3000, () => {
console.log(`Server is ready`);
});
}
export async function readJson(location) {
return new Promise((callback, reject) => {
fs.readFile(location, "utf8", function (err, contents) {
if (err) {
console.log(err);
reject(err);
}
callback(JSON.parse(contents));
});
});
}
async function downloadJSON(url, filePath) {
try {
const response = await axios.get(url);
const jsonContent = JSON.stringify(response.data, null, 2);
fs.writeFileSync(filePath, jsonContent);
console.log("JSON data downloaded and saved to", filePath);
} catch (error) {
console.error("The link you provided is old or broken");
}
}