-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
108 lines (96 loc) · 2.77 KB
/
router.js
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
const express = require("express");
const router = express.Router();
const axios = require('axios')
var apikey = 'LBEUVrpr04SsOsrodFB5ay5AIGjaWsdd';
router.get('/js/*.js', (req, res) => {
res.sendFile(__dirname + "/" + req.url);
});
router.get('/css/*.css', (req, res) => {
res.sendFile(__dirname + "/" + req.url);
})
router.get("/", (req, res) => {
res.sendFile(__dirname + '/index.html');
});
router.get("/chat", (req, res) => {
res.sendFile(__dirname + '/chat.html');
});
router.get("/search/:q", (req, resquest) => {
// console.log(req.params.q)
axios.get("http://dataservice.accuweather.com/locations/v1/cities/search", {
params: {
'apikey': apikey,
q: req.params.q,
}
}).then(res => {
if(res.data.length==0){
resquest.send(-1)
}
else{
resquest.send(res.data[0]['Key'])
}
}).catch(err => {
console.log(err)
})
});
router.get("/today/:q", (req, resquest) => {
axios.get("http://dataservice.accuweather.com/forecasts/v1/hourly/12hour/"+req.params.q, {
params: {
'apikey': apikey
}
}).then(res => {
if(res.data.length==0){
resquest.send(-1)
}
else{
let data = []
res.data.forEach(ele => {
let d={}
d['DateTime'] = ele['DateTime']
d['IconPhrase'] = ele['IconPhrase']
d['Temperature'] = ele['Temperature']['Value']+ele['Temperature']['Unit']
data.push(d);
});
resquest.send(data);
}
}).catch(err => {
console.log(err)
})
});
router.get("/indices/:q", (req, resquest) => {
axios.get(`http://dataservice.accuweather.com/indices/v1/daily/5day/${req.params.q}/groups/1` ,{
params: {
'apikey': apikey
}
}).then(res => {
if(res.data.length==0){
resquest.send(-1)
}
else{
let data = [];
let count = 0;
let t ;
let nums = res.data.length;
let dayitem = nums/5
for(let i=0;i<nums;i++){
let ele = res.data[i]
if(i%dayitem==0){
if(t){
data.push(t)
}
t = {day:ele['LocalDateTime'],datas:[]};
}
let d = {}
d['Name'] = ele['Name']
d['Value'] = ele['Value']
d['Category'] = ele['Category']
t['datas'].push(d);
}
data.push(t);
indice=data
resquest.send(data);
}
}).catch(err => {
console.log(err)
})
});
module.exports = router;