generated from GSM-MSG/MSG-Repository-Generator
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters