박권수

feat. Hide secret key

1 const axios = require('axios'); 1 const axios = require('axios');
2 2
3 -const SERVICE_KEY = "tNd%2FZ0MMJA5NZrU9nA5IVTKkhpz6N3j1OGpFT0PmbcCOVEZbpR9PYiNHuD9rLuSsyMWkTXPqHsHLWoxlW%2BVVrg%3D%3D" 3 +exports.updateMedicineInfo = async() => {
4 -const url = "http://apis.data.go.kr/1471000/DrbEasyDrugInfoService/getDrbEasyDrugList"; 4 + console.log('starting')
5 - 5 +
6 -const updateMedicineInfo = async() => { 6 + const itemArray = await getItemsList(getQueryURL);
7 - const queryParams = '?' + encodeURIComponent('ServiceKey') + '=' + SERVICE_KEY; 7 + await exportJsonData(itemArray);
8 - const pageNum36 = '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent(1) 8 +
9 - const pageNum37 = '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent(2); 9 + console.log('data is saved');
10 - const numOfItem = '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent(2); 10 +}
11 +
12 +//queryUrl을 return하는 함수 : 한 페이지에 100개의 item씩 요청할 수 있다.
13 +const getQueryURL = (i) => {
14 + const url = "http://apis.data.go.kr/1471000/DrbEasyDrugInfoService/getDrbEasyDrugList";
15 + const queryParams = '?' + encodeURIComponent('ServiceKey') + '=' + process.env.SERVICE_KEY;
16 + const pageNum = '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent(i);
17 + const numOfItem = '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent(100);
11 const output = '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); 18 const output = '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json');
12 19
13 - const result36 = await axios.get(url + queryParams + pageNum36 + numOfItem + output); 20 + return url + queryParams + pageNum + numOfItem + output;
14 - const result37 = await axios.get(url + queryParams + pageNum37 + numOfItem + output); 21 +}
22 +
23 +//모든 page의 item을 list에 push해서 return하는 함수
24 +const getItemsList = async(queryUrl) => {
25 + let i = 1, getItem = null, items = null;
26 + const result = new Array();
27 +
28 + while(true) {
29 + getItem = await axios.get(queryUrl(i));
30 + items = getItem.data.body.items;
15 31
16 - console.log(result36.data.body.items); 32 + if(items === undefined)
17 - console.log(result37.data.body.items); 33 + return result;
34 +
35 + result.push(...items);
36 + console.log('medicine data getting processing... : page', i, 'done');
37 + i++;
38 + }
18 } 39 }
19 40
20 -updateMedicineInfo();
...\ No newline at end of file ...\ No newline at end of file
41 +//itemArray에 있는 모든 data를 json으로 만들어서 json파일로 저장
42 +const exportJsonData = async(itemList) => {
43 + itemList.forEach(item => {
44 +
45 + })
46 +}
...\ No newline at end of file ...\ No newline at end of file
......