Compare commits
2 Commits
2fb5f48a54
...
f086fd9792
| Author | SHA1 | Date | |
|---|---|---|---|
| f086fd9792 | |||
| 1cdcf2f423 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,6 +5,7 @@ node_modules
|
|||||||
output
|
output
|
||||||
out
|
out
|
||||||
dist
|
dist
|
||||||
|
build
|
||||||
*.tgz
|
*.tgz
|
||||||
|
|
||||||
# code coverage
|
# code coverage
|
||||||
|
|||||||
67
app/app.ts
67
app/app.ts
@@ -1,20 +1,13 @@
|
|||||||
import { TEventType } from "./component/event/event.types";
|
import { TEventType } from "./component/event/event.types";
|
||||||
import { db } from "./sql";
|
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 { sendNotification } from "./sendNotification";
|
||||||
import { createPlaceholders } from "./util";
|
import { createPlaceholders, pad_l2 } from "./util";
|
||||||
|
|
||||||
const argv = require('minimist')(process.argv.slice(2));
|
const argv = require('minimist')(process.argv.slice(2));
|
||||||
console.dir(argv)
|
console.dir(argv)
|
||||||
|
|
||||||
const TS_TODAY = new Date();
|
// 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");
|
|
||||||
}
|
|
||||||
|
|
||||||
function getTsNow() {
|
function getTsNow() {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
@@ -29,8 +22,10 @@ function getTsNow() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function main( ) {
|
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
|
// Write to JSON File Section START
|
||||||
// const data = JSON.stringify(events, null, 2);
|
// 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()}`;
|
// 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);
|
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 ) {
|
for ( const ev of list_of_events ) {
|
||||||
const body = [
|
const body = [
|
||||||
`Title: ${ev.title}`,
|
`Title: ${ev.title}`,
|
||||||
@@ -108,41 +114,10 @@ async function main( ) {
|
|||||||
})( ev );
|
})( ev );
|
||||||
sendNotification(
|
sendNotification(
|
||||||
`${today_prefix ? "TODAY " : ""}${notification_prefix ? notification_prefix + ": " : ""} ${ev.title} (${ TEventType[ ev.event_type ] })`,
|
`${today_prefix ? "TODAY " : ""}${notification_prefix ? notification_prefix + ": " : ""} ${ev.title} (${ TEventType[ ev.event_type ] })`,
|
||||||
`${body}`,
|
`${body}`
|
||||||
`${ev.link || "https://77th-jsoc.com/#/events"}`
|
// `${ev.link || "https://77th-jsoc.com/#/events"}`
|
||||||
);
|
);
|
||||||
ev.set_notification("done", db);
|
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();
|
main();
|
||||||
// do {
|
|
||||||
// await getEvents(TS_TODAY.getFullYear(), TS_TODAY.getMonth() + 1 , -120);
|
|
||||||
// await Bun.sleep(1000 * 60 * 60 * 24);
|
|
||||||
// }
|
|
||||||
// while( true )
|
|
||||||
|
|||||||
@@ -4,6 +4,14 @@ import { transformArray } from "../../util";
|
|||||||
|
|
||||||
const BASE_URL = "https://77th-jsoc.com/service.php?action=get_events";
|
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 & {
|
export type TEventEntity = TEvent & {
|
||||||
event_uid: number
|
event_uid: number
|
||||||
notification: "new" | "changed" | "deleted" | "done"
|
notification: "new" | "changed" | "deleted" | "done"
|
||||||
@@ -45,7 +53,7 @@ export class Event implements TEventEntity {
|
|||||||
console.log(`Inserted ${count} events`);
|
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 url = `${BASE_URL}&year=${_year_}&month=${_month_}&timezone=${timezone}`
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
@@ -55,15 +63,18 @@ export class Event implements TEventEntity {
|
|||||||
return events;
|
return events;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get_events (notification: TEventEntity["notification"][] | null, db: Database ) {
|
static get_events (options: TGetEventsOptions, db: Database ) {
|
||||||
const whereConditions: string[] = [];
|
const whereConditions: string[] = [];
|
||||||
if ( notification ) {
|
if ( options.notification ) {
|
||||||
whereConditions.push( `notification IN ('${ notification.join("', '") }')` )
|
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 = ( () => {
|
const where = ( () => {
|
||||||
let str = "WHERE ";
|
let str = "WHERE ";
|
||||||
if ( whereConditions.length >= 1 ) {
|
if ( whereConditions.length >= 1 ) {
|
||||||
str += whereConditions.join(" AND ");
|
str += whereConditions.join(" OR ");
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
})()
|
})()
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ ntfy_password = os.getenv('ntfy_password')
|
|||||||
ntfy_host = os.getenv('ntfy_host')
|
ntfy_host = os.getenv('ntfy_host')
|
||||||
ntfy_topic = os.getenv('ntfy_topic')
|
ntfy_topic = os.getenv('ntfy_topic')
|
||||||
dc_webhook = os.getenv('dc_webhook')
|
dc_webhook = os.getenv('dc_webhook')
|
||||||
|
dc_botname = os.getenv('dc_botname')
|
||||||
|
dc_avatar_url = os.getenv('dc_avatar_url')
|
||||||
|
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
import apprise
|
import apprise
|
||||||
@@ -27,7 +29,7 @@ if ntfy_host and ntfy_topic:
|
|||||||
ntfy_link = ntfy_link + "?click=" + args.click
|
ntfy_link = ntfy_link + "?click=" + args.click
|
||||||
apobj.add(ntfy_link)
|
apobj.add(ntfy_link)
|
||||||
if dc_webhook:
|
if dc_webhook:
|
||||||
apobj.add(f"https://discord.com/api/webhooks/{dc_webhook}");
|
apobj.add(f"discord://{dc_webhook}?avatar_url={dc_avatar_url}&botname={dc_botname}");
|
||||||
|
|
||||||
apobj.notify(
|
apobj.notify(
|
||||||
body=args.body,
|
body=args.body,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import * as Bun from "bun";
|
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 = [
|
const command = [
|
||||||
"python",
|
"python",
|
||||||
"./app/notification.py",
|
"./app/notification.py",
|
||||||
|
|||||||
@@ -20,3 +20,10 @@ export function prefixKeysWithDollar<T extends Record<string, any>>(obj: T): Add
|
|||||||
export function transformArray<T extends Record<string, any>>(arr: T[]): AddDollarPrefix<T>[] {
|
export function transformArray<T extends Record<string, any>>(arr: T[]): AddDollarPrefix<T>[] {
|
||||||
return arr.map(prefixKeysWithDollar);
|
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");
|
||||||
|
}
|
||||||
@@ -10,7 +10,8 @@
|
|||||||
"dev": "bun run ./app/app.ts",
|
"dev": "bun run ./app/app.ts",
|
||||||
"dev:init": "bun run ./app/app.ts --init",
|
"dev:init": "bun run ./app/app.ts --init",
|
||||||
"db:init": "bun run ./run/db_init.ts",
|
"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": {
|
"peerDependencies": {
|
||||||
"typescript": "^5"
|
"typescript": "^5"
|
||||||
|
|||||||
Reference in New Issue
Block a user