-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp-practice.js
More file actions
69 lines (59 loc) · 1.92 KB
/
Copy pathapp-practice.js
File metadata and controls
69 lines (59 loc) · 1.92 KB
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
// const yargs = require('yargs');
// const axios = require('axios');
// const geocode = require('./geocode/geocode.js');
// const weather = require('./weather/weather.js');
// const argv = yargs
// .options({
// a :
// {
// alias:'address',
// demand : true,
// describe:'finding location for the given address'
// }
// }
// ).alias('h','help')
// .argv;
// var encodedURL = encodeURIComponent(argv.address);
// axios.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${encodedURL}`)
// .then((response)=>{
// console.log(response.data);
// }).catch((errorMsg)=>{
// if(errorMsg)
// console.log(errorMsg);
// })
const yargs = require('yargs');
const axios = require('axios');
const argv = yargs
.options({
a: {
demand: true,
alias: 'address',
describe: 'Address to fetch weather for',
string: true
}
})
.help()
.alias('help', 'h')
.argv;
var encodedAddress = encodeURIComponent(argv.address);
var geocodeUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodedAddress}`;
axios.get(geocodeUrl).then((response) => {
if (response.data.status === 'ZERO_RESULTS') {
throw new Error('Unable to find that address.');
}
var lat = response.data.results[0].geometry.location.lat;
var lng = response.data.results[0].geometry.location.lng;
var weatherUrl = `https://api.forecast.io/forecast/4a04d1c42fd9d32c97a2c291a32d5e2d/${lat},${lng}`;
console.log(response.data.results[0].formatted_address);
return axios.get(weatherUrl);
}).then((response) => {
var temperature = response.data.currently.temperature;
var apparentTemperature = response.data.currently.apparentTemperature;
console.log(`It's currently ${temperature}. It feels like ${apparentTemperature}.`);
}).catch((e) => {
if (e.code === 'ENOTFOUND') {
console.log('Unable to connect to API servers.');
} else {
console.log(e.message);
}
});