windio
Version:
A cli tool to get weather data.
41 lines (35 loc) ⢠1.14 kB
JavaScript
import axios from "axios";
import chalk from "chalk";
import spinner from "../helpers/spinner.js";
export async function getWeatherToday(city) {
if (!process.env.API_KEY) {
console.log(
"ā API Key not found. Use --apiKey <your-openWeatherApi-key> to save it.",
);
return;
}
if (!city) {
console.log(
"ā City not provided. Use --setCity <your-city-name> to specify a city.",
);
return;
}
try {
spinner.start();
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${process.env.API_KEY}`;
const response = await axios.get(apiUrl);
const data = response.data;
spinner.stop();
console.log(
chalk.blueBright(`\nš Location: ${data.name}, ${data.sys.country}`),
);
console.log(`š”ļø Temperature: ${data.main.temp}°C`);
console.log(`š„ļø Weather: ${data.weather[0].description}`);
console.log(`š§ Humidity: ${data.main.humidity}%\n`);
} catch (err) {
console.log(
"ā Failed to fetch weather. Please check the city name and API key.",
);
console.error(err.message);
}
}