UpdatingMedicineInfo.js
2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const axios = require('axios');
const Medicine = require('../models/medicine');
exports.updateMedicineInfo = async() => {
const itemArray = await getItemsList(getQueryURL);
await exportJsonData(itemArray);
console.log('All of data is updated!');
}
//queryUrl을 return하는 함수 : 한 페이지에 100개의 item씩 요청할 수 있다.
const getQueryURL = (i) => {
const url = "http://apis.data.go.kr/1471000/DrbEasyDrugInfoService/getDrbEasyDrugList";
const queryParams = '?' + encodeURIComponent('ServiceKey') + '=' + process.env.SERVICE_KEY;
const pageNum = '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent(i);
const numOfItem = '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent(100);
const output = '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json');
return url + queryParams + pageNum + numOfItem + output;
}
//모든 page의 item을 list에 push해서 return하는 함수
const getItemsList = async(queryUrl) => {
let i = 1, getItem = null, items = null;
const result = new Array();
while(true) {
getItem = await axios.get(queryUrl(i));
items = getItem.data.body.items;
if(items === undefined)
return result;
result.push(...items);
console.log('medicine data getting processing... : page', i, 'done');
i++;
}
}
//itemArray에 있는 모든 data를 MongoDB의 SMB collections에 저장함
const exportJsonData = async(itemList) => {
itemList.forEach(item => {
const medicine = new Medicine({
medicineId : item.itemSeq,
name : item.itemName,
company : item.entpName,
target : item.efcyQesitm,
dosage : item.useMethodQesitm,
warn : item.atpnWarnQesitm + '\n\n' + item.atpnQesitm,
antiEffect : item.seQesitm
})
Medicine.findOneAndUpdate({
medicineId : medicine.medicineId
}, medicine, {
upsert : true
})
})
}