6 Commits
2.0.1 ... 2.0.4

Author SHA1 Message Date
Fran Jurmanović
9d137b6b52 fix legica date format 2023-11-01 15:46:04 +01:00
Fran Jurmanović
79110bdb1c version increase 2023-10-21 11:51:25 +02:00
Fran Jurmanović
0d2fba3883 fix regex not working everytime 2023-10-21 11:50:47 +02:00
Fran Jurmanović
77aa924161 increase version 2023-10-21 11:50:23 +02:00
Fran Jurmanović
b4b6f42a1a increment version 2023-10-14 01:09:22 +02:00
Fran Jurmanović
dd0b7cde36 add date check and fix send next post not working 2023-10-14 01:04:04 +02:00
6 changed files with 41 additions and 12 deletions

BIN
bun.lockb

Binary file not shown.

View File

@@ -1,6 +1,6 @@
{
"name": "legica-dana",
"version": "2.0.1",
"version": "2.0.4",
"main": "src/app.ts",
"scripts": {
"start": "bun src/app.ts"
@@ -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",

1
process-env.d.ts vendored
View File

@@ -6,6 +6,7 @@ declare global {
CRON_LEGICA: string;
PASSWORD: string;
TIMEZONE: string;
LEGICA_DATE_FORMAT: string;
}
}
}

View File

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

View File

@@ -1,13 +1,31 @@
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);
export async function sendDiscordMessage(
client: Client,
url: string
url: string,
dateCheck?: dayjs.Dayjs
): Promise<void> {
if (!url) return;
const { img, title } = await getImgTitle(url);
if (dateCheck) {
const dateRegex = /\d{1,2}.\d{1,2}.\d{4}/g;
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 +57,6 @@ export async function sendDiscordMessage(
}
export async function sendNextMessage(client: Client): Promise<void> {
try {
const href = await getFirstHtml();
await sendDiscordMessage(client, href);
} catch (err) {
console.error(err);
}
await sendDiscordMessage(client, href, dayjs());
}

View File

@@ -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 || "D.M.YYYY",
};
export { config };