Showing
1 changed file
with
75 additions
and
67 deletions
1 | -// api에서 데이터 불러오기 | 1 | + // api에서 데이터 불러오기 |
2 | + var xml2js = require('xml-js'); | ||
3 | + var urlencode = require('urlencode'); | ||
4 | + var xmlToJson; | ||
2 | 5 | ||
3 | -var xml2js = require('xml-js'); | 6 | + // 변수들 |
4 | -var urlencode = require('urlencode'); | 7 | + var City; // 도시 이름 |
5 | -var xmlToJson; | 8 | + var Feb_Dust_2018 =0 ; |
9 | + var Mar_Dust_2018 =0 ; | ||
10 | + var April_Dust_2018 =0 ; | ||
11 | + var May_Dust_2018 =0 ; | ||
12 | + var June_Dust_2018 =0 ; | ||
13 | + var Feb_Dust_2019 =0 ; | ||
14 | + var Mar_Dust_2019 =0 ; | ||
15 | + var April_Dust_2019 =0 ; | ||
16 | + var May_Dust_2019 =0 ; | ||
17 | + var June_Dust_2019 =0 ; | ||
18 | + var Feb_Dust_2020 =0 ; | ||
19 | + var Mar_Dust_2020 =0 ; | ||
20 | + var April_Dust_2020 =0 ; | ||
21 | + var May_Dust_2020 =0 ; | ||
22 | + var June_Dust_2020 =0 ; | ||
23 | + var check = false; | ||
24 | + var url; | ||
25 | + var Dust_Place //측정 도시 | ||
26 | + const request = require('request'); | ||
27 | + const Dust_URL = "http://openAPI.seoul.go.kr:8088/" //미세먼지 api주소_1 | ||
28 | + const Dust_URL2 = "/xml/MonthlyAverageAirQuality/1/5/" //미세먼지 api주소_2 | ||
29 | + const Dust_ServiceKey = "73754a53586a696d38345951716b6b" //api의 key값 | ||
6 | 30 | ||
7 | -//var Dust_Month=0; | 31 | +module.exports = function(app) |
8 | - | ||
9 | -var City; | ||
10 | -var Feb_Dust_2018 =0 ; | ||
11 | -var Mar_Dust_2018 =0 ; | ||
12 | -var April_Dust_2018 =0 ; | ||
13 | -var May_Dust_2018 =0 ; | ||
14 | -var June_Dust_2018 =0 ; | ||
15 | -var Feb_Dust_2019 =0 ; | ||
16 | -var Mar_Dust_2019 =0 ; | ||
17 | -var April_Dust_2019 =0 ; | ||
18 | -var May_Dust_2019 =0 ; | ||
19 | -var June_Dust_2019 =0 ; | ||
20 | -var Feb_Dust_2020 =0 ; | ||
21 | -var Mar_Dust_2020 =0 ; | ||
22 | -var April_Dust_2020 =0 ; | ||
23 | -var May_Dust_2020 =0 ; | ||
24 | -var June_Dust_2020 =0 ; | ||
25 | -const request = require('request'); | ||
26 | -var url; | ||
27 | -const Dust_URL = "http://openAPI.seoul.go.kr:8088/" //미세먼지 api주소_1 | ||
28 | -const Dust_URL2 = "/xml/MonthlyAverageAirQuality/1/5/" //미세먼지 api주소_2 | ||
29 | -const Dust_ServiceKey = "73754a53586a696d38345951716b6b" //Dust url의 key값 | ||
30 | -const Dust_Place = '/' + urlencode("강남구") //측정한 장소 | ||
31 | - | ||
32 | - | ||
33 | - | ||
34 | -//2020년 2월~6월 미세먼지 평균 | ||
35 | -function DelayNode_2020(item) | ||
36 | { | 32 | { |
33 | + app.get('/',function(req,res){ | ||
34 | + res.render('City_login') | ||
35 | + }); | ||
36 | + | ||
37 | + app.get('/city',function(req,res){ | ||
38 | + //2020년 2월~6월 미세먼지 평균 | ||
39 | + function DelayNode_2020(item) | ||
40 | + { | ||
41 | + City = req.query.city; | ||
37 | return new Promise(function(resolve,reject){ | 42 | return new Promise(function(resolve,reject){ |
38 | setTimeout(function(){ | 43 | setTimeout(function(){ |
39 | - //Dust_Place ='/'+ City | 44 | + Dust_Place ='/'+ urlencode(req.query.city) |
40 | url = Dust_URL+Dust_ServiceKey+Dust_URL2+item+Dust_Place; | 45 | url = Dust_URL+Dust_ServiceKey+Dust_URL2+item+Dust_Place; |
41 | - //console.log(url) | ||
42 | request(url,function(error,res,body) { | 46 | request(url,function(error,res,body) { |
43 | if(error){ | 47 | if(error){ |
44 | console.log('error=>${error}'); | 48 | console.log('error=>${error}'); |
... | @@ -67,24 +71,25 @@ function DelayNode_2020(item) | ... | @@ -67,24 +71,25 @@ function DelayNode_2020(item) |
67 | } | 71 | } |
68 | }); | 72 | }); |
69 | resolve(); | 73 | resolve(); |
70 | - },500) | ||
71 | }) | 74 | }) |
72 | -} | 75 | + }) |
73 | -async function Dust_Month_2020(Dust_Month_2020){ | 76 | + } |
77 | + async function Dust_Month_2020(Dust_Month_2020){ | ||
74 | for(let i=0;i<Dust_Month_2020.length;i++) | 78 | for(let i=0;i<Dust_Month_2020.length;i++) |
75 | { | 79 | { |
76 | await DelayNode_2020(Dust_Month_2020[i]); | 80 | await DelayNode_2020(Dust_Month_2020[i]); |
77 | } | 81 | } |
78 | -} | 82 | + } |
79 | -Dust_Month_2020([202002,202003,202004,202005,202006]); | 83 | + Dust_Month_2020([202002,202003,202004,202005,202006]); |
80 | 84 | ||
81 | -//2019년 2월~6월 미세먼지 평균 | 85 | + //2019년 2월~6월 미세먼지 평균 |
82 | -function DelayNode_2019(item) | 86 | + function DelayNode_2019(item) |
83 | -{ | 87 | + { |
84 | return new Promise(function(resolve,reject){ | 88 | return new Promise(function(resolve,reject){ |
85 | setTimeout(function(){ | 89 | setTimeout(function(){ |
86 | - //Dust_Place ='/'+ City | 90 | + Dust_Place ='/'+ urlencode(req.query.city) |
87 | url = Dust_URL+Dust_ServiceKey+Dust_URL2+item+Dust_Place; | 91 | url = Dust_URL+Dust_ServiceKey+Dust_URL2+item+Dust_Place; |
92 | + console.log(url) | ||
88 | request(url,function(error,res,body) { | 93 | request(url,function(error,res,body) { |
89 | if(error){ | 94 | if(error){ |
90 | console.log('error=>${error}'); | 95 | console.log('error=>${error}'); |
... | @@ -113,24 +118,24 @@ function DelayNode_2019(item) | ... | @@ -113,24 +118,24 @@ function DelayNode_2019(item) |
113 | } | 118 | } |
114 | }); | 119 | }); |
115 | resolve(); | 120 | resolve(); |
116 | - },500) | ||
117 | }) | 121 | }) |
118 | -} | 122 | + }) |
119 | -async function Dust_Month_2019(Dust_Month_2019){ | 123 | + } |
124 | + async function Dust_Month_2019(Dust_Month_2019){ | ||
120 | for(let i=0;i<Dust_Month_2019.length;i++) | 125 | for(let i=0;i<Dust_Month_2019.length;i++) |
121 | { | 126 | { |
122 | await DelayNode_2019(Dust_Month_2019[i]); | 127 | await DelayNode_2019(Dust_Month_2019[i]); |
123 | } | 128 | } |
124 | -} | 129 | + } |
125 | -Dust_Month_2019([201902,201903,201904,201905,201906]); | 130 | + Dust_Month_2019([201902,201903,201904,201905,201906]); |
126 | 131 | ||
127 | 132 | ||
128 | -//2018년 2월~6월 미세먼지 평균 | 133 | + //2018년 2월~6월 미세먼지 평균 |
129 | -function DelayNode_2018(item) | 134 | + function DelayNode_2018(item) |
130 | -{ | 135 | + { |
131 | return new Promise(function(resolve,reject){ | 136 | return new Promise(function(resolve,reject){ |
132 | setTimeout(function(){ | 137 | setTimeout(function(){ |
133 | - //Dust_Place ='/'+ City | 138 | + Dust_Place ='/'+ urlencode(req.query.city) |
134 | url = Dust_URL+Dust_ServiceKey+Dust_URL2+item+Dust_Place; | 139 | url = Dust_URL+Dust_ServiceKey+Dust_URL2+item+Dust_Place; |
135 | request(url,function(error,res,body) { | 140 | request(url,function(error,res,body) { |
136 | if(error){ | 141 | if(error){ |
... | @@ -143,42 +148,43 @@ function DelayNode_2018(item) | ... | @@ -143,42 +148,43 @@ function DelayNode_2018(item) |
143 | xmlToJson2 = JSON.parse(xmlToJson); | 148 | xmlToJson2 = JSON.parse(xmlToJson); |
144 | if(item == 201802){ | 149 | if(item == 201802){ |
145 | Feb_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]) | 150 | Feb_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]) |
151 | + console.log(1) | ||
146 | } | 152 | } |
147 | else if(item == 201803){ | 153 | else if(item == 201803){ |
148 | Mar_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]) | 154 | Mar_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]) |
155 | + console.log(2) | ||
149 | } | 156 | } |
150 | else if(item == 201804){ | 157 | else if(item == 201804){ |
151 | April_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]) | 158 | April_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]) |
159 | + console.log(3) | ||
152 | } | 160 | } |
153 | else if(item == 201805){ | 161 | else if(item == 201805){ |
154 | May_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]) | 162 | May_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]) |
163 | + console.log(4) | ||
155 | } | 164 | } |
156 | else if(item == 201806){ | 165 | else if(item == 201806){ |
157 | June_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]) | 166 | June_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]) |
167 | + console.log(5) | ||
168 | + console.log("Setting Complete!!!") | ||
169 | + check = true; | ||
158 | } | 170 | } |
159 | } | 171 | } |
160 | } | 172 | } |
161 | }); | 173 | }); |
162 | resolve(); | 174 | resolve(); |
163 | - },500) | ||
164 | }) | 175 | }) |
165 | -} | 176 | + }) |
166 | -async function Dust_Month_2018(Dust_Month_2018){ | 177 | + } |
178 | + async function Dust_Month_2018(Dust_Month_2018){ | ||
167 | for(let i=0;i<Dust_Month_2018.length;i++) | 179 | for(let i=0;i<Dust_Month_2018.length;i++) |
168 | { | 180 | { |
169 | await DelayNode_2018(Dust_Month_2018[i]); | 181 | await DelayNode_2018(Dust_Month_2018[i]); |
170 | } | 182 | } |
171 | -} | 183 | + } |
172 | -Dust_Month_2018([201802,201803,201804,201805,201806]); | 184 | + Dust_Month_2018([201802,201803,201804,201805,201806]); |
173 | 185 | ||
174 | -module.exports = function(app) | 186 | + //값을 다 받고난뒤 index.ejs에 값 보내주기 |
175 | -{ | 187 | + if(check == true){ |
176 | - app.get('/',function(req,res){ | ||
177 | - res.render('City_login') | ||
178 | - City = req.query.city; | ||
179 | - console.log(City) | ||
180 | - }); | ||
181 | - app.get('/city',function(req,res){ | ||
182 | res.render('index',{ | 188 | res.render('index',{ |
183 | Feb_Dust_2018, | 189 | Feb_Dust_2018, |
184 | Mar_Dust_2018, | 190 | Mar_Dust_2018, |
... | @@ -194,7 +200,9 @@ module.exports = function(app) | ... | @@ -194,7 +200,9 @@ module.exports = function(app) |
194 | Mar_Dust_2020, | 200 | Mar_Dust_2020, |
195 | April_Dust_2020, | 201 | April_Dust_2020, |
196 | May_Dust_2020, | 202 | May_Dust_2020, |
197 | - June_Dust_2020 | 203 | + June_Dust_2020, |
204 | + City | ||
198 | }) | 205 | }) |
206 | + } | ||
199 | }); | 207 | }); |
200 | } | 208 | } | ... | ... |
-
Please register or login to post a comment