-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
42 lines (39 loc) · 1.31 KB
/
app.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
const yargs = require('yargs');
const axios = require('axios');
const ftoc = require('f-c');
var argv = yargs
.options({
address : {
alias:'a',
demand: true,
describe:"enter address to get the temperature",
string: true
}
})
.help()
.alias('help', 'h')
.argv;
var encodedAddress = encodeURIComponent(argv.address);
var locationUrl = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + encodedAddress;
axios.get(locationUrl).then((response) => {
if(response.data.status === 'ZERO_RESULTS'){
throw new Error('Unable to find the address');
}
var lat= response.data.results[0].geometry.location.lat;
var lng= response.data.results[0].geometry.location.lng;
var weatherUrl = 'https://api.darksky.net/forecast/7ccd66aefdfde2cb09e8b8fdec36137a/' +lat +',' + lng;
console.log('Address',response.data.results[0].formatted_address);
return axios.get(weatherUrl);
})
.then((response) => {
var temperature = Math.round(ftoc(response.data.currently.temperature)*100)/100;
var apparentTemperature = Math.round(ftoc(response.data.currently.apparentTemperature)*100)/100;
console.log(`The temperature is ${temperature} celcius. But is feels like ${apparentTemperature} celcius`);
})
.catch((error) => {
if(error.code === 'ENOTFOUND'){
console.log("Unable to connect to server");
}else{
console.log(e.message);
}
})