Last active
July 31, 2022 19:14
-
-
Save carlfriess/2a62c69147f21a486b33d240b89ac1c1 to your computer and use it in GitHub Desktop.
Sends countdown notifications via Pushover (usage example: `./pushover-countdown.js ./z-events.json`).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const https = require("https"); | |
const config = require(process.argv[2]); | |
function durationString(target) { | |
const msPerMinute = 60 * 1000; | |
const msPerHour = msPerMinute * 60; | |
const msPerDay = msPerHour * 24; | |
const msPerWeek = msPerDay * 7; | |
const msPerMonth = msPerDay * 31; | |
const msPerYear = msPerDay * 365; | |
const duration = target - (new Date()).getTime(); | |
if (duration < 0) { | |
return null; | |
} else if (duration < msPerWeek) { | |
const days = Math.ceil(duration / msPerDay); | |
return `${days} day${days > 1 ? "s" : ""}`; | |
} else if (duration < 2 * msPerMonth) { | |
const weeks = Math.floor(duration / msPerWeek); | |
const days = Math.ceil((duration % msPerWeek) / msPerDay); | |
return `${weeks} week${weeks > 1 ? "s" : ""} ${days} day${days > 1 ? "s" : ""}`; | |
} else { | |
const months = Math.floor(duration / msPerMonth); | |
const days = Math.ceil((duration % msPerMonth) / msPerDay); | |
return `${months} month${months > 1 ? "s" : ""} ${days} day${days > 1 ? "s" : ""}`; | |
} | |
} | |
function notification(token, user, title, message) { | |
const req = https.request({ | |
"method": "POST", | |
"hostname": "api.pushover.net", | |
"path": "/1/messages.json", | |
"headers": { | |
"Content-Type": "application/json", | |
}, | |
}, function (res) { | |
res.on("error", function (error) { | |
console.error(error); | |
}); | |
}); | |
req.write(JSON.stringify({ | |
"token": token, | |
"user": user, | |
"title": title, | |
"message": message, | |
})); | |
req.end(); | |
} | |
for (const [name, date] of Object.entries(config.events)) { | |
const msg = durationString(new Date(date).getTime()); | |
if (msg) { | |
notification(config.token, config.user, name, msg); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"token": "******************************", | |
"user": "******************************", | |
"events": { | |
"Christmas": "2022-12-25", | |
"New Year's": "2023-01-01" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment