jimin

코드 폴더

1 +// api에서 데이터 불러오기
2 +
3 +var xml2js = require('xml-js');
4 +var urlencode = require('urlencode');
5 +var xmlToJson;
6 +
7 +//var Dust_Month=0;
8 +
9 +var Feb_Dust_2018 =0 ;
10 +var Mar_Dust_2018 =0 ;
11 +var April_Dust_2018 =0 ;
12 +var May_Dust_2018 =0 ;
13 +var June_Dust_2018 =0 ;
14 +var Feb_Dust_2019 =0 ;
15 +var Mar_Dust_2019 =0 ;
16 +var April_Dust_2019 =0 ;
17 +var May_Dust_2019 =0 ;
18 +var June_Dust_2019 =0 ;
19 +var Feb_Dust_2020 =0 ;
20 +var Mar_Dust_2020 =0 ;
21 +var April_Dust_2020 =0 ;
22 +var May_Dust_2020 =0 ;
23 +var June_Dust_2020 =0 ;
24 +const request = require('request');
25 +var url;
26 +const Dust_URL = "http://openAPI.seoul.go.kr:8088/" //미세먼지 api주소_1
27 +const Dust_URL2 = "/xml/MonthlyAverageAirQuality/1/5/" //미세먼지 api주소_2
28 +const Dust_ServiceKey = "73754a53586a696d38345951716b6b" //Dust url의 key값
29 +const Dust_Place ='/'+urlencode("강남구") //측정한 장소
30 +
31 +//2020년 2월~6월 미세먼지 평균
32 +function DelayNode_2020(item)
33 +{
34 + return new Promise(function(resolve,reject){
35 + setTimeout(function(){
36 + url = Dust_URL+Dust_ServiceKey+Dust_URL2+item+Dust_Place;
37 + request(url,function(error,res,body) {
38 + if(error){
39 + console.log('error=>${error}');
40 + }
41 + else{
42 + if(res.statusCode == 200) {
43 + var result = body;
44 + xmlToJson = xml2js.xml2json(result, {compact: true, spaces: 4});
45 + xmlToJson2 = JSON.parse(xmlToJson);
46 + if(item == 202002){
47 + console.log("2미세먼지 농도:", xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]);
48 + Feb_Dust_2020 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
49 + }
50 + else if(item == 202003){
51 + console.log("3미세먼지 농도:", xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]);
52 + Mar_Dust_2020 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
53 + }
54 + else if(item == 202004){
55 + console.log("4미세먼지 농도:", xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]);
56 + April_Dust_2020 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
57 + }
58 + else if(item == 202005){
59 + console.log("5미세먼지 농도:", xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]);
60 + May_Dust_2020 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
61 + }
62 + else if(item == 202006){
63 + console.log("6미세먼지 농도:", xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]);
64 + June_Dust_2020 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
65 + }
66 + }
67 + }
68 + });
69 + resolve();
70 + },500)
71 + })
72 +}
73 +async function Dust_Month_2020(Dust_Month_2020){
74 + for(let i=0;i<Dust_Month_2020.length;i++)
75 + {
76 + await DelayNode_2020(Dust_Month_2020[i]);
77 + }
78 +}
79 +Dust_Month_2020([202002,202003,202004,202005,202006]);
80 +
81 +//2019년 2월~6월 미세먼지 평균
82 +function DelayNode_2019(item)
83 +{
84 + return new Promise(function(resolve,reject){
85 + setTimeout(function(){
86 + url = Dust_URL+Dust_ServiceKey+Dust_URL2+item+Dust_Place;
87 + request(url,function(error,res,body) {
88 + if(error){
89 + console.log('error=>${error}');
90 + }
91 + else{
92 + if(res.statusCode == 200) {
93 + var result = body;
94 + xmlToJson = xml2js.xml2json(result, {compact: true, spaces: 4});
95 + xmlToJson2 = JSON.parse(xmlToJson);
96 + if(item == 201902){
97 + console.log("2미세먼지 농도:", xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]);
98 + Feb_Dust_2019 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
99 + }
100 + else if(item == 201903){
101 + console.log("3미세먼지 농도:", xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]);
102 + Mar_Dust_2019 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
103 + }
104 + else if(item == 201904){
105 + console.log("4미세먼지 농도:", xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]);
106 + April_Dust_2019 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
107 + }
108 + else if(item == 201905){
109 + console.log("5미세먼지 농도:", xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]);
110 + May_Dust_2019 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
111 + }
112 + else if(item == 201906){
113 + console.log("6미세먼지 농도:", xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]);
114 + June_Dust_2019 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
115 + }
116 + }
117 + }
118 + });
119 + resolve();
120 + },500)
121 + })
122 +}
123 +async function Dust_Month_2019(Dust_Month_2019){
124 + for(let i=0;i<Dust_Month_2019.length;i++)
125 + {
126 + await DelayNode_2019(Dust_Month_2019[i]);
127 + }
128 +}
129 +Dust_Month_2019([201902,201903,201904,201905,201906]);
130 +
131 +
132 +//2018년 2월~6월 미세먼지 평균
133 +function DelayNode_2018(item)
134 +{
135 + return new Promise(function(resolve,reject){
136 + setTimeout(function(){
137 + url = Dust_URL+Dust_ServiceKey+Dust_URL2+item+Dust_Place;
138 + request(url,function(error,res,body) {
139 + if(error){
140 + console.log('error=>${error}');
141 + }
142 + else{
143 + if(res.statusCode == 200) {
144 + var result = body;
145 + xmlToJson = xml2js.xml2json(result, {compact: true, spaces: 4});
146 + xmlToJson2 = JSON.parse(xmlToJson);
147 + if(item == 201802){
148 + console.log("2미세먼지 농도:", xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]);
149 + Feb_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
150 + }
151 + else if(item == 201803){
152 + console.log("3미세먼지 농도:", xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]);
153 + Mar_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
154 + }
155 + else if(item == 201804){
156 + console.log("4미세먼지 농도:", xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]);
157 + April_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
158 + }
159 + else if(item == 201805){
160 + console.log("5미세먼지 농도:", xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]);
161 + May_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
162 + }
163 + else if(item == 201806){
164 + console.log("6미세먼지 농도:", xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"]);
165 + June_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
166 + }
167 + }
168 + }
169 + });
170 + resolve();
171 + },500)
172 + })
173 +}
174 +async function Dust_Month_2018(Dust_Month_2018){
175 + for(let i=0;i<Dust_Month_2018.length;i++)
176 + {
177 + await DelayNode_2018(Dust_Month_2018[i]);
178 + }
179 +}
180 +Dust_Month_2018([201802,201803,201804,201805,201806]);
181 +
182 +module.exports = function(app)
183 +{
184 + app.get('/',function(req,res){
185 + res.render('index',{
186 + Feb_Dust_2018,
187 + Mar_Dust_2018,
188 + April_Dust_2018,
189 + May_Dust_2018,
190 + June_Dust_2018,
191 + Feb_Dust_2019,
192 + Mar_Dust_2019,
193 + April_Dust_2019,
194 + May_Dust_2019,
195 + June_Dust_2019,
196 + Feb_Dust_2020,
197 + Mar_Dust_2020,
198 + April_Dust_2020,
199 + May_Dust_2020,
200 + June_Dust_2020
201 + })
202 + });
203 + app.get('/about',function(req,res){
204 + res.render('about.ejs');
205 + });
206 +}
...\ No newline at end of file ...\ No newline at end of file