-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (95 loc) · 2.95 KB
/
index.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
let activeLeagues = []
const { getClubNextMatches, getCompetitionData } = require("./crowler.js");
const { getClubName, getCompetitionName } = require("./name.js");
const { activeLeaguesToday, matchesToday, liveMatches } = require("./live.js");
console.log("test")
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(cors({
origin: 'https://buchaplay.netlify.app'
}));
app.post('/api/refresh', async (req, res) => {
activeLeagues = await activeLeaguesToday();
res.send('Updated');
});
// Competition's Past Matches and Upcoming Matches, Including Live Matches
app.get('/competition/:slug', async (req, res) => {
try {
const { slug } = req.params;
const adress = getCompetitionName(slug);
if (adress !== 'none') {
const competitionData = await getCompetitionData(adress)
res.send(competitionData);
} else {
res.status(400).send('Invalid competition parameter');
}
} catch (error) {
console.error(error);
res.status(500).send('Internal server error');
}
});
// Next Matches From a Club
app.get('/nextmatches/:slug', async (req, res) => {
try {
const { slug } = req.params;
const adress = getClubName(slug);
if (adress !== 'none') {
const nextGame = await getClubNextMatches(adress)
res.send(nextGame);
} else {
res.status(400).send('Invalid club parameter');
}
} catch (error) {
console.error(error);
res.status(500).send('Internal server error');
}
});
// Schedule Matches For Today
app.get('/todaymatches', async (req, res) => {
try {
if (activeLeagues.length === 0){
activeLeagues = await activeLeaguesToday();
}
const matches = await matchesToday(activeLeagues);
res.send(matches);
} catch (error) {
console.error(error);
res.status(500).send('Internal server error');
}
});
// Live Matches
app.get('/livematches', async (req, res) => {
try {
if (activeLeagues.length === 0){
activeLeagues = await activeLeaguesToday();
}
const matches = await liveMatches(activeLeagues);
res.send(matches);
} catch (error) {
console.error(error);
res.status(500).send('Internal server error');
}
});
// Only Leagues That Have Matches Today
app.get('/activeleagues', async (req, res) => {
try {
if (activeLeagues.length === 0){
activeLeagues = await activeLeaguesToday();
}
res.send(activeLeagues);
} catch (error) {
console.error(error);
res.status(500).send('Internal server error');
}
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err);
res.status(500).send('Internal server error');
});
// Start server
app.listen(3000, async () => {
console.log(`Server is running...`);
});