weather.js
8.55 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var request = require('request');
let express = require('express');
let app=express();
let bodyParser = require('body-parser');
let session = require('express-session');
let cors = require('cors');
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}))
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors());
//nx,ny구하기
//변수들
let curaddress;
var url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst';
var queryParams = '?' + encodeURIComponent('serviceKey') + '=3OcUyvx97Vx2YikiZ9IHyRQ6suapku7Xn8VlefQKQWrGIFOGaejhbevwagcubdHfSiQAqJwCV5lyIutw0%2BsppA%3D%3D'; /* Service Key*/
//오늘의 날짜 구하기
let base,tommorow;
let today=new Date();
let CurDay=today.getFullYear().toString();
if(today.getMonth()<9){
CurDay+="0"+(today.getMonth()+1).toString();
}
else{
CurDay+=(today.getMonth()+1).toString();
}
if(today.getDate()<10){
base=CurDay+"0"+(today.getDate()-1).toString();
tommorow=CurDay+"0"+(today.getDate()+1).toString();
CurDay+="0"+today.getDate().toString();
}
else{
base=CurDay+(today.getDate()-1).toString();
tommorow=CurDay+(today.getDate()+1).toString();
CurDay+=today.getDate().toString();
}
console.log(tommorow);
//nx,ny구하기
const xlsx=require('xlsx')
const excel=xlsx.readFile('location.xlsx');
const sheet=excel.SheetNames[0];
const first=excel.Sheets[sheet];
const jsonData=xlsx.utils.sheet_to_json(first,{defval:""});
let nx,ny;
app.post('/api/address', (req, res) => {
let i=0;
while(i<3788){
if(jsonData[i].address1==req.body.address1 && jsonData[i].address2==req.body.address2 && jsonData[i].address3==req.body.address3){
nx=jsonData[i].nx;
ny=jsonData[i].ny;
curaddress=jsonData[i];
res.json({nx, ny});
break;
}
i+=1;
}
});
let a3=[], a4=[], a5=[], a6=[], a7=[], a8=[], a9=[], a10=[], a11=[], a12=[], a13=[], a14=[], a15=[], a16=[], a17=[], a18=[], a19=[], a20=[], a21=[], a22=[], a23=[];
//입력받기
app.post('/api/weather',(req,res)=>{
let Nx = req.body.dotX;
let Ny = req.body.dotY;
var queryParams = '?' + encodeURIComponent('serviceKey') + '=3OcUyvx97Vx2YikiZ9IHyRQ6suapku7Xn8VlefQKQWrGIFOGaejhbevwagcubdHfSiQAqJwCV5lyIutw0%2BsppA%3D%3D'; /* Service Key*/
var url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst';
queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); /* */
queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('1000'); /* */
queryParams += '&' + encodeURIComponent('dataType') + '=' + encodeURIComponent('JSON'); /* */
queryParams += '&' + encodeURIComponent('base_date') + '=' + encodeURIComponent(base); /* */
queryParams += '&' + encodeURIComponent('base_time') + '=' + encodeURIComponent('2300'); /* */
queryParams += '&' + encodeURIComponent('nx') + '=' + encodeURIComponent(Nx); /*nx*/
queryParams += '&' + encodeURIComponent('ny') + '=' + encodeURIComponent(Ny); /*ny*/
request({
url: url + queryParams,
method: 'GET'
}, function (error, response, body) {
let ex=JSON.parse(body);
let item=ex.response.body.items.item;
let i=0;
let a=[];//, a4=[], a5=[], a6=[], a7=[], a8=[], a9=[], a10=[], a11=[], a12=[], a13=[], a14=[], a15=[], a16=[], a17=[], a18=[], a19=[], a20=[], a21=[], a22=[], a23=[];
while(item[i].fcstDate==CurDay){
if(item[i].category=='POP' || item[i].category=='TMP'){
a.push(item[i]);
}
i+=1;
}
let result=[];
i=0;
let Json;
while(i<a.length){
if(a[i].fcstValue<=8){
Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:0};
}
else if(a[i].fcstValue>8 && a[i].fcstValue<=12){
Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:1};
}
else if(a[i].fcstValue>12 && a[i].fcstValue<=18){
Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:2};
}
else if(a[i].fcstValue>18 && a[i].fcstValue<=23){
Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:3};
}
else{
Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:4};
};
result.push(Json);
i+=2;
}
res.json(result);
});//pop:강수확률 tmp:한시간 기온
});
app.post('/api/tomorrow',(req,res)=>{
const nx = req.body.dotX;
const ny = req.body.dotY;
var queryParams = '?' + encodeURIComponent('serviceKey') + '=3OcUyvx97Vx2YikiZ9IHyRQ6suapku7Xn8VlefQKQWrGIFOGaejhbevwagcubdHfSiQAqJwCV5lyIutw0%2BsppA%3D%3D'; /* Service Key*/
var url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst';
queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); /* */
queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('1000'); /* */
queryParams += '&' + encodeURIComponent('dataType') + '=' + encodeURIComponent('JSON'); /* */
queryParams += '&' + encodeURIComponent('base_date') + '=' + encodeURIComponent(base); /* */
queryParams += '&' + encodeURIComponent('base_time') + '=' + encodeURIComponent('2300'); /* */
queryParams += '&' + encodeURIComponent('nx') + '=' + encodeURIComponent(nx); /*nx*/
queryParams += '&' + encodeURIComponent('ny') + '=' + encodeURIComponent(ny); /*ny*/
request({
url: url + queryParams,
method: 'GET'
}, function (error, response, body) {
let ex=JSON.parse(body);
let item=ex.response.body.items.item;
let i=0;
let a=[];//, a4=[], a5=[], a6=[], a7=[], a8=[], a9=[], a10=[], a11=[], a12=[], a13=[], a14=[], a15=[], a16=[], a17=[], a18=[], a19=[], a20=[], a21=[], a22=[], a23=[];
while(item[i].fcstDate==tommorow||item[i].fcstDate==CurDay){
if((item[i].category=='POP' || item[i].category=='TMP') && item[i].fcstDate==tommorow){
a.push(item[i]);
}
i+=1;
}
let result=[];
i=0;
let Json;
while(i<a.length){
if(a[i].fcstValue<=8){
Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:0};
}
else if(a[i].fcstValue>8 && a[i].fcstValue<=12){
Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:1};
}
else if(a[i].fcstValue>12 && a[i].fcstValue<=18){
Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:2};
}
else if(a[i].fcstValue>18 && a[i].fcstValue<=23){
Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:3};
}
else{
Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:4};
};
result.push(Json);
i+=2;
}
res.json(result);
});//pop:강수확률 tmp:한시간 기온
});
var clothesSchema = new mongoose.Schema({
gender: Number,
weather: Number,
top: Array,
bottom:Array
});
var Clothes = mongoose.model('clothes', clothesSchema);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ limit: '1gb', extended: false }));
app.post('/api/clothes', (req, res) => {
Clothes.findOne({ gender: req.body.gender, weather: req.body.weather }, (err, clothes) => {
let randt=Math.floor(Math.random()*clothes.top.length);
let randb=Math.floor(Math.random()*clothes.bottom.length);
if (err) return res.status(500).json({ MatchingSuccess: false });
else if (clothes) {
if(req.body.rain==1) return res.status(200).json({top:"../../../../src/img/"+ clothes.top[randt]+".jpg",bottom:"../../../../src/img/"+ clothes.bottom[randb]+".jpg",umbrella:1,top1:clothes.top[randt],bottom1:clothes.bottom[randb]})
else return res.status(200).json({top:"../../../../src/img/"+ clothes.top[randt]+".jpg",bottom:"../../../../src/img/"+ clothes.bottom[randb]+".jpg",umbrella:0,top1:clothes.top[randt],bottom1:clothes.bottom[randb]});
}
else return res.status(404).json({ MatchingSuccess: false });
});
});
app.listen(4000, () => console.log('Server On 4000'));