-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
328 lines (291 loc) · 9.27 KB
/
main.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Import des modules nécessaires
import axios from "axios"
import * as cheerio from "cheerio"
import express from "express"
let VERBOSE = true
// if -q is passed, disable verbose
if (process.argv.includes("-q")) VERBOSE = false
// URL à fetch
// const url = "https://liquipedia.net/rocketleague/Liquipedia:Matches"
// const inputCompetitionRegex = "^RLCS"
const competitionMatches = (event, regex) => {
if (!event.competition.match(regex)) return false
return true
}
// const inputTeamsMatches = "^(KC|M8|VIT)$"
// const inputTeamsMatches = ""
const teamsMatches = (event, regex) => {
if (!event.team1.match(regex) && !event.team2.match(regex)) return false
return true
}
const teamsFullnameMatches = (event, regex) => {
if (!event.team1fullName.match(regex) && !event.team2fullName.match(regex))
return false
return true
}
// Fonction pour récupérer les matchs à venir de Rocket League
async function fetchMatches(url, opts) {
const {
competitionRegex = ``,
teamsRegex = ``,
teamsRegexUseFullnames = false,
conditionIsOr = false,
ignoreTbd = false,
shouldVerbose = false,
} = opts || {}
try {
// Fetch de la page
const response = await axios.get(url)
// Charger la réponse HTML dans cheerio
const $ = cheerio.load(response.data)
// Tableau pour stocker les événements
const events = []
// test if element exists
const containerSelectorsToTry = [
// ".matches-list div:nth-child(2) div:nth-child(2) .infobox_matches_content",
// first parent of .infobox_matches_content
".wikitable",
]
let elements = null
for (const selector of containerSelectorsToTry) {
elements = $(selector)
if (elements.length > 0) break
}
// Sélection des éléments du tableau de matchs
elements.each((index, element) => {
// if (shouldVerbose) {
// console.log(`ELEMENT TEXT: ${$(element).text()}`)
// }
// Récupération des dates, équipes et nom de la compétition
const dateTimestamp = parseInt(
$(element).find(".timer-object").attr("data-timestamp")
)
if (dateTimestamp < Date.now() / 1000 - 3600 * 2) {
// if (shouldVerbose) console.log("Ignoring match too old")
return
}
let team1 =
$(element).find(".team-left span.team-template-text").text().trim() ||
$(element).find(".team-left").text().trim()
let team2 =
$(element).find(".team-right span.team-template-text").text().trim() ||
$(element).find(".team-right").text().trim()
let team1fullName
let team2fullName
try {
team1fullName =
$(element).find(".team-left span").attr("data-highlightingclass") ||
null
team2fullName =
$(element).find(".team-right span").attr("data-highlightingclass") ||
null
} catch {}
if (!team1 || !team2) {
if (shouldVerbose) console.log("Ignoring match with missing teams")
return
}
if (ignoreTbd && team1 === "TBD" && team2 === "TBD") {
if (shouldVerbose) console.log("Ignoring match with TBD team")
return
}
// OLD WAY OF OBTAINING COMPETITION, DOES NOT WORK FOR ALL GAMES
// const competition = $(element)
// .find(".match-filler tbody tr td:nth-child(2)")
// .text()
// .trim()
let competition = $(element).find(".match-filler")
competition.find(".match-countdown").remove()
competition = competition.text().trim()
let descriptor
let descriptorMoreInfo
try {
const versus = $(element).find(".versus")
if (
// has score
$(versus)
.text()
.trim()
.match(/[0-9]+\:[0-9]+/)
) {
descriptor = $(versus).text().trim()
} else {
descriptor =
$(element).find(".versus abbr").text() ||
$(element).find(".versus abbr").html() ||
$(element).find(".versus abbr").attr("title") ||
null
descriptorMoreInfo =
$(element).find(".versus abbr").attr("title") || null
}
} catch (error) {
// ignore missing competition subpart
}
if (team1 === "TBD") team1 = "???"
if (team2 === "TBD") team2 = "???"
const eventData = {
uid:
`${competition.replace(
/[^a-zA-Z0-9\-]/g,
""
)}/${dateTimestamp}/${team1}/${team2}` +
"@liquipedia-calendar.snwfdhmp.com",
dateTimestamp,
team1,
team2,
team1fullName,
team2fullName,
competition,
summary:
`${team1} vs ${team2} ` +
(descriptor ? `(${descriptor}) ` : "") +
`[${competition}]`,
description: `${team1fullName || team1} vs ${team2fullName || team2} ${
descriptor
? "(" + (descriptorMoreInfo || descriptor || "") + ") "
: ""
}[${competition}]`,
}
const competitionOk = competitionMatches(eventData, competitionRegex)
const teamsOk = teamsRegexUseFullnames
? teamsFullnameMatches(eventData, teamsRegex)
: teamsMatches(eventData, teamsRegex)
const logMessage = `eventData=${JSON.stringify(
eventData
)} opts=${JSON.stringify(opts)}`
// if condition is OR, need at least one to be true
if (conditionIsOr && !competitionOk && !teamsOk) {
if (shouldVerbose)
console.log(`Ignoring match due to filter conditions ${logMessage}`)
return
}
// if condition is AND, need both to be true
if (!conditionIsOr && (!competitionOk || !teamsOk)) {
if (shouldVerbose)
console.log(`Ignoring match due to filter conditions ${logMessage}`)
return
}
events.push(eventData)
if (shouldVerbose) console.log({ eventData })
})
// display all opts as \t separated string
console.log(
`${
events.length
} matches fetched from ${url}\tcompetition_regex=${competitionRegex}\tteams_regex=${teamsRegex}\t${Object.entries(
opts
)
.map(([key, value]) => `${key}=${value}`)
.join("\t")}`
)
// Retourner les événements sous forme d'ICS
return events
} catch (error) {
console.error("Erreur lors de la récupération des matchs:", error)
throw error
}
}
// desiredFormat: YYYYMMDD'T'HHmmss'Z'
const timestampToIcs = (timestamp) => {
const date = new Date(timestamp * 1000)
// produce YYYYMMDD
const datePart = date.toISOString().split("T")[0].replace(/-/g, "")
// produce HHmmss
const timePart = date
.toISOString()
.split("T")[1]
.replace(/:/g, "")
.replace(/\.[0-9]+/, "")
return `${datePart}T${timePart}`
}
const buildCalendar = (events) => {
// events = events.slice(0, 2)
const headers = `BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//com.snwfdhmp.liquipedia-calendar//NONSGML v1.0//EN
CALSCALE:GREGORIAN
METHOD:PUBLISH`
.split("\n")
.map((line) => line.trim())
.join("\n")
const footers = `END:VCALENDAR`
const body = events
.map((event) =>
[
`BEGIN:VEVENT`,
`UID:${event.uid}`,
`LOCATION:${event.competition}`,
`DTSTAMP:${timestampToIcs(event.dateTimestamp)}`,
`DTSTART:${timestampToIcs(event.dateTimestamp)}`,
`DURATION:PT40M`,
`DESCRIPTION:${event.description.split("\n").join("\\n")}`,
`SUMMARY:${event.summary}`,
`END:VEVENT`,
].join("\n")
)
.join("\n\n")
return `${headers}\n\n${body}\n\n${footers}`
}
// todo: store events and set DTSTAMP when event discovered/updated
// const cal = buildCalendar(await fetchMatches())
// console.log(cal)
// fs.writeFileSync("rocket.ics", cal)
const app = express()
// log requests
app.use((req, res, next) => {
const ip = req.headers["x-forwarded-for"] || req.connection.remoteAddress
console.log(
`${new Date().toISOString()}\t${ip.padEnd(15)} ${req.method} ${
req.url
}${JSON.stringify(req.query)}`
)
next()
})
app.get("/", (req, res) => {
const __dirname = import.meta.url
.replace("file://", "")
.split("/")
.slice(0, -1)
.join("/")
res.sendFile(__dirname + "/url_builder.html")
})
app.get("/matches.ics", async (req, res) => {
// read url, competition_regex and teams_regex from query params
const {
url,
competition_regex: competitionRegex,
teams_regex: teamsRegex,
teams_regex_use_fullnames: teamsRegexUseFullnames,
condition_is_or: conditionIsOr,
ignore_tbd: ignoreTbd,
verbose: shouldVerbose,
} = req.query
if (!url) {
res.status(400).send("Missing url query param")
return
}
if (!url.startsWith("https://liquipedia.net/")) {
res.status(403).send("Use liquipedia URL")
return
}
try {
const ics = buildCalendar(
await fetchMatches(url, {
competitionRegex,
teamsRegex,
teamsRegexUseFullnames,
conditionIsOr,
ignoreTbd,
shouldVerbose,
})
)
res.setHeader("Content-Type", "text/calendar; charset=utf-8")
res.setHeader("Content-Disposition", "attachment; filename=calendar.ics")
res.send(ics)
} catch (error) {
res.status(500).send("Erreur lors de la récupération des matchs")
}
})
const PORT = process.env.PORT || 9059
app.listen(PORT, () => {
console.log(`Server started on 0.0.0.0:${PORT}`)
})