const axios = require('axios'); const pronote = require('pronote-api'); const config = require('../includes/app.json'); const url = config.pronote.url; const username = config.pronote.user; const password = config.pronote.password; async function getTimetable() { const session = await pronote.login(url, username, password); const timetable = await session.timetable(); let nextClass = timetable.filter(i => !i.isCancelled)[0]; return nextClass ? nextClass["from"] : null; } async function notification(title, content, priority) { await fetch("https://" + config.ntfy.server, { method: "POST", body: JSON.stringify({ topic: config.ntfy.topic, message: content, title: title, tags: [ "alarm" ], priority: priority }), headers: { "Authorization": "Basic " + Buffer.from(config.ntfy.user + ":" + config.ntfy.password).toString("base64"), "Content-Type": "application/json" } }) } global.wakeUpTime = null; global.wakeUpTimeDiff = null; global.sleepTime = null; global.sleepTimeDiff = null; global.sleepClockR = null; global.sleepClockC = null; global.wakeUpClockC = null; global.wakeUpClockR = null; async function nightWarning() { if (global.sleepTimeDiff) { await notification("🌙 Good evening!", "You are going to sleep " + global.sleepTimeDiff + ", " + global.sleepClockC + " BST (" + global.sleepClockR + " CEST) so you can wake up " + global.wakeUpTimeDiff + ", " + global.wakeUpClockC + " BST (" + global.wakeUpClockR + " CEST). You will be reminded when it's time to go to sleep.", 3); } else { await notification("🌙 Good evening!", "You don't need to wake up at a specific time tomorrow, enjoy!", 3); } } async function nightReminder() { if (global.sleepTimeDiff) { await notification("🌙 Now let's sleep!", "You should get ready to sleep now so you can wake up " + global.wakeUpTimeDiff + ", " + global.wakeUpClockC + " BST (" + global.wakeUpClockR + " CEST).", 3); } } function digits(input) { if (input < 10) { return "0" + input; } else { return input; } } async function updateSleepTime() { let classTime = await getTimetable(); if (classTime) { global.wakeUpTime = new Date(classTime.getTime() - 5100000); global.sleepTime = new Date(global.wakeUpTime.getTime() - 27000000); let diff = (wakeUpTime - new Date().getTime()) / 1000; let diffHours = Math.floor(diff / 3600); let diffMinutes = Math.floor((diff - diffHours * 3600) / 60); global.wakeUpTimeDiff = "in " + diffHours + " hour" + (diffHours > 1 ? "s" : "") + (diffMinutes > 0 ? " and " + diffMinutes + " minute" + (diffMinutes > 1 ? "s" : "") : ""); let diff2 = (sleepTime - new Date().getTime()) / 1000; let diffHours2 = Math.floor(diff2 / 3600); let diffMinutes2 = Math.floor((diff2 - diffHours2 * 3600) / 60); global.sleepTimeDiff = "in " + diffHours2 + " hour" + (diffHours2 > 1 ? "s" : "") + (diffMinutes2 > 0 ? " and " + diffMinutes2 + " minute" + (diffMinutes2 > 1 ? "s" : "") : ""); global.sleepClockR = (sleepTime.getUTCHours() + 2) + ":" + digits(sleepTime.getUTCMinutes()); global.sleepClockC = (sleepTime.getUTCHours() + 1) + ":" + digits(sleepTime.getUTCMinutes()); global.wakeUpClockR = (wakeUpTime.getUTCHours() + 2) + ":" + digits(wakeUpTime.getUTCMinutes()); global.wakeUpClockC = (wakeUpTime.getUTCHours() + 1) + ":" + digits(wakeUpTime.getUTCMinutes()); } else { global.wakeUpTime = null; global.wakeUpTimeDiff = null; global.sleepTime = null; global.sleepTimeDiff = null; } console.log(global.wakeUpTime, "Waking up " + global.wakeUpTimeDiff + " (" + global.wakeUpClockR + "/" + global.wakeUpClockC + "), sleeping " + global.sleepTimeDiff + " (" + global.sleepClockR + "/" + global.sleepClockC + ")"); } (async () => { //await notification("Test notification", "Testing notification latency, you can safely ignore this notification.", 3); setInterval(async () => { await updateSleepTime(); }, 3600000); await updateSleepTime(); await nightReminder(); })()