added --today parameter to send a nofitication for todays events else its change or new events. also fetches current and next month now.

This commit is contained in:
2025-10-20 02:58:16 +02:00
parent 1cdcf2f423
commit f086fd9792
6 changed files with 49 additions and 54 deletions

1
.gitignore vendored
View File

@@ -5,6 +5,7 @@ node_modules
output
out
dist
build
*.tgz
# code coverage

View File

@@ -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();

View File

@@ -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<TEvent[]> {
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;
})()

View File

@@ -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",

View File

@@ -19,4 +19,11 @@ export function prefixKeysWithDollar<T extends Record<string, any>>(obj: T): Add
export function transformArray<T extends Record<string, any>>(arr: T[]): AddDollarPrefix<T>[] {
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");
}

View File

@@ -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"