From f086fd97924645dd1cc7a34f98b7010fe2df0db6 Mon Sep 17 00:00:00 2001 From: chiko Date: Mon, 20 Oct 2025 02:58:16 +0200 Subject: [PATCH] added --today parameter to send a nofitication for todays events else its change or new events. also fetches current and next month now. --- .gitignore | 1 + app/app.ts | 69 +++++++++++------------------------ app/component/event/events.ts | 21 ++++++++--- app/sendNotification.ts | 2 +- app/util.ts | 7 ++++ package.json | 3 +- 6 files changed, 49 insertions(+), 54 deletions(-) diff --git a/.gitignore b/.gitignore index 9d2a127..398a986 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ node_modules output out dist +build *.tgz # code coverage diff --git a/app/app.ts b/app/app.ts index 6b9ccb7..70c6df5 100644 --- a/app/app.ts +++ b/app/app.ts @@ -1,20 +1,13 @@ import { TEventType } from "./component/event/event.types"; import { db } from "./sql"; -import { Event, type TEventEntityNew } from "./component/event/events"; +import { Event, type TEventEntityNew, type TGetEventsOptions } from "./component/event/events"; import { sendNotification } from "./sendNotification"; -import { createPlaceholders } from "./util"; +import { createPlaceholders, pad_l2 } from "./util"; const argv = require('minimist')(process.argv.slice(2)); console.dir(argv) -const TS_TODAY = new Date(); - -function pad_l2 ( _thing: string | number ): string { - if ( typeof _thing == "number" ) { - _thing = JSON.stringify(_thing); - }; - return _thing.padStart(2, "0"); -} +// const TS_TODAY = new Date(); function getTsNow() { const now = new Date(); @@ -29,8 +22,10 @@ function getTsNow() { } async function main( ) { - const events = await Event.fetch_events( TS_TODAY.getFullYear(), TS_TODAY.getMonth() + 1 , -120 ); - + const TODAY = getTsNow(); + const events_currentMonth = await Event.fetch_events( TODAY.year, TODAY.month , -120 ); + const events_nextMonth = await Event.fetch_events( TODAY.year, TODAY.month + 1 , -120 ); + const events = [...events_currentMonth, ...events_nextMonth]; // 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()}`; @@ -70,7 +65,18 @@ async function main( ) { } Event.insert( eventsToInsert, db); - const list_of_events = Event.get_events(["new", "changed"], db); + const options: TGetEventsOptions = { + } + if (argv.today) { + options.date = { + year: TODAY.year, + month: TODAY.month, + day: TODAY.day + } + } else { + options.notification = ["new", "changed"] + } + const list_of_events = Event.get_events( options, db ); for ( const ev of list_of_events ) { const body = [ `Title: ${ev.title}`, @@ -108,41 +114,10 @@ async function main( ) { })( ev ); sendNotification( `${today_prefix ? "TODAY " : ""}${notification_prefix ? notification_prefix + ": " : ""} ${ev.title} (${ TEventType[ ev.event_type ] })`, - `${body}`, - `${ev.link || "https://77th-jsoc.com/#/events"}` + `${body}` + // `${ev.link || "https://77th-jsoc.com/#/events"}` ); ev.set_notification("done", db); } - // events.forEach( event => { - // const now = getTsNow(); - // const [year, month, day] = event.date_at.split("-") - // if ( - // year == String(now.year) && - // month == pad_l2( String(now.month) ) && - // day == pad_l2( String( now.day ) ) - // ) { - // // console.dir( event ); - // const body = [ - // `Title: ${event.title}`, - // `Location: ${event.location}`, - // `Type: ${ TEventType[ event.event_type ] }`, - // `Date: ${event.date_at}`, - // `Time: ${event.time_start}`, - // `By: ${event.posted_by}`, - // `Link: ${event.link}`, - // ].join("\n"); - - // sendNotification( - // `TODAY ${ TEventType[ event.event_type ] } - ${event.title}`, - // `${body}`, - // `${event.link || "https://77th-jsoc.com/#/events"}` - // ); - // } - // }); }; -main(); -// do { -// await getEvents(TS_TODAY.getFullYear(), TS_TODAY.getMonth() + 1 , -120); -// await Bun.sleep(1000 * 60 * 60 * 24); -// } -// while( true ) +main(); \ No newline at end of file diff --git a/app/component/event/events.ts b/app/component/event/events.ts index 65e6b59..c39958d 100644 --- a/app/component/event/events.ts +++ b/app/component/event/events.ts @@ -4,6 +4,14 @@ import { transformArray } from "../../util"; const BASE_URL = "https://77th-jsoc.com/service.php?action=get_events"; +export type TGetEventsOptions = { + notification?: TEventEntity["notification"][] | null, + date?: { + year: number, + month: number, + day: number + } +} export type TEventEntity = TEvent & { event_uid: number notification: "new" | "changed" | "deleted" | "done" @@ -45,7 +53,7 @@ export class Event implements TEventEntity { console.log(`Inserted ${count} events`); } - static async fetch_events( _year_: number, _month_: number, timezone: number) { + static async fetch_events( _year_: number, _month_: number, timezone: number): Promise { const url = `${BASE_URL}&year=${_year_}&month=${_month_}&timezone=${timezone}` const response = await fetch(url, { method: "GET", @@ -55,15 +63,18 @@ export class Event implements TEventEntity { return events; } - static get_events (notification: TEventEntity["notification"][] | null, db: Database ) { + static get_events (options: TGetEventsOptions, db: Database ) { const whereConditions: string[] = []; - if ( notification ) { - whereConditions.push( `notification IN ('${ notification.join("', '") }')` ) + if ( options.notification ) { + whereConditions.push( `notification IN ('${ options.notification.join("', '") }')` ) + } + if (options.date) { + whereConditions.push(`date_at = "${options.date.year}-${options.date.month}-${options.date.day}"`); } const where = ( () => { let str = "WHERE "; if ( whereConditions.length >= 1 ) { - str += whereConditions.join(" AND "); + str += whereConditions.join(" OR "); } return str; })() diff --git a/app/sendNotification.ts b/app/sendNotification.ts index 86d3777..cb0a754 100644 --- a/app/sendNotification.ts +++ b/app/sendNotification.ts @@ -1,6 +1,6 @@ import * as Bun from "bun"; -export function sendNotification(title: string, body: string, click?: string) { +export function sendNotification(title: string, body: string, click?: string | null) { const command = [ "python", "./app/notification.py", diff --git a/app/util.ts b/app/util.ts index b2f0b12..7d3314a 100644 --- a/app/util.ts +++ b/app/util.ts @@ -19,4 +19,11 @@ export function prefixKeysWithDollar>(obj: T): Add export function transformArray>(arr: T[]): AddDollarPrefix[] { return arr.map(prefixKeysWithDollar); +} + +export function pad_l2 ( _thing: string | number ): string { + if ( typeof _thing == "number" ) { + _thing = JSON.stringify(_thing); + }; + return _thing.padStart(2, "0"); } \ No newline at end of file diff --git a/package.json b/package.json index e48c0b5..23a04eb 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "dev": "bun run ./app/app.ts", "dev:init": "bun run ./app/app.ts --init", "db:init": "bun run ./run/db_init.ts", - "db:deleteall": "bun run ./run/db_deleteall.ts" + "db:deleteall": "bun run ./run/db_deleteall.ts", + "build": "bun build ./app/app.ts --compile --outfile ./build/77th_event_calendar_notification" }, "peerDependencies": { "typescript": "^5"