Skip to content

Commit

Permalink
Merge pull request #109 from GDSC-PKNU-21-22/fix/#108
Browse files Browse the repository at this point in the history
Fix: 크롤링한 공지가 DB에 없는경우 DB에 저장 및 구독한 유저들에게 push 알림 주도록 수정
  • Loading branch information
pp449 authored Sep 26, 2023
2 parents b40b375 + d6edc4f commit 11b6f99
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/db/data/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const saveNotice = (notice: Notice, major: string): Promise<void> => {
db.query(saveNoticeQuery, values, (err) => {
if (err) {
console.log(`${major} 공지사항 입력 실패`);
resolve();
return;
}
console.log(`${major} 공지사항 입력 성공`);
resolve();
Expand Down Expand Up @@ -84,50 +86,55 @@ export const saveNoticeToDB = async (): Promise<string[]> => {
: college.departmentSubName;

if (noticeLists.pinnedNotice !== undefined) {
const pinnedNotiQuery = `SELECT link FROM ${major}고정 ORDER BY STR_TO_DATE(uploadDate, '%Y-%m-%d') DESC LIMIT 1;`;
let pinnedNotiLink = '';
const pinnedNotiQuery = `SELECT link FROM ${major}고정;`;
db.query(pinnedNotiQuery, async (err, res) => {
if (err) {
await notificationToSlack(pinnedNotiQuery.split('ORDER')[0] + '에러');
return;
}
const rows = res as RowDataPacket[];
if (Array.isArray(rows) && rows.length > 0) {
pinnedNotiLink = rows[0].link;
}
let pinnedNotiLink: string[] = [];

if (Array.isArray(rows) && rows.length > 0)
pinnedNotiLink = rows.map((row) => row.link);

for (const notice of noticeLists.pinnedNotice) {
const result = await noticeContentCrawling(notice);
if (result.path === '') {
notificationToSlack(`${notice} 콘텐츠 크롤링 실패`);
continue;
}
if (result.path === pinnedNotiLink) break;
savePromises.push(saveNotice(result, major + '고정'));
if (!pinnedNotiLink.includes(result.path)) {
if (!newNoticeMajor.includes(major)) newNoticeMajor.push(major);
savePromises.push(saveNotice(result, major + '고정'));
}
}
});
}

const normalNotiQuery = `SELECT link FROM ${major}일반 ORDER BY STR_TO_DATE(uploadDate, '%Y-%m-%d') DESC LIMIT 1;`;
let normalNotiLink = '';
const normalNotiQuery = `SELECT link FROM ${major}일반;`;
db.query(normalNotiQuery, async (err, res) => {
if (err) {
await notificationToSlack(normalNotiQuery.split('ORDER')[0] + '에러');
return;
}

const rows = res as RowDataPacket[];
if (Array.isArray(rows) && rows.length > 0) normalNotiLink = rows[0].link;
let normalNotiLink: string[] = [];
if (Array.isArray(rows) && rows.length > 0)
normalNotiLink = rows.map((row) => row.link);

for (const notice of noticeLists.normalNotice) {
const result = await noticeContentCrawling(notice);
if (result.path === '') {
notificationToSlack(`${notice} 콘텐츠 크롤링 실패`);
continue;
}
if (result.path === normalNotiLink) {
break;

if (!normalNotiLink.includes(result.path)) {
if (!newNoticeMajor.includes(major)) newNoticeMajor.push(major);
savePromises.push(saveNotice(result, major + '일반'));
}
if (!newNoticeMajor.includes(major)) newNoticeMajor.push(major);
savePromises.push(saveNotice(result, major + '일반'));
}
});
}
Expand Down

0 comments on commit 11b6f99

Please sign in to comment.