removed Python and the Python Script, added the apprise-api docker container to the Docker-compose to send notificaiton
This commit is contained in:
79
src/app.ts
79
src/app.ts
@@ -1,31 +1,25 @@
|
||||
import { TEventType } from "./component/event/event.types";
|
||||
import { db } from "./sql";
|
||||
import { Event, type TEventEntityNew, type TGetEventsOptions } from "./component/event/events";
|
||||
import { createPlaceholders, getTsNow, pad_l2 } from "./util";
|
||||
import { sendNotification } from "./sendNotification";
|
||||
import { createPlaceholders, pad_l2 } from "./util";
|
||||
|
||||
const argv = require('minimist')(process.argv.slice(2));
|
||||
console.dir(argv)
|
||||
console.log("App started");
|
||||
console.dir({argv})
|
||||
|
||||
// const TS_TODAY = new Date();
|
||||
|
||||
function getTsNow() {
|
||||
const now = new Date();
|
||||
const rtn = {
|
||||
year: now.getFullYear(),
|
||||
month: now.getMonth() + 1,
|
||||
day: now.getDate(),
|
||||
minute: now.getMinutes(),
|
||||
seconds: now.getSeconds()
|
||||
}
|
||||
return rtn;
|
||||
}
|
||||
|
||||
async function main( ) {
|
||||
async function main ( ) {
|
||||
console.log("Excecuting main()");
|
||||
const TODAY = getTsNow();
|
||||
console.dir(TODAY);
|
||||
const events_currentMonth = await Event.fetch_events( TODAY.year, TODAY.month , -120 );
|
||||
console.log("events_currentMonth.length:" + events_currentMonth.length );
|
||||
const events_nextMonth = await Event.fetch_events( TODAY.year, TODAY.month + 1 , -120 );
|
||||
console.log("events_nextMonth.length:" + events_nextMonth.length );
|
||||
const events = [...events_currentMonth, ...events_nextMonth];
|
||||
console.log("events.length:" + events.length );
|
||||
|
||||
// const TS_TODAY = new Date();
|
||||
// Write to JSON File Section START
|
||||
// const data = JSON.stringify(events, null, 2);
|
||||
// const TS = `${TS_TODAY.getFullYear()}-${TS_TODAY.getMonth() + 1}-${TS_TODAY.getDate()}_${TS_TODAY.getHours()}-${TS_TODAY.getMinutes()}-${TS_TODAY.getSeconds()}`;
|
||||
@@ -33,16 +27,19 @@ async function main( ) {
|
||||
// Write to JSON File Section END
|
||||
|
||||
const allEventUids = events.map( event => { return event.uid; });
|
||||
console.dir(allEventUids );
|
||||
const placeholders = createPlaceholders( allEventUids );
|
||||
const getAllRelevantEventsQuery = db.query(
|
||||
`SELECT * FROM events WHERE uid IN (${placeholders}); `
|
||||
).as(Event );
|
||||
const AllRelevantEvents = getAllRelevantEventsQuery.all(...allEventUids);
|
||||
|
||||
console.log("AllRelevantEvents.length:" + AllRelevantEvents.length );
|
||||
const eventsToInsert: TEventEntityNew[] = [];
|
||||
for ( const ev of events ) {
|
||||
console.log("loop ev: " + [ ev.uid, ev.title, ev.date_at ].join( ", " ) );
|
||||
const found = AllRelevantEvents.find(event => event.uid === ev.uid);
|
||||
if ( found ) {
|
||||
console.log("loop ev found: " + [ found.uid, found.title, found.date_at ].join( ", " ) );
|
||||
if (
|
||||
found.title != ev.title ||
|
||||
found.description != ev.description ||
|
||||
@@ -55,29 +52,34 @@ async function main( ) {
|
||||
found.timezone != ev.timezone ||
|
||||
found.link != ev.link
|
||||
) {
|
||||
console.log("loop ev different (changed): " + [ ev.uid, ev.title, ev.date_at ].join( ", " ) );
|
||||
const newEventToInsert: TEventEntityNew = {... ev, notification: "changed"};
|
||||
eventsToInsert.push( newEventToInsert );
|
||||
}
|
||||
} else {
|
||||
console.log("loop ev added (new): " + [ ev.uid, ev.title, ev.date_at ].join( ", " ) );
|
||||
const newEventToInsert: TEventEntityNew = {... ev, notification: "new"};
|
||||
eventsToInsert.push( newEventToInsert );
|
||||
}
|
||||
}
|
||||
|
||||
console.dir(eventsToInsert)
|
||||
Event.insert( eventsToInsert, db);
|
||||
const options: TGetEventsOptions = {
|
||||
}
|
||||
if (argv.today) {
|
||||
options.date = {
|
||||
const where: TGetEventsOptions = {}
|
||||
where.notification = ["new", "changed"]
|
||||
if ( argv.today ) {
|
||||
where.date = {
|
||||
year: TODAY.year,
|
||||
month: TODAY.month,
|
||||
day: TODAY.day
|
||||
}
|
||||
} else {
|
||||
options.notification = ["new", "changed"]
|
||||
}
|
||||
const list_of_events = Event.get_events( options, db );
|
||||
const list_of_events = Event.get_events( where, db );
|
||||
console.dir({
|
||||
list_of_events,
|
||||
where
|
||||
});
|
||||
for ( const ev of list_of_events ) {
|
||||
console.log("loop list_of_events - ev: " + [ev.uid, ev.title, ev.date_at, "notification:" + ev.notification].join( ", " ) );
|
||||
const body = [
|
||||
`Title: ${ev.title}`,
|
||||
`Location: ${ev.location}`,
|
||||
@@ -87,6 +89,7 @@ async function main( ) {
|
||||
`By: ${ev.posted_by}`,
|
||||
`Link: ${ev.link}`,
|
||||
].join("\n");
|
||||
console.log("loop list_of_events - ev 'body': " + body );
|
||||
const notification_prefix = ( (event: Event) => {
|
||||
switch( event.notification) {
|
||||
case "new":
|
||||
@@ -113,28 +116,8 @@ async function main( ) {
|
||||
return false;
|
||||
})( ev );
|
||||
const title = `${today_prefix ? "TODAY " : ""}${notification_prefix ? notification_prefix + ": " : ""} ${ev.title} (${ TEventType[ ev.event_type ] })`;
|
||||
|
||||
await fetch("http://apprise:8000/notify", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({
|
||||
urls: [
|
||||
`ntfys://${process.env.ntfy_username}:${process.env.ntfy_password}@${process.env.ntfy_host}/${process.env.ntfy_topic}${ ev.link ? `?click=${ev.link}`: "?click=https://77th-jsoc.com/#/events" }`,
|
||||
`discord://${process.env.dc_webhook}?avatar_url=${process.env.dc_avatar_url}&botname=${process.env.dc_botname}`
|
||||
].join(","),
|
||||
title: title,
|
||||
body: body,
|
||||
format: "text"
|
||||
})
|
||||
});
|
||||
|
||||
// await sendNotification(
|
||||
// title,
|
||||
// body
|
||||
// // `${ev.link || "https://77th-jsoc.com/#/events"}`
|
||||
// );
|
||||
console.log("loop list_of_events - ev 'title': " + title );
|
||||
await sendNotification( title, body, ev.link ? ev.link : null);
|
||||
ev.set_notification("done", db);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user