-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
439 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,6 @@ lerna-debug.log* | |
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
|
||
**/.env | ||
**/.env | ||
|
||
src/public/data/**/*.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
{ | ||
"collection": "@nestjs/schematics", | ||
"sourceRoot": "src" | ||
"sourceRoot": "src", | ||
"compilerOptions": { | ||
"assets": [ | ||
{ | ||
"include": "./public/data/result/*.json", | ||
"outDir": "./dist" | ||
} | ||
] | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
const REGION_BASE = require('./region_base'); | ||
|
||
const axios = require('axios'); | ||
const fs = require('fs-extra'); | ||
|
||
const path = require('path'); | ||
require('dotenv').config(); | ||
|
||
const targetPath = path.join(__dirname, '..', '..'); | ||
|
||
const KAKAO_REST_API_KEY = process.env.KAKAO_REST_API_KEY; | ||
|
||
const updatedAt = Date.now(); | ||
|
||
let totalDataCount = 0; | ||
|
||
const resultMap = new Map(); | ||
|
||
const uniquenessMap = new Map(); | ||
|
||
const regionKeys = Object.keys(REGION_BASE); | ||
|
||
const getTargetPath = (region) => `${targetPath}/public/data/result/${region}.json`; | ||
|
||
const TARGET_DIR = `${targetPath}/public/data/result`; | ||
|
||
const LOCATION_PATH = `${targetPath}/public/data/result/location.json`; | ||
|
||
const RESULT_PATH = `${targetPath}/public/data/result/total_count.txt`; | ||
|
||
const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time)); | ||
|
||
// ๊ฒ์์ด, ์นดํ ๊ณ ๋ฆฌ // | ||
const QUERY = '์ฐ'; | ||
const CATEGORY_NAME = '์ฌํ > ๊ด๊ด,๋ช ์ > ์ฐ'; | ||
|
||
// ex) ['์์ธํน๋ณ์ ์ข ๋ก๊ตฌ', ...]; | ||
const REGION = Object.entries(REGION_BASE).reduce((acc, [k, v]) => [...acc, ...v.map((u) => `${k} ${u}`)], []); | ||
|
||
const kakaoSearchAPI = ({ query, page = 1 }) => | ||
axios.get(`https://dapi.kakao.com/v2/local/search/keyword.json`, { | ||
headers: { | ||
Authorization: `KakaoAK ${KAKAO_REST_API_KEY}`, | ||
}, | ||
params: { | ||
query, | ||
page, | ||
}, | ||
timeout: 10000, | ||
}); | ||
|
||
async function* asyncAPI(query, total_pages) { | ||
for (let i = 1; i <= total_pages; i++) { | ||
const { data } = await kakaoSearchAPI({ query, page: i }); | ||
await sleep(300); | ||
yield data; | ||
} | ||
} | ||
|
||
const fetchRegionData = async () => { | ||
for (const region of REGION) { | ||
const query = `${region} ${QUERY}`; | ||
const regionKey = region.split(' ')[0]; | ||
const targetRegionArray = resultMap.get(regionKey); | ||
|
||
const { | ||
data: { meta }, | ||
} = await kakaoSearchAPI({ query }); | ||
|
||
const { pageable_count, total_count } = meta; | ||
|
||
if (pageable_count * 15 < total_count) { | ||
throw new Error(`${region} ์ง์ญ ์ ๋ณด๋ ๋ ์์ธํ ๊ฒ์์ด ํ์ํฉ๋๋ค`); | ||
} | ||
|
||
for await (const { documents } of asyncAPI(query, pageable_count)) { | ||
documents.forEach((v) => { | ||
const isDuplicatePresent = uniquenessMap.has(v.id); | ||
if (isDuplicatePresent) return; | ||
uniquenessMap.set(v.id, true); | ||
|
||
const isCategoryMatch = v.category_name !== CATEGORY_NAME; | ||
if (isCategoryMatch) return; | ||
|
||
targetRegionArray.push(v); | ||
}); | ||
} | ||
|
||
console.log(`${region} ์ง์ญ ๋ฐ์ดํฐ๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ ํ์์ต๋๋ค.`); | ||
} | ||
}; | ||
|
||
const writeRegionData = () => { | ||
for (const regionKey of regionKeys) { | ||
const regionResultArray = resultMap.get(regionKey); | ||
|
||
totalDataCount += regionResultArray.length; | ||
|
||
fs.writeFileSync(getTargetPath(regionKey), JSON.stringify(regionResultArray)); | ||
} | ||
}; | ||
|
||
const writeResult = () => { | ||
const endAt = new Date().getTime(); | ||
fs.writeFileSync(RESULT_PATH, `totalDataCount : ${totalDataCount} time: ${(endAt - updatedAt) / 1000}`); | ||
fs.writeFileSync(LOCATION_PATH, JSON.stringify(REGION_BASE)); | ||
}; | ||
|
||
(async () => { | ||
if (!fs.existsSync(TARGET_DIR)) { | ||
fs.mkdirSync(TARGET_DIR); | ||
} | ||
|
||
for (const regionKey of regionKeys) { | ||
resultMap.set(regionKey, []); | ||
} | ||
|
||
try { | ||
await fetchRegionData(); | ||
|
||
writeRegionData(); | ||
|
||
writeResult(); | ||
console.log('๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ ํ์์ต๋๋ค'); | ||
} catch (error) { | ||
console.warn('์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ์์ฑํ๋ ์ค ๋ฌธ์ ๊ฐ ๋ฐ์ ํ์ต๋๋ค', error); | ||
fs.rmdirSync(TARGET_DIR, { recursive: true }); | ||
} | ||
})(); |
Oops, something went wrong.