Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0d2fba3883 | ||
|
|
77aa924161 | ||
|
|
b4b6f42a1a | ||
|
|
dd0b7cde36 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "legica-dana",
|
"name": "legica-dana",
|
||||||
"version": "2.0.1",
|
"version": "2.0.3",
|
||||||
"main": "src/app.ts",
|
"main": "src/app.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "bun src/app.ts"
|
"start": "bun src/app.ts"
|
||||||
@@ -14,14 +14,17 @@
|
|||||||
"axios": "^0.26.0",
|
"axios": "^0.26.0",
|
||||||
"body-parser": "^1.20.2",
|
"body-parser": "^1.20.2",
|
||||||
"cheerio": "^1.0.0-rc.10",
|
"cheerio": "^1.0.0-rc.10",
|
||||||
|
"dayjs": "^1.11.10",
|
||||||
"discord.js": "^12.5.1",
|
"discord.js": "^12.5.1",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"elysia": "^0.7.15",
|
"elysia": "^0.7.15",
|
||||||
|
"lodash-es": "^4.17.21",
|
||||||
"minimatch": "^9.0.3",
|
"minimatch": "^9.0.3",
|
||||||
"pino": "^8.15.4",
|
"pino": "^8.15.4",
|
||||||
"typescript": "^4.1.5"
|
"typescript": "^4.1.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/lodash-es": "^4.17.9",
|
||||||
"@types/node": "^20.8.2",
|
"@types/node": "^20.8.2",
|
||||||
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
||||||
"@typescript-eslint/parser": "^6.7.4",
|
"@typescript-eslint/parser": "^6.7.4",
|
||||||
|
|||||||
1
process-env.d.ts
vendored
1
process-env.d.ts
vendored
@@ -6,6 +6,7 @@ declare global {
|
|||||||
CRON_LEGICA: string;
|
CRON_LEGICA: string;
|
||||||
PASSWORD: string;
|
PASSWORD: string;
|
||||||
TIMEZONE: string;
|
TIMEZONE: string;
|
||||||
|
LEGICA_DATE_FORMAT: string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
18
src/app.ts
18
src/app.ts
@@ -22,12 +22,20 @@ const logger = pino(
|
|||||||
fileTransport
|
fileTransport
|
||||||
);
|
);
|
||||||
|
|
||||||
|
async function jobRunner() {
|
||||||
|
try {
|
||||||
|
await sendNextMessage(client);
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const taskPlugin = new Elysia({ prefix: "/job" })
|
const taskPlugin = new Elysia({ prefix: "/job" })
|
||||||
.use(
|
.use(
|
||||||
cron({
|
cron({
|
||||||
name: "job",
|
name: "job",
|
||||||
pattern: config.CRON_LEGICA,
|
pattern: config.CRON_LEGICA,
|
||||||
run: () => sendNextMessage(client),
|
run: jobRunner,
|
||||||
paused: true,
|
paused: true,
|
||||||
timezone: config.TIMEZONE,
|
timezone: config.TIMEZONE,
|
||||||
})
|
})
|
||||||
@@ -143,9 +151,11 @@ const taskPlugin = new Elysia({ prefix: "/job" })
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
body: t.Object({
|
body: t.Optional(
|
||||||
url: t.String(),
|
t.Object({
|
||||||
}),
|
url: t.Optional(t.String()),
|
||||||
|
})
|
||||||
|
),
|
||||||
detail: {
|
detail: {
|
||||||
summary: "Send legica-dana post to discord channels",
|
summary: "Send legica-dana post to discord channels",
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,13 +1,31 @@
|
|||||||
import { getFirstHtml, getImgTitle } from "@common";
|
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";
|
import { Client, MessageEmbed, TextChannel } from "discord.js";
|
||||||
|
|
||||||
|
dayjs.extend(customParseFormat);
|
||||||
|
|
||||||
export async function sendDiscordMessage(
|
export async function sendDiscordMessage(
|
||||||
client: Client,
|
client: Client,
|
||||||
url: string
|
url: string,
|
||||||
|
dateCheck?: dayjs.Dayjs
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (!url) return;
|
if (!url) return;
|
||||||
const { img, title } = await getImgTitle(url);
|
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) => {
|
client.channels.cache.forEach(async (channel) => {
|
||||||
try {
|
try {
|
||||||
if (channel.type !== "text") return null;
|
if (channel.type !== "text") return null;
|
||||||
@@ -39,10 +57,6 @@ export async function sendDiscordMessage(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function sendNextMessage(client: Client): Promise<void> {
|
export async function sendNextMessage(client: Client): Promise<void> {
|
||||||
try {
|
const href = await getFirstHtml();
|
||||||
const href = await getFirstHtml();
|
await sendDiscordMessage(client, href, dayjs());
|
||||||
await sendDiscordMessage(client, href);
|
|
||||||
} catch (err) {
|
|
||||||
console.error(err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ const config: ProjectConfig = {
|
|||||||
APP_VERSION: version,
|
APP_VERSION: version,
|
||||||
LEGICA_URL: "https://sib.net.hr/legica-dana",
|
LEGICA_URL: "https://sib.net.hr/legica-dana",
|
||||||
TIMEZONE: process.env.TIMEZONE || "utc",
|
TIMEZONE: process.env.TIMEZONE || "utc",
|
||||||
|
LEGICA_DATE_FORMAT: process.env.LEGICA_DATE_FORMAT || "DD.MM.YYYY",
|
||||||
};
|
};
|
||||||
|
|
||||||
export { config };
|
export { config };
|
||||||
|
|||||||
Reference in New Issue
Block a user