1
0
This commit is contained in:
Jason Williams 2019-09-29 12:36:49 -06:00
parent ef64250820
commit 5ffdfbbb98
3 changed files with 11 additions and 11 deletions

View File

@ -6,17 +6,17 @@ const address = process.argv[2]
if (!address) { if (!address) {
console.log('Please provide an address') console.log('Please provide an address')
} else { } else {
geocode(address, (error, data) => { geocode(address, (error, {latitude, longitude, location}) => {
if (error) { if (error) {
return console.log(error) return console.log(error)
} }
forecast(data.latitude, data.longitude, (error, forecastData) => { forecast(latitude, longitude, (error, forecastData) => {
if (error) { if (error) {
return console.log(error) return console.log(error)
} }
console.log(data.location) console.log(location)
console.log(forecastData) console.log(forecastData)
}) })
}) })

View File

@ -3,13 +3,13 @@ const request = require('request')
const forecast = (latitude, longitude, callback) => { const forecast = (latitude, longitude, callback) => {
const url = 'https://api.darksky.net/forecast/0466bccb953c0b9daeb091a529da2c0d/' + latitude + ',' + longitude + '?units=si' const url = 'https://api.darksky.net/forecast/0466bccb953c0b9daeb091a529da2c0d/' + latitude + ',' + longitude + '?units=si'
request({ url: url, json: true }, (error, response) => { request({ url, json: true }, (error, {body}) => {
if (error) { if (error) {
callback('Unable to connect to weather service!', undefined) callback('Unable to connect to weather service!', undefined)
} else if (response.body.error) { } else if (body.error) {
callback('Unable to find location', undefined) callback('Unable to find location', undefined)
} else { } else {
callback(undefined, response.body.daily.data[0].summary + ' It is currently ' + response.body.currently.temperature + ' degrees out. There is a ' + response.body.currently.precipProbability + '% chance of rain.') callback(undefined, body.daily.data[0].summary + ' It is currently ' + body.currently.temperature + ' degrees out. There is a ' + body.currently.precipProbability + '% chance of rain.')
} }
}) })
} }

View File

@ -3,16 +3,16 @@ const request = require('request')
const geocode = (address, callback) => { const geocode = (address, callback) => {
const url = 'https://api.mapbox.com/geocoding/v5/mapbox.places/' + encodeURIComponent(address) + '.json?access_token=pk.eyJ1IjoiamF5d2xsIiwiYSI6ImYyZjkyMGNlOTgyNGQ5ZWM5ZDExN2FjNGYwMDZmNTQ4In0.y3lv5ddmGhVNSL7wfMpB0g&limit=1' const url = 'https://api.mapbox.com/geocoding/v5/mapbox.places/' + encodeURIComponent(address) + '.json?access_token=pk.eyJ1IjoiamF5d2xsIiwiYSI6ImYyZjkyMGNlOTgyNGQ5ZWM5ZDExN2FjNGYwMDZmNTQ4In0.y3lv5ddmGhVNSL7wfMpB0g&limit=1'
request({ url: url, json: true }, (error, response) => { request({ url, json: true }, (error, {body}) => {
if (error) { if (error) {
callback('Unable to connect to location services!', undefined) callback('Unable to connect to location services!', undefined)
} else if (response.body.features.length === 0) { } else if (body.features.length === 0) {
callback('Unable to find location. Try another search.', undefined) callback('Unable to find location. Try another search.', undefined)
} else { } else {
callback(undefined, { callback(undefined, {
latitude: response.body.features[0].center[1], latitude: body.features[0].center[1],
longitude: response.body.features[0].center[0], longitude: body.features[0].center[0],
location: response.body.features[0].place_name location: body.features[0].place_name
}) })
} }
}) })