Skip to content

Instantly share code, notes, and snippets.

@anubhavgirdhar
Created September 1, 2019 09:12
Show Gist options
  • Save anubhavgirdhar/3cfbec5900a2d8f1f6f6c473737caf78 to your computer and use it in GitHub Desktop.
Save anubhavgirdhar/3cfbec5900a2d8f1f6f6c473737caf78 to your computer and use it in GitHub Desktop.
weather-app
const forecast = require('./utils/forecast')
const geocode = require('./utils/geocode')
const address = process.argv[2]
if (!address){
console.log("Please provide a valid address")
} else{
console.log(geocode(address,(error,{Latitude,Longitude,Location}) => {
if ( error ) {
console.log('error',error)
} else{
console.log("Location : " + Location)
}
forecast(Latitude,Longitude,(error,forecastData) => {
if (error){
console.log('error',error)
}else{
console.log(forecastData)
}
})
}))
}
const request = require('request');
const forecast = (Latitude,Longitude,callback) => {
const url="https://api.darksky.net/forecast/d30fc4010a9e70fcb9462595ada4233c/"+encodeURIComponent(Latitude)+","+encodeURIComponent(Longitude)+"?units=si"
request({url,json : true},(error,{body}) => {
if (error){
callback('Unable to reach weather service!', undefined)
}else if (body.code ===400){
callback("Unable to find location.Please try again",undefined)
} else{
callback(undefined,"Summary for today : " + body.currently.summary + ". The current temperature is " + body.currently.temperature
)
}
})
}
module.exports = forecast
const request = require('request');
const geocode = (address,callback) => {
const url="https://api.mapbox.com/geocoding/v5/mapbox.places/"+encodeURIComponent(address)+".json?access_token=pk.eyJ1IjoiYW51YmhhdmdpcmRoYXIiLCJhIjoiY2p6eWllZzdpMTE3NTNkcGtiN294ZnVtaSJ9.lTeS2jOgAql7IRUeb3X_oA"
request({url,json : true},(error,{body}) => {
if (error) {
callback ('Unable to reach weather service!', undefined)
}else if (body.features.length ===0){
callback("Unable to find location.Please try again",undefined)
} else{
callback(undefined,{
Latitude : body.features[0].center[1],
Longitude : body.features[0].center[0],
Location : body.features[0].place_name
})
}
})
// Needs a return statement here, otherwise prints "undefined"
}
module.exports = geocode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment