From f8cd5ee97f967c07e25994e648b765eb7d5dfa8f Mon Sep 17 00:00:00 2001 From: baegteun Date: Wed, 12 Jun 2024 22:58:42 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20EO=20Planet=20=EC=88=98=EC=A7=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eo-planet-news.json | 7 ++++ src/eo-planet.ts | 87 +++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 2 ++ 3 files changed, 96 insertions(+) create mode 100644 eo-planet-news.json create mode 100644 src/eo-planet.ts diff --git a/eo-planet-news.json b/eo-planet-news.json new file mode 100644 index 0000000..91b7ef7 --- /dev/null +++ b/eo-planet-news.json @@ -0,0 +1,7 @@ +{ + "title": "\n 200:1 면접에서 ‘상위 1%’라고 칭찬 받았던 이유\n ", + "description": "앞선 편에서 밝혔듯 저는 IT 지식이나 스펙이 전무한 상태에서 IT 스타트업에 입사하였습니다. 일련의 입사 전후의 과정을 블로그에 올렸었는데 꽤 많은 분들이 면접 노하우나 고민을 ...", + "url": "https://eopla.net//magazines/17365", + "thumbnailURL": "https://eopla.net/undefined", + "color": 7467936 +} \ No newline at end of file diff --git a/src/eo-planet.ts b/src/eo-planet.ts new file mode 100644 index 0000000..8c55bcd --- /dev/null +++ b/src/eo-planet.ts @@ -0,0 +1,87 @@ +import "dotenv/config"; +import axios, { AxiosResponse } from "axios"; +import * as cheerio from "cheerio"; +import { EmbedBuilder, WebhookClient } from "discord.js"; +import { News } from "./news"; +import { getJSON, setJSON } from "./utils/json-utils"; + +interface EOPlanetNews { + title: string; +} + +const eoPlanetJSONFile = "./eo-planet-news.json"; + +const getLastNewsTitle = (): string | null => { + const json = getJSON(eoPlanetJSONFile); + return json?.title ?? null; +}; + +const setLastNewsTitle = (news: EOPlanetNews) => { + setJSON(eoPlanetJSONFile, news); +}; + +const getEOPlanetList = async () => { + const eoPlanetBaseURL = "https://eopla.net/"; + const result: News[] = []; + let html: AxiosResponse | undefined; + let $: cheerio.CheerioAPI; + try { + html = await axios.get(eoPlanetBaseURL); + $ = cheerio.load(html?.data); + } catch (error) { + console.log("ERROR : getEOPlanet"); + return result; + } + + const $bodyList = $("div.magazines ").children("div.magazine-container"); + + $bodyList.each((i, elem) => { + const news = { + title: $(elem).find(".title").text(), + description: $(elem).find(".body").text().slice(0, 100) + "...", + url: eoPlanetBaseURL + $(elem).find(".title-container a").attr("href"), + thumbnailURL: `${eoPlanetBaseURL}${$(elem).find("a").attr("src")}`, + color: 0x71f3a0 + }; + result.push(news); + }); + return result.slice(0, 20); +}; + +export const sendEOPlanet = async () => { + const eoPlanetList = await getEOPlanetList(); + const lastNewsTitle = getLastNewsTitle(); + let filteredList = eoPlanetList; + + if (lastNewsTitle) { + const lastIndex = eoPlanetList.findIndex((news) => news.title === lastNewsTitle); + if (lastIndex !== -1) { + filteredList = eoPlanetList.slice(0, lastIndex); + } + } + + if (filteredList.length === 0) { + return; + } + + const webhookClient = new WebhookClient({ + url: process.env.EO_PLANET_WEBHOOK ?? "" + }); + + const embeds = filteredList.reverse().map((news) => + new EmbedBuilder() + .setTitle(news.title) + .setDescription(news.description) + .setURL(news.url) + .setColor(news.color) + .setThumbnail(news.thumbnailURL ?? null) + ); + + await webhookClient.send({ + content: `## 오늘의 EO Planet 최신글 ${filteredList.length}개!`, + embeds: embeds + }); + + const lastIndex = filteredList.length - 1; + setLastNewsTitle(filteredList[lastIndex]); +}; diff --git a/src/index.ts b/src/index.ts index 2cbc05e..f4d1a19 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +import { sendEOPlanet } from "./eo-planet"; import { sendDesignYozm } from "./yozm-design"; import { sendDevelopYozm } from "./yozm-develop"; import { sendPMYozm } from "./yozm-pm"; @@ -8,6 +9,7 @@ async function main() { await sendDesignYozm(); await sendPMYozm(); await sendProductYozm(); + await sendEOPlanet(); } main();