-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-promise.js
51 lines (46 loc) · 1.58 KB
/
app-promise.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
const fs = require("fs");
const yargs = require("yargs");
const axios = require("axios");
const googleKeyFileName = "C:\\Development\\googlekey.txt";
const weatherKeyFileName = "C:\\Development\\weatherkey.txt";
const argv = yargs
.options({
address: {
demand: true,
alias: "a",
describe: "Address to fetch weather for",
string: true
}
})
.help()
.alias("help", "h").argv;
const searchTerm = encodeURIComponent(argv.a);
const googleKey = fs.readFileSync(googleKeyFileName);
const geocodeUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${searchTerm}&key=${googleKey}`;
axios
.get(geocodeUrl)
.then(response => {
if (response.data.status === "ZERO_RESULTS") {
throw new Error("Unable to find that address");
}
var latitude = response.data.results[0].geometry.location.lat;
var longitude = response.data.results[0].geometry.location.lng;
const weatherKey = fs.readFileSync(weatherKeyFileName);
const weatherUrl = `https://api.darksky.net/forecast/${weatherKey}/${latitude},${longitude}?units=ca`;
console.log(response.data.results[0].formatted_address);
return axios.get(weatherUrl);
})
.then(response => {
const temperature = response.data.currently.temperature;
const apparentTemperature = response.data.currently.apparentTemperature;
console.log(
`It's currently ${temperature}. It feels like it's ${apparentTemperature}`
);
})
.catch(e => {
if (e.code === "ENOTFOUND") {
console.log("Unable to connect to API servers");
} else {
console.log(e.message);
}
});