From c533d2c1d579906924237e41b6d71e1601deecae Mon Sep 17 00:00:00 2001 From: davidpkj Date: Tue, 23 Apr 2024 21:45:37 +0200 Subject: mega commit: ics, readme updates, easier usage --- src/files.js | 27 +++++++++++++--- src/ics.js | 62 ++++++++++++++++++++++++++++++++++++ src/main.js | 101 +++++------------------------------------------------------ src/pdf.js | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 188 insertions(+), 98 deletions(-) create mode 100644 src/ics.js create mode 100644 src/pdf.js (limited to 'src') diff --git a/src/files.js b/src/files.js index 970d28d..e5ac76a 100644 --- a/src/files.js +++ b/src/files.js @@ -3,18 +3,37 @@ import * as fs from "fs" const dir = "./public"; +function read(file) { + console.log(`Reading ${file}`); + return fs.readFileSync(file, 'utf8'); +} + +function write(file, content) { + let r = fs.writeFileSync(file, content); + console.log("Done!"); + return r; +} + function readConfig() { - return yml.load(fs.readFileSync(`${dir}/config.yaml`, 'utf8')); + return yml.load(read(`${dir}/config.yaml`)); } function readStyle() { - return fs.readFileSync(`${dir}/style.css`, 'utf8'); + return read(`${dir}/style.css`); +} + +export function writePDF(buffer) { + return write(c.filename + ".pdf", buffer) +} + +export function writeICS(ics) { + return write(c.filename + ".ics", ics); } export function readXML() { - return fs.readFileSync(`${dir}/data.xml`, 'utf8'); + return read(`${dir}/data.xml`); } export const c = readConfig(); -c.style = ``; \ No newline at end of file +c.style = ``; diff --git a/src/ics.js b/src/ics.js new file mode 100644 index 0000000..ebf6480 --- /dev/null +++ b/src/ics.js @@ -0,0 +1,62 @@ +import * as ics from "ics" + +import { prepare } from "./main.js" +import { c, writeICS } from "./files.js" + +const daymap = { + "Mo": "MO", + "Di": "TU", + "Mi": "WE", + "Do": "TH", + "Fr": "FR", +} + +let start = new Date(c.vorlesungszeit[0]) +let end = new Date(c.vorlesungszeit[1]) + +function getDateList(date, time) { + let year = date.getFullYear() - 0 + let month = date.getMonth() - 0 + let day = date.getDate() - 0 + + let timelist = time.split(":") + + let hour = timelist[0] - 0 + let minute = timelist[1] - 0 + + return [year, month, day, hour, minute] +} + +function generateICS() { + let tage = prepare(true); + let veranstaltungen = [] + + for (let tag in tage) { + for (let eintrag of tage[tag]) { + let until = end.toISOString().replace(/-|\.|:/g, "").replace("000000000", "000000"); + let day = daymap[tag]; + + if (!eintrag.name.includes("STYLECLASS")) { + veranstaltungen.push({ + title: eintrag.name, + location: eintrag.raum, + start: getDateList(start, eintrag.von), + end: getDateList(start, eintrag.bis), // yes this is correct + recurrenceRule: `FREQ=WEEKLY;INTERVAL=1;BYDAY=${day};UNTIL=${until}` + }) + } + } + + start.setDate(start.getDate() + 1); + } + + const { error, value } = ics.createEvents(veranstaltungen) + + if (error) { + console.log(error) + } else { + writeICS(value) + } +} + +generateICS() diff --git a/src/main.js b/src/main.js index 31183b2..4792319 100644 --- a/src/main.js +++ b/src/main.js @@ -1,22 +1,5 @@ -import * as fs from "fs" -import * as html_to_pdf from "html-pdf-node" - import { c } from './files.js' -let hinweise = "`; - -let date = new Date(); -date = date.toLocaleDateString("de-DE", { - weekday: 'long', - year: 'numeric', - month: 'long', - day: 'numeric', -}); - const ctage = { "Mo": [], "Di": [], @@ -25,24 +8,13 @@ const ctage = { "Fr": [] } -const options = { - format: 'A4', - landscape: true, - margin: { - top: "0.5cm", - right: "1cm", - bottom: "1cm", - left: "2cm", - }, -}; - // returns time difference in 15-minute intervalls function timediff(a, b) { const time = "1970-01-01 "; return (new Date(time + a) - new Date(time + b)) / 1000 / 60 / 15; } -function gettimefromintervalls(x) { +export function gettimefromintervalls(x) { let y = 15 * x / 60 + 8 let z = y.toString().padStart(2, '0'); @@ -127,9 +99,7 @@ function bufferUp(tage) { return tage; } -function main() { - let res = ""; - +export function prepare(forICS = false) { let tage = structuredClone(ctage); let tage_runtimes = structuredClone(ctage); @@ -149,69 +119,12 @@ function main() { } } + if (forICS) { + return tage = sortTage(tage); + } + tage = bufferUp(tage, ctage); tage = sortTage(tage); - tage_runtimes = runtimes(tage, tage_runtimes) - - let timeout = {} - - // 1h has 4 15 minute intervalls: 08 to 19 means 44 intervalls - for (let i = 0; i < 44; i++) { - let mensa = ""; - - if (11 < i && i < 25) mensa = "mensa1"; - if (25 < i && i < 27) mensa = "mensa2"; - - res += `` + gettimefromintervalls(i) - for (let day in tage_runtimes) { - if (timeout[day] > 0) { - timeout[day]--; - continue; - } - - let el = tage_runtimes[day][0]; - - if (el.name.includes("STYLECLASS")) { - let styleclass = el.name.split("/")[1].trim().toLowerCase(); - res += ``; - } else { - let name = el.name == "BUFFER" ? "" : `
${el.name}`; - let raum = el.raum == "BUFFER" ? "" : `${el.raum}`; - - res += `${name}${raum}`; - } - - timeout[day] = el.runtime - 1; - tage_runtimes[day].splice(0, 1); - } - - res += "" - } - - return res + return runtimes(tage, tage_runtimes) } - -let html = ` -${c.style} -

Persönlicher Stundenplan von ${c.student} für das ${c.semester}. Stand: ${date}.

- - - - - - - - - - ${main()} -
UhrzeitMontagDienstagMittwochDonnerstagFreitag
-
-Hinweise: -${hinweise} -`; - -html_to_pdf.generatePdf({content: html}, options).then(pdfBuffer => { - // fs.writeFileSync("test.html", html) - fs.writeFileSync(c.filename, pdfBuffer); -}); diff --git a/src/pdf.js b/src/pdf.js new file mode 100644 index 0000000..a943781 --- /dev/null +++ b/src/pdf.js @@ -0,0 +1,96 @@ +import * as html_to_pdf from "html-pdf-node" + +import { prepare, gettimefromintervalls } from "./main.js" +import { c, writePDF } from './files.js' + +let date = new Date(); +date = date.toLocaleDateString("de-DE", { + weekday: 'long', + year: 'numeric', + month: 'long', + day: 'numeric', +}); + +let hinweise = "`; + +const options = { + format: 'A4', + landscape: true, + margin: { + top: "0.5cm", + right: "1cm", + bottom: "1cm", + left: "2cm", + }, +}; + +function format() { + let tage_runtimes = prepare() + let timeout = {} + let res = ""; + + // 1h has 4 15 minute intervalls: 08 to 19 means 44 intervalls + for (let i = 0; i < 44; i++) { + let mensa = ""; + + if (11 < i && i < 25) mensa = "mensa1"; + if (25 < i && i < 27) mensa = "mensa2"; + + res += `` + gettimefromintervalls(i) + + for (let day in tage_runtimes) { + if (timeout[day] > 0) { + timeout[day]--; + continue; + } + + let el = tage_runtimes[day][0]; + + if (el.name.includes("STYLECLASS")) { + let styleclass = el.name.split("/")[1].trim().toLowerCase(); + res += ``; + } else { + let name = el.name == "BUFFER" ? "" : `
${el.name}`; + let raum = el.raum == "BUFFER" ? "" : `${el.raum}`; + + res += `${name}${raum}`; + } + + timeout[day] = el.runtime - 1; + tage_runtimes[day].splice(0, 1); + } + + res += "" + } + + return res +} + +function generatePDF() { + let html = ` + ${c.style} +

Persönlicher Stundenplan von ${c.student} für das ${c.semester}. Stand: ${date}.

+ + + + + + + + + + ${format()} +
UhrzeitMontagDienstagMittwochDonnerstagFreitag
+
+ Hinweise: + ${hinweise} + `; + + html_to_pdf.generatePdf({content: html}, options).then(pdfBuffer => {writePDF(pdfBuffer)}); +} + +generatePDF() -- cgit v1.2.3