diff --git a/bun.lockb b/bun.lockb index 0a73e01..8b0e86d 100644 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 5ff5a98..d71c938 100644 --- a/package.json +++ b/package.json @@ -14,14 +14,17 @@ "axios": "^0.26.0", "body-parser": "^1.20.2", "cheerio": "^1.0.0-rc.10", + "dayjs": "^1.11.10", "discord.js": "^12.5.1", "dotenv": "^8.2.0", "elysia": "^0.7.15", + "lodash-es": "^4.17.21", "minimatch": "^9.0.3", "pino": "^8.15.4", "typescript": "^4.1.5" }, "devDependencies": { + "@types/lodash-es": "^4.17.9", "@types/node": "^20.8.2", "@typescript-eslint/eslint-plugin": "^6.7.4", "@typescript-eslint/parser": "^6.7.4", diff --git a/process-env.d.ts b/process-env.d.ts index f2489d7..7798f53 100644 --- a/process-env.d.ts +++ b/process-env.d.ts @@ -6,6 +6,7 @@ declare global { CRON_LEGICA: string; PASSWORD: string; TIMEZONE: string; + LEGICA_DATE_FORMAT: string; } } } diff --git a/src/app.ts b/src/app.ts index c0380d9..33fcca7 100644 --- a/src/app.ts +++ b/src/app.ts @@ -22,12 +22,20 @@ const logger = pino( fileTransport ); +async function jobRunner() { + try { + await sendNextMessage(client); + } catch (err) { + logger.error(err); + } +} + const taskPlugin = new Elysia({ prefix: "/job" }) .use( cron({ name: "job", pattern: config.CRON_LEGICA, - run: () => sendNextMessage(client), + run: jobRunner, paused: true, timezone: config.TIMEZONE, }) @@ -143,9 +151,11 @@ const taskPlugin = new Elysia({ prefix: "/job" }) } }, { - body: t.Object({ - url: t.String(), - }), + body: t.Optional( + t.Object({ + url: t.Optional(t.String()), + }) + ), detail: { summary: "Send legica-dana post to discord channels", }, diff --git a/src/common/sendDiscordMessage.ts b/src/common/sendDiscordMessage.ts index 64ab73a..e0d1823 100644 --- a/src/common/sendDiscordMessage.ts +++ b/src/common/sendDiscordMessage.ts @@ -1,13 +1,32 @@ import { getFirstHtml, getImgTitle } from "@common"; +import { config } from "@constants"; +import dayjs from "dayjs"; +import customParseFormat from "dayjs/plugin/customParseFormat"; import { Client, MessageEmbed, TextChannel } from "discord.js"; +dayjs.extend(customParseFormat); + +const dateRegex = /\d{1,2}.\d{1,2}.\d{4}/g; + export async function sendDiscordMessage( client: Client, - url: string + url: string, + dateCheck?: dayjs.Dayjs ): Promise { if (!url) return; const { img, title } = await getImgTitle(url); + if (dateCheck) { + const date = dateRegex.exec(title)?.[0]; + const dayjsDate = dayjs(date, config.LEGICA_DATE_FORMAT); + if (!dateCheck.isSame(dayjsDate, "D")) + throw new Error( + `Post failed date check, date from post ${date}, date checked ${dateCheck.format( + config.LEGICA_DATE_FORMAT + )}` + ); + } + client.channels.cache.forEach(async (channel) => { try { if (channel.type !== "text") return null; @@ -39,10 +58,6 @@ export async function sendDiscordMessage( } export async function sendNextMessage(client: Client): Promise { - try { - const href = await getFirstHtml(); - await sendDiscordMessage(client, href); - } catch (err) { - console.error(err); - } + const href = await getFirstHtml(); + await sendDiscordMessage(client, href, dayjs()); } diff --git a/src/constants/config.ts b/src/constants/config.ts index e807ab3..9e53406 100644 --- a/src/constants/config.ts +++ b/src/constants/config.ts @@ -17,6 +17,7 @@ const config: ProjectConfig = { APP_VERSION: version, LEGICA_URL: "https://sib.net.hr/legica-dana", TIMEZONE: process.env.TIMEZONE || "utc", + LEGICA_DATE_FORMAT: process.env.LEGICA_DATE_FORMAT || "DD.MM.YYYY", }; export { config };