change selectors to fit the new design
This commit is contained in:
@@ -195,7 +195,8 @@ const taskPlugin = new Elysia({ prefix: "/job" })
|
|||||||
return true;
|
return true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
set.status = 400;
|
set.status = 400;
|
||||||
return err;
|
logger.error(err);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ import { config } from "@constants";
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import cheerio from "cheerio";
|
import cheerio from "cheerio";
|
||||||
|
|
||||||
export async function getFirstHtml(): Promise<string> {
|
export async function getFirstHtml(): Promise<string | undefined> {
|
||||||
const response = await axios.get(config.LEGICA_URL);
|
const response = await axios.get(config.LEGICA_URL);
|
||||||
const html = response.data;
|
const html = response.data;
|
||||||
const $ = cheerio.load(html);
|
const $ = cheerio.load(html);
|
||||||
const { href } = $(".News-link.c-def")?.attr() || {};
|
const href = $(".legica-dana").first().find("a").attr("href");
|
||||||
return href;
|
return href;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,9 @@ export async function getImgTitle(href: string): Promise<Legica> {
|
|||||||
const html = response.data;
|
const html = response.data;
|
||||||
const $ = cheerio.load(html);
|
const $ = cheerio.load(html);
|
||||||
|
|
||||||
const title = $(".Article-inner > h1").text();
|
const title = $(".article-title-container > h1").text();
|
||||||
const { src: img } = $(".Article-media > img").attr() || {};
|
const src = $(".image-holder", ".article-content").find("img").attr("src");
|
||||||
|
if (!src) throw new Error(`Image not found at ${href}.`);
|
||||||
|
|
||||||
return { title, img };
|
return { title, img: src };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ export async function sendDiscordMessage(
|
|||||||
url: string,
|
url: string,
|
||||||
dateCheck?: dayjs.Dayjs
|
dateCheck?: dayjs.Dayjs
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (!url) return;
|
|
||||||
const { img, title } = await getImgTitle(url);
|
const { img, title } = await getImgTitle(url);
|
||||||
|
|
||||||
if (dateCheck) {
|
if (dateCheck) {
|
||||||
@@ -26,7 +25,7 @@ export async function sendDiscordMessage(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
client.channels.cache.forEach(async (channel) => {
|
const promises = client.channels.cache.map(async (channel) => {
|
||||||
try {
|
try {
|
||||||
if (channel.type !== "text") return null;
|
if (channel.type !== "text") return null;
|
||||||
const embeddedMessage = new MessageEmbed().setTitle(title).setImage(img);
|
const embeddedMessage = new MessageEmbed().setTitle(title).setImage(img);
|
||||||
@@ -50,13 +49,16 @@ export async function sendDiscordMessage(
|
|||||||
console.error(`Reaction ${reaction} to channel ${channel.id} failed.`);
|
console.error(`Reaction ${reaction} to channel ${channel.id} failed.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch {
|
} catch (err) {
|
||||||
console.error(`Message to channel ${channel.id} failed.`);
|
console.error(`Message to channel ${channel.id} failed.`);
|
||||||
|
throw err;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
await Promise.all(promises);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function sendNextMessage(client: Client): Promise<void> {
|
export async function sendNextMessage(client: Client): Promise<void> {
|
||||||
const href = await getFirstHtml();
|
const href = await getFirstHtml();
|
||||||
|
if (!href) throw new Error("URL cannot be empty!");
|
||||||
await sendDiscordMessage(client, href, dayjs());
|
await sendDiscordMessage(client, href, dayjs());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user