diff --git a/src/crawling/whalebeCrawling.ts b/src/crawling/whalebeCrawling.ts index aaa5d8f9..6e7e08a5 100644 --- a/src/crawling/whalebeCrawling.ts +++ b/src/crawling/whalebeCrawling.ts @@ -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 => { const hostname = 'https://whalebe.pknu.ac.kr'; const whalebeLink = hostname + '/main'; @@ -20,7 +30,8 @@ export const whalebeCrawling = async (): Promise => { 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') @@ -32,6 +43,7 @@ export const whalebeCrawling = async (): Promise => { title, date, imgUrl, + link, }; whalebeData.push(tmpData); }); diff --git a/src/db/data/noticeHandler.ts b/src/db/data/noticeHandler.ts index 16fddb83..2cdbd9d0 100644 --- a/src/db/data/noticeHandler.ts +++ b/src/db/data/noticeHandler.ts @@ -236,11 +236,12 @@ export const saveSchoolNoticeToDB = async (): Promise => { }; export const saveWhalebeToDB = async (): Promise => { - 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((resolve) => { db.query(query, values, () => { diff --git a/src/db/table/createTables.ts b/src/db/table/createTables.ts index 7a010496..758cde14 100644 --- a/src/db/table/createTables.ts +++ b/src/db/table/createTables.ts @@ -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) => { diff --git a/src/index.ts b/src/index.ts index ef7507a6..9ed4272f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,7 @@ 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'; @@ -12,6 +13,7 @@ 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'; @@ -42,9 +44,45 @@ app.listen(env.SERVER_PORT, () => { webpush(); -const handleDeployToServer = () => { +const handleDeployToServer = async () => { // 이 함수는 현재 배포되어있는 서버를 위해 사용되는 로직이며 최초 서버에 배포되는 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(); + + 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();