Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/#134: 공지사항 크롤링에 발생하는 문제점 해결 #135

Merged
merged 3 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/crawling/noticeCrawling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ export const noticeListCrawling = async (
const pinnedNotice: string[] = [];
const normalNotice: string[] = [];

if (link === 'http://geoinfo.pknu.ac.kr/05piazza/08.php') {
const noticePage2Link =
'http://geoinfo.pknu.ac.kr/05piazza/08.php?p=2&key=&keyword=&bbscode=cate0501&reCategory=';
const noticePage2Lists = await noticeListCrawling(noticePage2Link, link);
pinnedNotice.push(...noticePage2Lists.pinnedNotice);
normalNotice.push(...noticePage2Lists.normalNotice);
}

tableData.each((index, element) => {
const anchorElement = $(element).find('a');
let tmpLink = anchorElement.attr('href');
Expand All @@ -86,7 +94,7 @@ export const noticeListCrawling = async (
.text()
.match(/\d{4}[-.]\d{2}[-.]\d{2}/);

if (link === 'http://geoinfo.pknu.ac.kr/05piazza/08.php') {
if (link.startsWith('http://geoinfo.pknu.ac.kr/05piazza/08.php')) {
// 공간정보시스템공학과
if ($(element).find('td').first().text().trim() === '공지')
pinnedNotice.push(tmpLink);
Expand Down
17 changes: 17 additions & 0 deletions src/db/data/noticeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ const saveNotice = (notice: Notice, major: string): Promise<void> => {
});
};

const deleteNotice = (major: string, noticeLinks: string[], mode: string) => {
const deleteQuery = `DELETE FROM ${major}${mode} WHERE link = ?`;
for (const link of noticeLinks) {
db.query(deleteQuery, [link], (err) => {
if (err) notificationToSlack(`${major}${mode} 공지사항 삭제 실패`);
});
}
};

export const saveNoticeToDB = async (): Promise<PushNoti> => {
const selectQuery = 'SELECT * FROM departments;';
const results = await new Promise<College[]>((resolve) => {
Expand Down Expand Up @@ -97,6 +106,7 @@ export const saveNoticeToDB = async (): Promise<PushNoti> => {
return;
}
const rows = res as RowDataPacket[];
const deleteNotiLinks: string[] = [];
let pinnedNotiLink: string[] = [];

if (Array.isArray(rows) && rows.length > 0)
Expand All @@ -112,6 +122,13 @@ export const saveNoticeToDB = async (): Promise<PushNoti> => {
savePromises.push(saveNotice(result, major + '고정'));
}
}

for (const noticeLink of pinnedNotiLink) {
if (!noticeLists.pinnedNotice.includes(noticeLink)) {
deleteNotiLinks.push(noticeLink);
}
}
deleteNotice(major, deleteNotiLinks, '고정');
});
}

Expand Down
24 changes: 1 addition & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,6 @@ webpush();
const handleDeployToServer = () => {
// 이 함수는 현재 배포되어있는 서버를 위해 사용되는 로직이며 최초 서버에 배포되는 1회만 실행되도록 하기위한 함수에요
// 그렇기에 아래에 작성된 코드들은 배포서버에 배포되면 다음 배포전 수정해주세요!!

// 어학 관련 테이블 생성 후 데이터 삽입
const createLanguageDataTable = () => {
const createTableQuery = `CREATE TABLE 어학공지 (
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
link VARCHAR(255) NOT NULL UNIQUE,
uploadDate VARCHAR(255) NOT NULL
);`;

db.query(createTableQuery, async (error) => {
if (error) {
console.log('어학 DB 생성 실패', error);
return;
}

console.log('어학 테이블 생성 성공!');
await saveLanguageNoticeToDB();
});
};

createLanguageDataTable();
};

handleDeployToServer();
// handleDeployToServer();