diff --git a/.vscode/settings.json b/.vscode/settings.json index eb26746..b23df9a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,5 +6,6 @@ "editor.formatOnSave": true, "editor.formatOnPaste": true, "editor.defaultFormatter": "esbenp.prettier-vscode" - } + }, + "typescript.tsdk": "node_modules\\typescript\\lib" } diff --git a/src/app.ts b/src/app.ts index e901412..8735417 100644 --- a/src/app.ts +++ b/src/app.ts @@ -3,7 +3,7 @@ import morgan from "morgan"; import testRouter from "./test/test.router.ts"; import dotenv from "dotenv"; import { userContract, userRouter } from "./user/user_router"; -import { dbScheduler } from "./scheduler/scheduler"; +import { dbScheduler } from "./scheduler/scheduler.ts"; import db from "./db/models"; import schedule from "node-schedule"; import { createExpressEndpoints, initServer } from "@ts-rest/express"; @@ -14,6 +14,7 @@ import { reviewRouter, reviewContract } from "./review/mod.ts"; import { restaurantsContract, restaurantsRouter } from "./restaurants"; import { initContract } from "@ts-rest/core"; import { regionsContract, regionsRouter } from "./regions"; +import {} from "node:fs"; dotenv.config(); @@ -29,7 +30,7 @@ app.use("/openapi", swaggerUi.serve, swaggerUi.setup(openApiDocument)); db.sequelize .sync({ - force: true, //임시 + force: false, //임시 }) .then(() => { console.log("connected"); @@ -73,9 +74,9 @@ createExpressEndpoints(reviewContract, reviewRouter, app, jwtMiddleware); app.listen(3000, () => { console.log("Server On"); - // schedule.scheduleJob("0 * * * * *", function () { - // dbScheduler(); - // }); + schedule.scheduleJob("0 * * * * *", function () { + dbScheduler(); + }); }); /* diff --git a/src/scheduler/scheduler.ts b/src/scheduler/scheduler.ts index 6e38b03..b232c98 100644 --- a/src/scheduler/scheduler.ts +++ b/src/scheduler/scheduler.ts @@ -6,13 +6,12 @@ import { allInsertDB } from "./all_insert"; export const dbScheduler = async () => { console.log(new Date() + "Running Scheduler !!! "); + // 오늘의 요일 0 ~ 6 const nowData = await db.Restaurant.count(); if (nowData > 0) { - console.log("0보다 큼"); await updateRestData(); } else { - console.log("DB에 데이터 없음"); await allInsertDB(); } }; diff --git a/src/scheduler/sectors.json b/src/scheduler/sectors.json new file mode 100644 index 0000000..241c4e1 --- /dev/null +++ b/src/scheduler/sectors.json @@ -0,0 +1,30 @@ +{ + "0": { + "data1": "Genrestrtcate", + "data2": "Genrestrtsash" + }, + "1": { + "data1": "Genrestrtfastfood", + "data2": "Genrestrtstandpub" + }, + "2": { + "data1": "Genrestrtlunch", + "data2": "Genrestrtjpnfood" + }, + "3": { + "data1": "Genrestrtbsrpcook", + "data2": "Genrestrtchifood'" + }, + "4": { + "data1": "Genrestrttratearm", + "data2": "Genrestrtbuff" + }, + "5": { + "data1": "Genrestrtsoup", + "data2": "Genrestrtfugu" + }, + "6": { + "data1": "Genrestrtmovmntcook", + "data2": "Kidscafe" + } +} diff --git a/src/scheduler/update_restaurant.ts b/src/scheduler/update_restaurant.ts index 2fda376..fd8b98f 100644 --- a/src/scheduler/update_restaurant.ts +++ b/src/scheduler/update_restaurant.ts @@ -1,6 +1,97 @@ import axios from "axios"; import db from "../db/models/index"; +import { readFileSync } from "node:fs"; +import { join, dirname } from "node:path"; +import { fileURLToPath } from "node:url"; +import { restaurantsRouter } from "../restaurants"; export const updateRestData = async () => { - console.log("업데이트 "); + const today = new Date(2023, 11, 10).getDay(); + // 하루에 2개의 업종별로 업데이트 + const path = join(dirname(fileURLToPath(import.meta.url)), "sectors.json"); + const raw = readFileSync(path, { encoding: "utf8" }); + const secotrs = JSON.parse(raw); + + await updateDB(secotrs[today].data1); +}; + +const updateDB = async (_sectors: string) => { + const key = process.env.api_key; + + try { + //업종명 + + const getHeadUrl = + "https://openapi.gg.go.kr/" + + _sectors + + "?key=" + + key + + "&type=json&pSize=5&pindex=1"; + + const getHead = await axios.get(getHeadUrl); + const headData = getHead.data; + + // 데이터 총 개수 + // const total = headData.GENRESTRT[0].head[0].list_total_count; + const total = 5; + // 한번에 가져올 데이터 수 + const chunkSize = 2; + var cnt = 0; + for (let idx = 1; idx < total; idx += chunkSize) { + const url = + "https://openapi.gg.go.kr/" + + _sectors + + "?key=" + + key + + "&type=json&pSize=" + + chunkSize + + "&pindex=" + + Math.floor(idx / chunkSize + 1); + + // 전체 데이터 가져오기 + const response = await axios.get(url); + const responseData = response.data; + const restaurants = responseData[_sectors][1].row; + + for (let i in restaurants) { + // 폐업한 가게 중에서 DB에 데이터가 있다면 삭제 + if (restaurants[i].BSN_STATE_NM == "폐업") { + const closed = await db.Restaurant.findOne({ + where: { + restaurant_name: restaurants[i].BIZPLC_NM, + adress: restaurants[i].REFINE_LOTNO_ADDR, + }, + }); + // 삭제 로직 + if (closed !== null) { + db.Restaurant.destroy({ + where: { + restaurant_name: restaurants[i].BIZPLC_NM, + adress: restaurants[i].REFINE_LOTNO_ADDR, + }, + }); + } + } else { + // 영업 중인 가게 정보 업데이트 + db.Restaurant.update( + { + restaurant_name: restaurants[i].BIZPLC_NM, + restaurant_type: restaurants[i].SANITTN_BIZCOND_NM, + adress: restaurants[i].REFINE_LOTNO_ADDR, // 도로명 주소가 없는 식당이 존재 함 + lat: Number(restaurants[i].REFINE_WGS84_LAT), + lon: Number(restaurants[i].REFINE_WGS84_LOGT), + }, + { + where: { + restaurant_name: restaurants[i].BIZPLC_NM, + adress: restaurants[i].REFINE_LOTNO_ADDR, + }, + }, + ); + } + } + } + } catch (error) { + console.log("api 호출 중 오류 발생 : ", error); + } };