Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env
node_modules
tempCodeRunnerFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Weather CLI

A simple command-line tool that fetches real-time weather data for any city using the OpenWeatherMap API.

## What it does

You enter a city name, and it returns the current weather status including temperature, humidity, weather condition, and country.

## Setup

**1. Clone the repo and install dependencies**
```bash
npm install
```

> This project uses `prompt-sync`for user input and `dotenv` for environment variables.

**2. Get an API key**

Create a free account at [openweathermap.org](https://openweathermap.org/api) and grab your API key.

**3. Create `.env` file and put you api key**
```
API_KEY=your_api_key_here
```

**4. Run the app**
```bash
node index.js
```

## Example

```
enter a city name to get its weather status: Algiers
{
country: 'DZ',
city: 'Algiers',
weather: 'Clear',
temp: '18°C',
humidity: '72%'
}
```

## Notes

- Handles API errors (invalid key, city not found, server issues).
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require('dotenv').config();
const prompt = require('prompt-sync')({ sigint: true }); // package for getting input



const API_KEY = process.env.API_KEY // api_key
let city_name; // initial declare
do {
city_name = prompt("enter a city name to get its weather status: ");

if (!city_name) {
console.log("city name cannot be empty. Try again");
}}while (!city_name) // while empty


//fetching data
async function getWeather() {
try {
if (!API_KEY) {
throw new Error("API-KEY is missing !!! check env file!!!!!!!")
}
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city_name}&units=${"metric"}&appid=${API_KEY}`)
if (response.status === 401 || response.status === 403){
throw new Error("INVALID API KEY!!")
}
if (response.status === 504) {
throw new Error("Server error, try again later")
}
if (response.status === 408) {
throw new Error("slow or unstable client internet connection, check your internet")
}

const data = await response.json()
console.log(data)
if (response.status === 404) // if city name was wrong
{
throw new Error("Invalid city name")
}
if (!response.ok) {
throw new Error(`Unexpected error: ${response.status}`)
}
const country = data['sys']['country']
city_name = data.name //ktacheft bli t9der dir . fi plaset ['']
const weather = data['weather'][0]['main']
const temp = data['main']['temp']
const humidity = data['main']['humidity']
const weather_status = {
"country" : country,
"city" : city_name,
"weather" : weather,
"temp" : `${temp}°C`,
"humidity" : `${humidity}%`
}
console.log(weather_status)
}catch (error){
console.error("Something went wrong: ", error.message)
}
}


getWeather() // dédicace l Anouar



Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "weather_api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs",
"dependencies": {
"dotenv": "^17.3.1"
}
}