-
Notifications
You must be signed in to change notification settings - Fork 1
/
weather.js
37 lines (32 loc) · 1.3 KB
/
weather.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
const axios = require('axios')
const appid = '736bbe5d8a6cc6ec2ebc1138ab70c706'
const getWeather = (q) => {
q.substring(q.toLowerCase().indexOf("weather in ") + 11)
return axios.get('http://api.openweathermap.org/data/2.5/weather', {
params: {
q,
appid,
units: 'metric'
}
})
.then(({ data }) => {
const compassSectors = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", "N"];
return 'Location: ' + data.name + ', ' + data.sys.country
+'\nTemperature: ' + data.main.temp + '°C'
+'\nSunrise: ' + convertUnixTime(data.sys.sunrise)
+'\nSunset: ' + convertUnixTime(data.sys.sunset)
+'\nWind Direction: ' + compassSectors[Math.round(data.wind.deg / 22.5)]
+'\nWind Speed: ' + data.wind.speed + ' km/h'
+'\nPressure: ' + data.main.pressure + ' mb'
+'\nHumidity: ' + data.main.humidity + '%';
})
}
const convertUnixTime = (unixTime) => {
let date = new Date(unixTime * 1000);
let hours = date.getHours();
let minutes = "0" + date.getMinutes();
//let seconds = "0" + date.getSeconds();
let formattedTime = hours + ':' + minutes.substr(-2)/* + ':' + seconds.substr(-2)*/;
return formattedTime;
}
module.exports = getWeather