-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
51 lines (46 loc) · 1.73 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
43
44
45
46
47
48
49
50
51
const yargs = require('yargs')
const axios = require('axios')
require('dotenv').config()
const API_KEY = process.env.API_KEY
const argv = yargs
.options({
a: {
alias: 'address',
describe: 'Address to fetch weather for. If omitted the program will use location based on IP address.',
string: true
}
})
.help()
.alias('help', 'h')
.argv
axios.get('http://ipinfo.io').then((response) => {
if (argv.address === undefined) {
argv.address = response.data.city
}
var encodedAddress = encodeURIComponent(argv.address)
var locationUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodedAddress}`
return axios.get(locationUrl)
}).then((response) => {
if (response.data.status === 'ZERO_RESULTS') {
throw new Error('Unable to find the address. Try again.')
}
var address = response.data.results[0].formatted_address
var latitude = response.data.results[0].geometry.location.lat
var longitude = response.data.results[0].geometry.location.lng
var weatherUrl = `https://api.darksky.net/forecast/${API_KEY}/${latitude},${longitude}?units=si`
console.log(`Getting weather for ${address}`)
return axios.get(weatherUrl)
}).then((response) => {
var currently = response.data.currently
console.log(`Current temperature is ${currently.temperature}\u00b0C`)
console.log(`Although it feels like ${currently.apparentTemperature}\u00b0C`)
console.log(`It's mostly ${currently.summary} with humidity at ${currently.humidity}`)
}).catch((error) => {
if (error.code === 'ENOTFOUND') {
console.log('Unable to connect to the server. Please try again later.')
} else if (error.response.status === 404) {
console.log('Unable to fetch weather.')
} else {
console.log(error.message)
}
})