-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
101 lines (88 loc) · 3.54 KB
/
server.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
const express = require('express')
const app = express()
const request = require('request');
app.get('/hi', function (req, res) {
var city;
if (req.query.city){
city = req.query.city;
}
else
{
city = "london";
}
var yahooweather = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22' + city + '%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
request(yahooweather, function (error, response, body) {
var weatherJson = {}
if (error){
response.status(500);
}
else {
weatherJson = JSON.parse(body).query.results.channel.item.forecast[0];
weatherJson.location = JSON.parse(body).query.results.channel.location;
weatherJson.type = 0;
weatherJson.speech = "Yo it looks like today in " + weatherJson.location.city + " will be a high of " +
weatherJson.high + " and a low of " + weatherJson.low + ". " +
getDressCode(weatherJson);
weatherJson.displayText = weatherJson.speech;
weatherJson.data = {};
weatherJson.contextOut = [ ];
weatherJson.source = " Our weather App"
res.json(weatherJson);
}
})
});
app.post('/dress', function (req, res) {
var city;
if (req.params.city){
city = req.params.city;
}
else
{
city = "london";
}
var yahooweather = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22' + city + '%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
request(yahooweather, function (error, response, body) {
var weatherJson = {}
if (error){
response.status(500);
}
else {
weatherJson = JSON.parse(body).query.results.channel.item.forecast[0];
weatherJson.location = JSON.parse(body).query.results.channel.location;
weatherJson.type = 0;
weatherJson.speech = "Yo it looks like today in " + weatherJson.location.city + " it will be a high of " +
weatherJson.high + " and a low of " + weatherJson.low + ". " +
getDressCode(weatherJson);
weatherJson.displayText = weatherJson.speech;
weatherJson.data = {};
weatherJson.contextOut = [ ];
weatherJson.source = " Our weather App";
res.json(weatherJson);
}
})
});
const server = app.listen(process.env.PORT || 3000, function () {
const port = server.address().port ;
console.log('Example app listening on port ' + port);
})
function getDressCode(weatherJson){
var dressCode = "";
var weather = weatherJson.text;
switch (weather) {
case 'Sunny':
dressCode ="Looks like it's going to be sunny today. Make sure to pack your sun care products";
break;
case 'Cloudy':
dressCode ="Looks like it's going to be cloudy today!";
break;
case 'Rain':
dressCode ="Looks like it's going to be rainy today!. Dont forget your umbrella";
break;
case 'Scatterd Showers':
weatherJson.speech ="Looks like it's going to be scattered showers today!. Dont forget your umbrella";
break;
default:
dressCode = "Don't forget to dress for the weather";
}
return dressCode;
}