-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·143 lines (129 loc) · 4.28 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
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
const cron = require("node-cron");
const axios = require("axios").default;
const cheerio = require("cheerio");
const config = require("./config.json");
const { Webhook } = require("simple-discord-webhooks");
const postman = new Webhook(config.discordWebhookURL);
const request = async (method, url, data = {}) => {
let options = {
method,
url: url,
headers: {
Accept: "*/*",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:104.0) Gecko/20100101 Firefox/104.0",
"Content-Type": "application/x-www-form-urlencoded",
},
data: new URLSearchParams(data).toString(),
};
return axios(options)
.then((response) => {
return response;
})
.catch((error) => {
return error.response || error;
});
};
const dateYMD = (date) => {
var d = date.getDate();
var m = date.getMonth() + 1; // Month from 0 to 11
var y = date.getFullYear();
return `${y}-${m < 10 ? `0${m}` : m}-${d <= 9 ? `0${d}` : d}`;
};
const capitalize = (word) => {
return `${word.charAt(0).toUpperCase()}${word.slice(1)}`;
};
cron.schedule(config.cronDelay, async () => {
const date = dateYMD(new Date());
try {
const embed = {
title: "CROUS menu tracker",
color: 0xe30512,
thumbnail: {
url: "https://www.etudiant.gouv.fr/sites/default/files/2020-09/Logo_Les_Crous_1_0.png",
},
footer: {
text: "CROUS menu tracker © LockBlock-dev",
},
};
const res = await request("POST", config.endpoint, {
ru: config.crousId,
dt: date, // YYYY-MM-DD
});
const resultHTML = cheerio.load(res.data);
const menu = {
period: capitalize(resultHTML(".menuRu > h2").text()), // todo: handle evening meal (when we have 2 periods)
self: {
entrees: [],
plat: [], // HTML DOES NOT INCLUDE AN S FOR SOME REASONS >:(
desserts: [],
},
brasserie: {
entrees: [],
plats: [],
desserts: [],
},
};
resultHTML(".menuRu > ul").each((idx, ul) => {
// for each <ul> of .menuRu
resultHTML(ul)
.children("li")
.each((_, li) => {
// for each <li> of <ul>
const prev = resultHTML(ul).prev().text();
let [category, type] = prev.split(" "); // from "Entrées Self"
category = category.toLowerCase().replace("é", "e"); // Entrées ; Plat(s) ; Desserts
type = type.toLowerCase(); // self ; brasserie
menu[type][category].push(resultHTML(li).text()); // append the meal to the correct list
});
});
/*
Parsing HTML:
<div class="menuRu">
<h2>midi</h2>
<h4>CATEGORY TYPE</h4>
<ul class="liste-plats">
<li>Something 1</li>
<li>Something 2</li>
<li>Something 3</li>
</ul>
...
</div>
*/
if (res.status != 200) return;
embed.description = `Menu pour le ${date}`;
embed.fields = [
{
name: "CROUS",
value: config.crousList[config.crousId],
},
{
name: "Période",
value: menu.period,
},
{
name: "Self",
value: `• Entrées :
\`\`\` - ${menu.self.entrees.join("\n - ")}\`\`\`
• Plats :
\`\`\` - ${menu.self.plat.join("\n - ")}\`\`\`
• Desserts :
\`\`\` - ${menu.self.desserts.join("\n - ")}\`\`\`
`,
},
{
name: "Brasserie",
value: `• Entrées :
\`\`\` - ${menu.brasserie.entrees.join("\n - ")}\`\`\`
• Plats :
\`\`\` - ${menu.brasserie.plats.join("\n - ")}\`\`\`
• Desserts :
\`\`\` - ${menu.brasserie.desserts.join("\n - ")}\`\`\`
`,
},
];
postman.send(`${config.ownerId ? `<@${config.ownerId}>` : ""}`, [embed]);
console.log(`${date} - menu sent!`);
} catch (e) {
console.error(`${date} - ${e}`);
}
});