-
Notifications
You must be signed in to change notification settings - Fork 11
/
wethr.js
executable file
·57 lines (44 loc) · 1.58 KB
/
wethr.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
52
53
54
55
56
57
#! /usr/bin/env node
const got = require('got')
const ora = require('ora')
const getEmoji = require('./get-emoji.js')
const chalk = require('chalk')
const API_KEY = '315bfb21a64943c67a92e2da0022fdbe'
const args = process.argv.slice(2)
let isMetric = true
if (args[0] === '--imperial') {
isMetric = false
}
const spinner = ora('Detecting your location').start()
function getLocation () {
return got('http://ip-api.com/json').then(response => {
const body = JSON.parse(response.body)
return body
})
.catch(error => {
spinner.stop()
console.log(chalk.red('Error talking with http://ip-api.com/.'))
console.log(chalk.red('Try running:\n\n $ curl http://ip-api.com/json\n\n'))
console.log(error)
})
}
function getTemperature ({ lat, lon, city, country }) {
spinner.color = 'yellow'
spinner.text = 'Loading weather'
const units = isMetric ? 'metric' : 'imperial'
const weatherURL = `http://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&APPID=${API_KEY}&units=${units}`
return got(weatherURL).then(response => {
spinner.stop()
const body = JSON.parse(response.body)
const temperature = body.main.temp
const units = isMetric ? 'C' : 'F'
const emoji = getEmoji(body.weather[0].description)
console.log(`${city}, ${country}: ${temperature}${units} ${emoji}`)
}).catch(error => {
spinner.stop()
console.log(chalk.red('Error talking with http://api.openweathermap.org/.'))
console.log(chalk.red(`Try running:\n\n $ curl ${weatherURL}\n\n`))
console.log(error)
})
}
getLocation().then(getTemperature)