Compare commits
19 Commits
2fb5f48a54
...
fix/docker
| Author | SHA1 | Date | |
|---|---|---|---|
| 8c5d6de5a4 | |||
| 5f735dce6a | |||
| de11a6934c | |||
| 457a49e754 | |||
| 92a2c6956a | |||
| 1bf297752d | |||
| 156a2db485 | |||
| 714738dcba | |||
| 66212229f5 | |||
| 3167bd7976 | |||
| 877d9e37b3 | |||
| fd0081d4d0 | |||
| 966353de3e | |||
| ae9ae46fea | |||
| d8e2027efa | |||
| 272c9519b9 | |||
| 1890d28f47 | |||
| f086fd9792 | |||
| 1cdcf2f423 |
@@ -13,3 +13,6 @@ helm-charts
|
||||
.editorconfig
|
||||
.idea
|
||||
coverage*
|
||||
output*
|
||||
dist
|
||||
data
|
||||
10
.env.sample
Normal file
10
.env.sample
Normal file
@@ -0,0 +1,10 @@
|
||||
ntfy_on=true
|
||||
ntfy_username=chiko
|
||||
ntfy_password=Blub
|
||||
ntfy_host=ntfy.some-service.com
|
||||
ntfy_topic=SomeTopic
|
||||
|
||||
dc_on=true
|
||||
dc_webhook=123123123123123/ABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEF
|
||||
dc_botname=Botname Here
|
||||
dc_avatar_url=https://pico.eix-1.n8x.sx/-tbcjZCuAtj
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,6 +5,7 @@ node_modules
|
||||
output
|
||||
out
|
||||
dist
|
||||
build
|
||||
*.tgz
|
||||
|
||||
# code coverage
|
||||
|
||||
2
Crontab
Normal file
2
Crontab
Normal file
@@ -0,0 +1,2 @@
|
||||
1 * * * * bun run ./src/app.ts --today > /dev/null 2>&1
|
||||
0 * * * * bun run ./src/app.ts > /dev/null 2>&1
|
||||
36
Dockerfile
36
Dockerfile
@@ -1,14 +1,12 @@
|
||||
FROM debian:12 AS base
|
||||
WORKDIR /usr/src/app
|
||||
WORKDIR /opt/app
|
||||
RUN apt-get update && \
|
||||
apt-get install -y curl unzip ca-certificates python3 python3-pip && \
|
||||
# apt-get install -y curl unzip cron ca-certificates python3 python3-pip && \
|
||||
apt-get install -y curl unzip cron ca-certificates python3 python3-pip && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
# install BunJs
|
||||
RUN curl -fsSL https://bun.com/install | bash
|
||||
ENV PATH="/root/.bun/bin:$PATH"
|
||||
# symlink python
|
||||
RUN ln -s /usr/bin/python3 /usr/bin/python
|
||||
|
||||
# install dependencies into temp directory
|
||||
# this will cache them and speed up future builds
|
||||
FROM base AS install
|
||||
@@ -20,24 +18,34 @@ RUN cd /temp/dev && bun install --frozen-lockfile
|
||||
RUN mkdir -p /temp/prod
|
||||
COPY package.json bun.lock /temp/prod/
|
||||
RUN cd /temp/prod && bun install --frozen-lockfile --production
|
||||
# prepare python packages
|
||||
RUN pip3 install -r requirements.txt
|
||||
|
||||
# and install python dependencies
|
||||
# COPY ./requirements.txt .
|
||||
# RUN python3 -m pip install --break-system-packages -r requirements.txt
|
||||
# RUN python3 -m pip install -U python-dotenv
|
||||
|
||||
# copy node_modules from temp directory
|
||||
# then copy all (non-ignored) project files into the image
|
||||
FROM base AS prerelease
|
||||
COPY --from=install /temp/dev/node_modules node_modules
|
||||
COPY . .
|
||||
COPY . ./
|
||||
|
||||
# [optional] tests & build
|
||||
ENV NODE_ENV=production
|
||||
|
||||
# copy production dependencies and source code into final image
|
||||
FROM base AS release
|
||||
WORKDIR /opt/app
|
||||
COPY --from=install /temp/prod/node_modules node_modules
|
||||
COPY --from=prerelease /usr/src/app/index.ts .
|
||||
COPY --from=prerelease /usr/src/app/package.json .
|
||||
|
||||
# run the app
|
||||
USER bun
|
||||
ENTRYPOINT ["./entrypoint.sh"]
|
||||
COPY --from=prerelease /opt/app/src/app.ts .
|
||||
COPY --from=prerelease /opt/app/package.json .
|
||||
#COPY --from=prerelease .entrypoint.sh .
|
||||
COPY Crontab /etc/cron.d/
|
||||
RUN chmod 0644 /etc/cron.d/Crontab
|
||||
COPY . ./
|
||||
# USER bun
|
||||
RUN touch /var/log/cron.log
|
||||
# RUN chmod +x entrypoint.sh
|
||||
# ENTRYPOINT ["./entrypoint.sh"]
|
||||
VOLUME /opt/app/data/db
|
||||
CMD bun run ./src/app.ts --today && cron && tail -f /var/log/cron.log
|
||||
@@ -1,35 +0,0 @@
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
load_dotenv() # Load environment variables from .env file
|
||||
ntfy_username = os.getenv('ntfy_username')
|
||||
ntfy_password = os.getenv('ntfy_password')
|
||||
ntfy_host = os.getenv('ntfy_host')
|
||||
ntfy_topic = os.getenv('ntfy_topic')
|
||||
dc_webhook = os.getenv('dc_webhook')
|
||||
|
||||
from argparse import ArgumentParser
|
||||
import apprise
|
||||
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument("--title")
|
||||
parser.add_argument("--body")
|
||||
parser.add_argument("--click")
|
||||
args = parser.parse_args()
|
||||
print(args)
|
||||
|
||||
apobj = apprise.Apprise()
|
||||
# config = apprise.AppriseConfig()
|
||||
# config.add('https://myserver:8080/path/to/config')
|
||||
if ntfy_host and ntfy_topic:
|
||||
ntfy_link = f"ntfys://{ntfy_username}:{ntfy_password}@{ntfy_host}/{ntfy_topic}"
|
||||
if args.click:
|
||||
ntfy_link = ntfy_link + "?click=" + args.click
|
||||
apobj.add(ntfy_link)
|
||||
if dc_webhook:
|
||||
apobj.add(f"https://discord.com/api/webhooks/{dc_webhook}");
|
||||
|
||||
apobj.notify(
|
||||
body=args.body,
|
||||
title=args.title
|
||||
)
|
||||
@@ -1,14 +0,0 @@
|
||||
import * as Bun from "bun";
|
||||
|
||||
export function sendNotification(title: string, body: string, click?: string) {
|
||||
const command = [
|
||||
"python",
|
||||
"./app/notification.py",
|
||||
`--title=${title}`,
|
||||
`--body=${body}`,
|
||||
];
|
||||
if (click) {
|
||||
command.push(`--click=${click}`);
|
||||
}
|
||||
Bun.spawn(command);
|
||||
}
|
||||
36
docker-compose.yml
Normal file
36
docker-compose.yml
Normal file
@@ -0,0 +1,36 @@
|
||||
services:
|
||||
app:
|
||||
build: .
|
||||
volumes:
|
||||
- ./data/db:/opt/app/data/db
|
||||
env_file:
|
||||
- path: ./.env
|
||||
required: true
|
||||
depends_on:
|
||||
apprise:
|
||||
condition: service_healthy
|
||||
links:
|
||||
- apprise
|
||||
apprise:
|
||||
image: caronc/apprise:latest
|
||||
hostname: apprise
|
||||
environment:
|
||||
- APPRISE_WORKER_COUNT=1
|
||||
- APPRISE_STATEFUL_MODE=simple
|
||||
# - PUID=$(id -u)
|
||||
# - PGID=$(id -g)
|
||||
volumes:
|
||||
- ./data/apprise/config:/config
|
||||
- ./data/apprise/plugin:/plugin
|
||||
- ./data/apprise/attach:/attach
|
||||
ports:
|
||||
- 8000:8000
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://localhost:8000/status"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 5
|
||||
# networks:
|
||||
# default:
|
||||
# external: true
|
||||
# name: npm
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
crontab -l > mycron
|
||||
echo "0 8 * * * bun run ./app/app.ts --today > /dev/null 2>&1" >> mycron
|
||||
echo "0 * * * * bun run ./app/app.ts --all > /dev/null 2>&1" >> mycron
|
||||
echo "0 8 * * * bun run ./src/app.ts --today > /dev/null 2>&1" >> mycron
|
||||
echo "0 * * * * bun run ./src/app.ts > /dev/null 2>&1" >> mycron
|
||||
crontab mycron
|
||||
rm mycron
|
||||
12
package.json
12
package.json
@@ -1,16 +1,20 @@
|
||||
{
|
||||
"version": "0.1.0",
|
||||
"name": "eventcalender",
|
||||
"module": "index.ts",
|
||||
"module": "./src/app.ts",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"@types/bun": "^1.3.0"
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "bun run ./app/app.ts",
|
||||
"dev:init": "bun run ./app/app.ts --init",
|
||||
"dev": "bun run ./src/app.ts",
|
||||
"dev:init": "bun run ./src/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 --compile --minify --sourcemap ./src/app.ts --outfile ./build/77th_event_calendar_notification",
|
||||
"build:linux": "bun build --compile --minify --sourcemap --target=bun-linux-arm64 ./src/app.ts --outfile ./build/77th_event_calendar_notification",
|
||||
"docker:build": "docker build -t chiko/77th_eventcalendarntfy:0.1.0 ."
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5"
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
apprise
|
||||
python-dotenv
|
||||
@@ -1,4 +1,4 @@
|
||||
import * as db from "../app/sql";
|
||||
import * as db from "../src/sql";
|
||||
|
||||
const query = db.db.query("DELETE FROM events;");
|
||||
query.run();
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Event } from "../app/component/event/events";
|
||||
import * as db from "../app/sql";
|
||||
import { Event } from "../src/component/event/events";
|
||||
import * as db from "../src/sql";
|
||||
import { Database } from "bun:sqlite";
|
||||
|
||||
export function init ( db: Database ) {
|
||||
|
||||
@@ -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}`,
|
||||
@@ -106,43 +112,30 @@ async function main( ) {
|
||||
}
|
||||
return false;
|
||||
})( ev );
|
||||
sendNotification(
|
||||
`${today_prefix ? "TODAY " : ""}${notification_prefix ? notification_prefix + ": " : ""} ${ev.title} (${ TEventType[ ev.event_type ] })`,
|
||||
`${body}`,
|
||||
`${ev.link || "https://77th-jsoc.com/#/events"}`
|
||||
);
|
||||
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"}`
|
||||
// );
|
||||
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 )
|
||||
@@ -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;
|
||||
})()
|
||||
41
src/notification.py
Normal file
41
src/notification.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
def main():
|
||||
load_dotenv() # Load environment variables from .env file
|
||||
ntfy_username = os.getenv('ntfy_username')
|
||||
ntfy_password = os.getenv('ntfy_password')
|
||||
ntfy_host = os.getenv('ntfy_host')
|
||||
ntfy_topic = os.getenv('ntfy_topic')
|
||||
dc_webhook = os.getenv('dc_webhook')
|
||||
dc_botname = os.getenv('dc_botname')
|
||||
dc_avatar_url = os.getenv('dc_avatar_url')
|
||||
|
||||
from argparse import ArgumentParser
|
||||
import apprise
|
||||
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument("--title")
|
||||
parser.add_argument("--body")
|
||||
parser.add_argument("--click")
|
||||
args = parser.parse_args()
|
||||
print(args)
|
||||
|
||||
apobj = apprise.Apprise()
|
||||
# config = apprise.AppriseConfig()
|
||||
# config.add('https://myserver:8080/path/to/config')
|
||||
if ntfy_host and ntfy_topic:
|
||||
ntfy_link = f"ntfys://{ntfy_username}:{ntfy_password}@{ntfy_host}/{ntfy_topic}"
|
||||
if args.click:
|
||||
ntfy_link = ntfy_link + "?click=" + args.click
|
||||
apobj.add(ntfy_link)
|
||||
if dc_webhook:
|
||||
apobj.add(f"discord://{dc_webhook}?avatar_url={dc_avatar_url}&botname={dc_botname}");
|
||||
|
||||
apobj.notify(
|
||||
body=args.body,
|
||||
title=args.title
|
||||
)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
16
src/sendNotification.ts
Normal file
16
src/sendNotification.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import * as Bun from "bun";
|
||||
|
||||
export async function sendNotification(title: string, body: string, click?: string | null) {
|
||||
const command = [
|
||||
"python3",
|
||||
"./src/notification.py",
|
||||
`--title=${title}`,
|
||||
`--body=${body}`,
|
||||
];
|
||||
if ( click ) {
|
||||
command.push(`--click=${click}`);
|
||||
}
|
||||
const proc = Bun.spawn(command);
|
||||
const text = await proc.stdout.text();
|
||||
console.log("sendNotification: " + text);
|
||||
}
|
||||
@@ -11,3 +11,5 @@ export const db = new Database(db_filepath);
|
||||
export function init () {
|
||||
Event.createTable(db);
|
||||
}
|
||||
|
||||
init();
|
||||
@@ -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>[] {
|
||||
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");
|
||||
}
|
||||
Reference in New Issue
Block a user