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

Feat/#136: 웨일비 데이터 크롤링 데이터 추가 #137

Merged
merged 5 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
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
5 changes: 3 additions & 2 deletions src/db/data/noticeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,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
18 changes: 17 additions & 1 deletion 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();