Last active
April 16, 2019 13:29
-
-
Save tbanj/625c2a68254f1f79a86dc522bde336d6 to your computer and use it in GitHub Desktop.
api call for next day weather through the use of openweatherapi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ora = require('ora'); | |
const getWeather = require('../utils/weather'); | |
const getLocation = require('../utils/location'); | |
module.exports = async (args) => { | |
const spinner = ora().start(); | |
try { | |
const location = args.location || args._l || await getLocation(); | |
const weatherTomorrow = await getWeather.methodTomorrow(location); | |
// weather for tomorrow from zero hours which is for every 3 hours | |
let hourCount =0; | |
for (let index = 3; index < 11; index++) { | |
const weatherNow =weatherTomorrow.list[index]; | |
spinner.stop(); | |
var dateTomorrow = new Date(weatherNow.dt*1000); | |
let hourlyCheck =hourCount +3; | |
hourCount =hourlyCheck; | |
console.log(`Tomorrow date is ${dateTomorrow}, first ${hourlyCheck} hours temp. below`); | |
console.log(`current conditions in ${location.city}, ${location.country_code}:`); | |
const celciusTemp = weatherNow.main.temp - 273.15 ; | |
console.log(`\t${weatherNow.weather[0].description}, ${weatherNow.main.temp}F in ${celciusTemp.toFixed(2)}C`); | |
} | |
spinner.stop(); | |
} catch (error) { | |
console.error(error); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
module.exports = { | |
methodToday: checkToday, | |
methodTomorrow: checkTomorrow | |
} | |
async function checkToday(location) { | |
let result; | |
try { | |
result = await axios({ | |
method:'get', | |
url:`http://api.openweathermap.org/data/2.5/weather?q=${location.city},${location.country_code}&APPID=0e277eb5936797c1db082cc2a34d16a4` | |
}); | |
} catch (error) { | |
console.error(error); | |
} | |
return result.data; | |
} | |
async function checkTomorrow(location) { | |
let resultTomorrow; | |
try { | |
resultTomorrow = await axios({ | |
method:'get', | |
url:`http://api.openweathermap.org/data/2.5/forecast?q=${location.city},${location.country_code}&NG&APPID=dcc0cc3d866d4152fe43d79fdee8bfad` | |
}); | |
} catch (error) { | |
console.error(error); | |
} | |
return resultTomorrow.data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment