hellowhales

Simplify ApiHandler

1 -let request = require('request');
2 -let options = {
3 - 'method': 'GET',
4 - 'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/areaBasedList?ServiceKey=3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D&contentTypeId=15&areaCode=&sigunguCode=&cat1=&cat2=&cat3=&listYN=Y&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&arrange=C&numOfRows=12&pageNo=1&_type=json',
5 - 'headers': {
6 - }
7 -};
8 -
9 -var contentId = new Array();
10 -request(options, function (error, response, body) {
11 - if (error) {
12 - throw new Error(error);
13 - }
14 -let info = JSON.parse(body);
15 -for(i in info['response']['body']['items']['item']){
16 - contentId[i]=info['response']['body']['items']['item'][i]['contentid'];
17 - let Info ={
18 - 'public': {
19 - 'method': 'GET',
20 - 'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailCommon?'
21 - + 'ServiceKey=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D'
22 - + '&contentTypeId=' + '15'
23 - + '&contentId=' + +contentId[i]
24 - + '&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&defaultYN=Y&firstImageYN=Y&areacodeYN=Y&catcodeYN=Y&addrinfoYN=Y&mapinfoYN=Y&overviewYN=Y&transGuideYN=Y&_type=json',
25 - 'headers': {}
26 - },
27 - 'detail': {
28 - 'method': 'GET',
29 - 'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailIntro?'
30 - + 'ServiceKey=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D'
31 - + '&contentTypeId=' + '15'
32 - + '&contentId=' + contentId[i]
33 - + '&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&introYN=Y&_type=json',
34 - 'headers': {}
35 - },
36 - 'weather': {
37 - 'method': 'GET',
38 - 'url': 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst?'
39 - + 'serviceKey=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D'
40 - + '&pageNo=' + '1'
41 - + '&numOfRows=' + '50'
42 - + '&dataType=' + 'JSON'
43 - + '&base_date=' + '20211129'
44 - + '&base_time=' + '0500'
45 - + '&nx=' + '55 '
46 - + '&ny=' + '127',
47 - 'headers': {}
48 - }
49 - };
50 - request(Info.public, function (error, response, body) {
51 - if (error) {
52 - throw new Error(error);
53 - }
54 - let pinfo = JSON.parse(body);
55 - console.log('축제 : ' + pinfo['response']['body']['items']['item']['title']);
56 - console.log('축제 주소 : ' + pinfo['response']['body']['items']['item']['addr1']);
57 - console.log('전화번호 : ' + pinfo['response']['body']['items']['item']['tel']);
58 - console.log('축제위도 : ' + pinfo['response']['body']['items']['item']['mapx']);
59 - console.log('축제경도 : ' + pinfo['response']['body']['items']['item']['mapy']);
60 - console.log('ID : ' + pinfo['response']['body']['items']['item']['contentid']);
61 - console.log('축제 정보 : ' + pinfo['response']['body']['items']['item']['overview']);
62 - console.log('이미지 Url : ' + pinfo['response']['body']['items']['item']['firstimage']);
63 - console.log('홈페이지 Url : ' + pinfo['response']['body']['items']['item']['homepage']);
64 - console.log('')
65 - });
66 - request(Info.detail, function (error, response, body) {
67 - if (error) {
68 - throw new Error(error);
69 - }
70 - let dinfo = JSON.parse(body);
71 - console.log('축제 시작일 : ' + dinfo['response']['body']['items']['item']['eventstartdate']);
72 - console.log('축제 종료일 : ' + dinfo['response']['body']['items']['item']['eventenddate']);
73 - console.log('나이제한 : ' + dinfo['response']['body']['items']['item']['agelimit']);
74 - });
75 - request(Info.weather, function (error, response, body) {
76 - if (error) {
77 - throw new Error(error);
78 - }
79 - let winfo = JSON.parse(body);
80 -
81 - for( let item of winfo['response']['body']['items']['item']) {
82 -
83 - if(item['category']=='TMP') {
84 - console.log('온도: '+item['fcstValue']);
85 - } else if(item['category']=='PTY') {
86 - let weather_code = item['fcstValue']
87 -
88 - if (weather_code == '1'){ console.log('날씨 : 비');}
89 - else if (weather_code == '2'){ console.log('날씨 :비/눈') ;}
90 - else if (weather_code == '3'){ console.log('날씨 :눈') ;}
91 - else if (weather_code == '4'){ console.log('날씨 :소나기') ;}
92 - else{console.log('없음') ;}
93 - }
94 - }
95 - });
96 - }
97 -});
1 -{
2 - "_id": {
3 - "$oid": "619a35f2a96f26c4d20a5122"
4 - },
5 - "title": "《MMCA 이건희컬렉션 특별전: 한국미술명작》",
6 - "addr": "서울특별시 종로구 삼청로 30",
7 - "tel": "02-3701-9500",
8 - "mapx": 126.9800649747,
9 - "mapy": 37.5788361778,
10 - "createdAt": {
11 - "$date": "2021-11-21T12:05:06.042Z"
12 - },
13 - "updatedAt": {
14 - "$date": "2021-11-21T12:05:06.042Z"
15 - },
16 - "__v": 0
17 -}
18 -
19 -{
20 - "_id": {
21 - "$oid": "619a361f6a4cad598e278632"
22 - },
23 - "title": "가무악극으로 만나는 토요 상설공연",
24 - "addr": "충청남도 부여군 규암면 백제문로 388",
25 - "tel": "041-832-5765",
26 - "mapx": 126.8997016069,
27 - "mapy": 36.3048428766,
28 - "createdAt": {
29 - "$date": "2021-11-21T12:05:51.663Z"
30 - },
31 - "updatedAt": {
32 - "$date": "2021-11-21T12:05:51.663Z"
33 - },
34 - "__v": 0
35 -}
36 -{
37 - "_id": {
38 - "$oid": "619a3642f8e65868598b5a17"
39 - },
40 - "title": "가산수피아 '봄의 정원'",
41 - "addr": "경상북도 칠곡군 가산면 학하들안2길 105",
42 - "tel": "054-971-9861",
43 - "mapx": 128.4850691574,
44 - "mapy": 36.0861545517,
45 - "createdAt": {
46 - "$date": "2021-11-21T12:06:26.416Z"
47 - },
48 - "updatedAt": {
49 - "$date": "2021-11-21T12:06:26.416Z"
50 - },
51 - "__v": 0
52 -}
53 -{
54 - "_id": {
55 - "$oid": "619a3663a1816be4d2a4882b"
56 - },
57 - "title": "가족끼리 빙어체험 2021",
58 - "addr": "경기도 양평군 단월면 곱다니길 55-2 수미마을 방문객센터",
59 - "tel": "031-775-5205",
60 - "mapx": 127.6574135888,
61 - "mapy": 37.514550408,
62 - "createdAt": {
63 - "$date": "2021-11-21T12:06:59.535Z"
64 - },
65 - "updatedAt": {
66 - "$date": "2021-11-21T12:06:59.535Z"
67 - },
68 - "__v": 0
69 -}
70 -{
71 - "_id": {
72 - "$oid": "619a3688b715b78b16cc6208"
73 - },
74 - "title": "가족끼리딸기 캠피닉",
75 - "addr": "경기도 양평군 단월면 곱다니길 55-2",
76 - "tel": "031-775-5205",
77 - "mapx": 127.657366373,
78 - "mapy": 37.5144948588,
79 - "createdAt": {
80 - "$date": "2021-11-21T12:07:36.556Z"
81 - },
82 - "updatedAt": {
83 - "$date": "2021-11-21T12:07:36.556Z"
84 - },
85 - "__v": 0
86 -}
87 -{
88 - "_id": {
89 - "$oid": "619a36abc8bcb135bf91ff48"
90 - },
91 - "title": "가족오페라 <마술피리> 2021",
92 - "addr": "대구광역시 북구 호암로 15",
93 - "tel": "053-666-6170",
94 - "mapx": 128.592257023,
95 - "mapy": 35.8835072863,
96 - "createdAt": {
97 - "$date": "2021-11-21T12:08:11.131Z"
98 - },
99 - "updatedAt": {
100 - "$date": "2021-11-21T12:08:11.131Z"
101 - },
102 - "__v": 0
103 -}
104 -{
105 - "_id": {
106 - "$oid": "619a36d2623874f5ebdb540d"
107 - },
108 - "title": "감악산 꽃&별 여행",
109 - "addr": "거창군 신원면 덕산리 산 57",
110 - "tel": "055-940-3390",
111 - "mapx": 127.9163995903,
112 - "mapy": 35.5912836573,
113 - "createdAt": {
114 - "$date": "2021-11-21T12:08:50.766Z"
115 - },
116 - "updatedAt": {
117 - "$date": "2021-11-21T12:08:50.766Z"
118 - },
119 - "__v": 0
120 -}
121 -{
122 - "_id": {
123 - "$oid": "619a36ff8d2678a5079637ab"
124 - },
125 - "title": "강동북페스티벌",
126 - "addr": "온라인 개최",
127 - "tel": "강일 427-4004, 성내 471-0044, 해공 478-9656, 암사 429-0476, 천호 488-7223, 둔촌 489-6557",
128 - "mapx": 127.1573829418,
129 - "mapy": 37.5512675743,
130 - "createdAt": {
131 - "$date": "2021-11-21T12:09:35.075Z"
132 - },
133 - "updatedAt": {
134 - "$date": "2021-11-21T12:09:35.075Z"
135 - },
136 - "__v": 0
137 -}
138 -{
139 - "_id": {
140 - "$oid": "619a37162e9742f8315195ed"
141 - },
142 - "title": "강동선사문화축제",
143 - "addr": "서울특별시 강동구 성내로 25",
144 - "tel": "02-3425-8530",
145 - "mapx": 127.1237865998,
146 - "mapy": 37.530177751,
147 - "createdAt": {
148 - "$date": "2021-11-21T12:09:58.635Z"
149 - },
150 - "updatedAt": {
151 - "$date": "2021-11-21T12:09:58.635Z"
152 - },
153 - "__v": 0
154 -}
155 -{
156 - "_id": {
157 - "$oid": "619a3733fc3082f196a0a589"
158 - },
159 - "title": "강동청년축제",
160 - "tel": "02-482-7647",
161 - "createdAt": {
162 - "$date": "2021-11-21T12:10:27.165Z"
163 - },
164 - "updatedAt": {
165 - "$date": "2021-11-21T12:10:27.165Z"
166 - },
167 - "__v": 0
168 -}
169 -{
170 - "_id": {
171 - "$oid": "619a379163ec5352d3c1bae6"
172 - },
173 - "title": "강릉 단오제",
174 - "addr": "강원도 강릉시 단오장길 1",
175 - "tel": "033-641-1593",
176 - "mapx": 128.8950169869,
177 - "mapy": 37.7482134974,
178 - "createdAt": {
179 - "$date": "2021-11-21T12:12:01.456Z"
180 - },
181 - "updatedAt": {
182 - "$date": "2021-11-21T12:12:01.456Z"
183 - },
184 - "__v": 0
185 -}
186 -{
187 - "_id": {
188 - "$oid": "619a37aba63f1a25f61a4f5e"
189 - },
190 - "title": "강릉국제영화제(GIFF)",
191 - "addr": "강원도 강릉시 경강로2021번길 9-1",
192 - "tel": "033-641-8079",
193 - "mapx": 128.8898396064,
194 - "mapy": 37.7504353618,
195 - "createdAt": {
196 - "$date": "2021-11-21T12:12:27.604Z"
197 - },
198 - "updatedAt": {
199 - "$date": "2021-11-21T12:12:27.604Z"
200 - },
201 - "__v": 0
202 -}
...\ No newline at end of file ...\ No newline at end of file
...@@ -12,8 +12,6 @@ const festivalSchema = new Schema({ ...@@ -12,8 +12,6 @@ const festivalSchema = new Schema({
12 overview : String, 12 overview : String,
13 firstimage : String, 13 firstimage : String,
14 homepage : String, 14 homepage : String,
15 - district : String,
16 - city : String,
17 temparature : Number, 15 temparature : Number,
18 weathers : String 16 weathers : String
19 // weather : { 17 // weather : {
......
...@@ -4,7 +4,10 @@ const db = require('mongodb'); ...@@ -4,7 +4,10 @@ const db = require('mongodb');
4 const Festival = require('./models/Festival'); 4 const Festival = require('./models/Festival');
5 const request = require('request-promise-native'); 5 const request = require('request-promise-native');
6 require("dotenv").config({ path: "variables.env"}); 6 require("dotenv").config({ path: "variables.env"});
7 - 7 +const ServiceKey = '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D';
8 +const ServiceKey2 ='%2FGjtI8kwZeJTzJm%2BxUxz%2Bjh15wnmV3rwFuRvrq3oRSqyklfiZfbUaqmsG0McVPJMdXSUYetGaCXl0ZkbfMI0BQ%3D%3D'
9 +const ServiceKey3 ='%2FsBWti235XX%2Fg1%2FqBZfiNQ6A%2BJmF3WL%2FboaNqJH4v3eWic59SiHc6W5vgZKU7Hjocj%2BAntIqHfhXOpmE5CpAFw%3D%3D'
10 +const WeatherServiceKey = '2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D';
8 const COORDINATES = require('./coordinates')['COORDINATES']; 11 const COORDINATES = require('./coordinates')['COORDINATES'];
9 const DISTRICT = [ 12 const DISTRICT = [
10 "서울특별시", "부산광역시", "울산광역시", "대구광역시", "대전광역시", 13 "서울특별시", "부산광역시", "울산광역시", "대구광역시", "대전광역시",
...@@ -51,7 +54,7 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{ ...@@ -51,7 +54,7 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{
51 let options = { 54 let options = {
52 'method': 'GET', 55 'method': 'GET',
53 'url' : 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/areaBasedList' 56 'url' : 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/areaBasedList'
54 - + '?ServiceKey=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D' 57 + + '?ServiceKey=' + ServiceKey2
55 + '&contentTypeId=15&areaCode=&sigunguCode=&cat1=&cat2=&cat3=&listYN=Y&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&arrange=C&numOfRows=12' 58 + '&contentTypeId=15&areaCode=&sigunguCode=&cat1=&cat2=&cat3=&listYN=Y&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&arrange=C&numOfRows=12'
56 + '&pageNo='+ i 59 + '&pageNo='+ i
57 + '&_type=json', 60 + '&_type=json',
...@@ -64,13 +67,14 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{ ...@@ -64,13 +67,14 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{
64 throw new Error(error); 67 throw new Error(error);
65 } 68 }
66 let info = JSON.parse(body); 69 let info = JSON.parse(body);
70 +
67 let items = info['response']['body']['items']['item']; 71 let items = info['response']['body']['items']['item'];
68 for(item of items) { 72 for(item of items) {
69 let Info = { 73 let Info = {
70 'public': { 74 'public': {
71 'method': 'GET', 75 'method': 'GET',
72 'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailCommon?' 76 'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailCommon?'
73 - + 'ServiceKey=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D' 77 + + 'ServiceKey=' + ServiceKey2
74 + '&contentTypeId=' + '15' 78 + '&contentTypeId=' + '15'
75 + '&contentId=' + item['contentid'] 79 + '&contentId=' + item['contentid']
76 + '&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&defaultYN=Y&firstImageYN=Y&areacodeYN=Y&catcodeYN=Y&addrinfoYN=Y&mapinfoYN=Y&overviewYN=Y&transGuideYN=Y&_type=json', 80 + '&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&defaultYN=Y&firstImageYN=Y&areacodeYN=Y&catcodeYN=Y&addrinfoYN=Y&mapinfoYN=Y&overviewYN=Y&transGuideYN=Y&_type=json',
...@@ -79,7 +83,7 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{ ...@@ -79,7 +83,7 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{
79 'detail': { 83 'detail': {
80 'method': 'GET', 84 'method': 'GET',
81 'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailIntro?' 85 'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailIntro?'
82 - + 'ServiceKey=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D' 86 + + 'ServiceKey=' + ServiceKey2
83 + '&contentTypeId=' + '15' 87 + '&contentTypeId=' + '15'
84 + '&contentId=' + item['contentid'] 88 + '&contentId=' + item['contentid']
85 + '&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&introYN=Y&_type=json', 89 + '&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&introYN=Y&_type=json',
...@@ -110,8 +114,7 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{ ...@@ -110,8 +114,7 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{
110 newFestival.overview= pinfo['response']['body']['items']['item']['overview']; 114 newFestival.overview= pinfo['response']['body']['items']['item']['overview'];
111 newFestival.firstimage = pinfo['response']['body']['items']['item']['firstimage']; 115 newFestival.firstimage = pinfo['response']['body']['items']['item']['firstimage'];
112 newFestival.homepage = pinfo ['response']['body']['items']['item']['homepage']; 116 newFestival.homepage = pinfo ['response']['body']['items']['item']['homepage'];
113 - newFestival.district = distriction[0]; // 광역시/도 117 +
114 - newFestival.city = distriction[1]; // 시군구
115 118
116 await request(Info.detail, function (error, response, body) { 119 await request(Info.detail, function (error, response, body) {
117 if (error) { 120 if (error) {
...@@ -128,11 +131,11 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{ ...@@ -128,11 +131,11 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{
128 let curDate = ('0500' < todayTime ? todayString : yesterdayString); 131 let curDate = ('0500' < todayTime ? todayString : yesterdayString);
129 132
130 Info.weather.url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst?' 133 Info.weather.url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst?'
131 - + 'serviceKey=' + '2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D' 134 + + 'serviceKey=' + WeatherServiceKey
132 + '&pageNo=' + '1' 135 + '&pageNo=' + '1'
133 + '&numOfRows=' + '2000' 136 + '&numOfRows=' + '2000'
134 + '&dataType=' + 'JSON' 137 + '&dataType=' + 'JSON'
135 - + '&base_date=' + '20211129' 138 + + '&base_date=' + curDate
136 + '&base_time=' + '0500' 139 + '&base_time=' + '0500'
137 + '&nx=' + nx 140 + '&nx=' + nx
138 + '&ny=' + ny; 141 + '&ny=' + ny;
......