-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (45 loc) · 1.35 KB
/
index.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
var request = require('request');
var moment=require('moment');
var Promise = require('bluebird');
exports.getWeatherHistory = function(lat,lon, timeIn,apiKey){
var time = moment(timeIn,'YYYY-MM-DD HH:mm');
var date = time.format('YYYY-MM-DD');
var hourIn = time.hour();
var query=lat +',' + lon;
var url="https://api.apixu.com/v1/history.json?key=" + apiKey + " &q="+ query +"&dt=" + date;
return new Promise(function(resolve, reject){
request(url, function (error, response, body) {
if (!isObject(body.forecast)){
var weather={};
weather.temp_c=15;
weather.isValid=false;
resolve(weather); //set default temp to 15 if unknown
}else{
var history = body.forecast;
var hourArray =history.forecastday.hour;
var final;
var weather={};
for(var i in hourArray){
var obj = hourArray[i];
var objOut={};
var time = moment(obj.time,'YYYY-MM-DD HH:mm');//2017-07-20 23:00
objOut.isValid=true;
objOut.time =time.toDate();
objOut.hour = time.hour();
objOut.temp_c=obj.temp_c;
objOut.feelslike_c=obj.feelslike_c;
objOut.humidity = obj.humidity;
objOut.wind_kph = obj.wind_kph;
objOut.wind_degree = obj.wind_degree;
if (objOut.hour==hourIn){
weather = objOut;
}
}
resolve(weather);
}
});
});//
}
function isObject(obj) {
return obj === Object(obj);
}