Skip to content

Commit

Permalink
✨ EO Planet 수집
Browse files Browse the repository at this point in the history
  • Loading branch information
baekteun committed Jun 12, 2024
1 parent 5644efd commit f8cd5ee
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
7 changes: 7 additions & 0 deletions eo-planet-news.json
Original file line number Diff line number Diff line change
@@ -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
}
87 changes: 87 additions & 0 deletions src/eo-planet.ts
Original file line number Diff line number Diff line change
@@ -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<EOPlanetNews>(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<any> | 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]);
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sendEOPlanet } from "./eo-planet";
import { sendDesignYozm } from "./yozm-design";
import { sendDevelopYozm } from "./yozm-develop";
import { sendPMYozm } from "./yozm-pm";
Expand All @@ -8,6 +9,7 @@ async function main() {
await sendDesignYozm();
await sendPMYozm();
await sendProductYozm();
await sendEOPlanet();
}

main();

0 comments on commit f8cd5ee

Please sign in to comment.