Skip to content

Commit

Permalink
Merge pull request #139 from GDSC-PKNU-Official/dev
Browse files Browse the repository at this point in the history
부리미 서버 1.04v 배포
  • Loading branch information
pp449 authored Oct 16, 2023
2 parents af4b40f + ad1d9d9 commit f84118f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
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
14 changes: 13 additions & 1 deletion src/crawling/whalebeCrawling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ export interface WhalebeData {
title: string;
date: string;
imgUrl: string;
link: string;
}

const sliceURL = (link: string): string => {
if (link.startsWith('location.href=')) {
link = link.slice(15);
link = link.slice(0, -2);
}

return link;
};

export const whalebeCrawling = async (): Promise<WhalebeData[]> => {
const hostname = 'https://whalebe.pknu.ac.kr';
const whalebeLink = hostname + '/main';
Expand All @@ -20,7 +30,8 @@ export const whalebeCrawling = async (): Promise<WhalebeData[]> => {

programs.each((_, element) => {
const imgUrl = hostname + $(element).find('img').attr('src');
const title = $(element).find('.card-title').text();
const link = hostname + sliceURL($(element).find('div').attr('onclick'));
const title = $(element).find('.card-title').text().trim();
const date = $(element)
.find('.app_date')
.find('.col-12')
Expand All @@ -32,6 +43,7 @@ export const whalebeCrawling = async (): Promise<WhalebeData[]> => {
title,
date,
imgUrl,
link,
};
whalebeData.push(tmpData);
});
Expand Down
22 changes: 20 additions & 2 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 Expand Up @@ -219,11 +236,12 @@ export const saveSchoolNoticeToDB = async (): Promise<void> => {
};

export const saveWhalebeToDB = async (): Promise<void> => {
const query = 'INSERT INTO 웨일비 (title, date, imgUrl) VALUES (?, ?, ?)';
const query =
'INSERT INTO 웨일비 (title, date, imgUrl, link) VALUES (?, ?, ?, ?)';
const whalebeDatas = await whalebeCrawling();

const promises = whalebeDatas.map((data) => {
const values = [data.title, data.date, data.imgUrl];
const values = [data.title, data.date, data.imgUrl, data.link];

return new Promise<void>((resolve) => {
db.query(query, values, () => {
Expand Down
3 changes: 2 additions & 1 deletion src/db/table/createTables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ const createWhalebeDataTable = () => {
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL UNIQUE,
date VARCHAR(255) NOT NULL,
imgUrl VARCHAR(255) NOT NULL
imgUrl VARCHAR(255) NOT NULL,
link VARCHAR(255) NOT NULL
);`;

db.query(createTableQuery, (error) => {
Expand Down
20 changes: 18 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import subscriptionRouter from '@apis/subscribe/controller';
import suggestionRouter from '@apis/suggestion/controller';
import env from '@config';
import { saveLanguageNoticeToDB } from '@db/data/languageHandler';
import { saveWhalebeToDB } from '@db/data/noticeHandler';
import db from '@db/index';
import { corsOptions } from '@middlewares/cors';
import errorHandler from '@middlewares/error-handler';
import cors from 'cors';
import express, { Request, Response } from 'express';
import morgan from 'morgan';
import webpush from 'src/config/webpush';
import notificationToSlack from 'src/hooks/notificateToSlack';
import { initialCrawling } from 'src/hooks/startCrawlingData';
import './hooks/cronNoticeCrawling';

Expand Down Expand Up @@ -42,7 +44,7 @@ app.listen(env.SERVER_PORT, () => {

webpush();

const handleDeployToServer = () => {
const handleDeployToServer = async () => {
// 이 함수는 현재 배포되어있는 서버를 위해 사용되는 로직이며 최초 서버에 배포되는 1회만 실행되도록 하기위한 함수에요
// 그렇기에 아래에 작성된 코드들은 배포서버에 배포되면 다음 배포전 수정해주세요!!

Expand All @@ -67,6 +69,20 @@ const handleDeployToServer = () => {
};

createLanguageDataTable();

const addColumnQuery = `ALTER TABLE 웨일비 ADD link VARCHAR(255)`;
db.query(addColumnQuery, (err) => {
if (err) {
notificationToSlack('배포시 최초 핸들러 함수 에러');
return;
}
const query = `delete from 웨일비;`;
db.query(query, (err) => {
if (!err) {
saveWhalebeToDB();
}
});
});
};

handleDeployToServer();
// handleDeployToServer();

0 comments on commit f84118f

Please sign in to comment.