diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0907fef --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.env +node_modules +tempCodeRunnerFile.js \ No newline at end of file diff --git a/web/Learning-Season/03-backend/challenges/ode.js & Modern JavaScript/solution/README.md b/web/Learning-Season/03-backend/challenges/ode.js & Modern JavaScript/solution/README.md new file mode 100644 index 0000000..ee39919 --- /dev/null +++ b/web/Learning-Season/03-backend/challenges/ode.js & Modern JavaScript/solution/README.md @@ -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). \ No newline at end of file diff --git a/web/Learning-Season/03-backend/challenges/ode.js & Modern JavaScript/solution/index.js b/web/Learning-Season/03-backend/challenges/ode.js & Modern JavaScript/solution/index.js new file mode 100644 index 0000000..8278866 --- /dev/null +++ b/web/Learning-Season/03-backend/challenges/ode.js & Modern JavaScript/solution/index.js @@ -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 + + + diff --git a/web/Learning-Season/03-backend/challenges/ode.js & Modern JavaScript/solution/package-lock.json b/web/Learning-Season/03-backend/challenges/ode.js & Modern JavaScript/solution/package-lock.json new file mode 100644 index 0000000..4719a9c --- /dev/null +++ b/web/Learning-Season/03-backend/challenges/ode.js & Modern JavaScript/solution/package-lock.json @@ -0,0 +1,28 @@ +{ + "name": "weather_api", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "weather_api", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "dotenv": "^17.3.1" + } + }, + "node_modules/dotenv": { + "version": "17.3.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.3.1.tgz", + "integrity": "sha512-IO8C/dzEb6O3F9/twg6ZLXz164a2fhTnEWb95H23Dm4OuN+92NmEAlTrupP9VW6Jm3sO26tQlqyvyi4CsnY9GA==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + } + } +} diff --git a/web/Learning-Season/03-backend/challenges/ode.js & Modern JavaScript/solution/package.json b/web/Learning-Season/03-backend/challenges/ode.js & Modern JavaScript/solution/package.json new file mode 100644 index 0000000..c3cb5e3 --- /dev/null +++ b/web/Learning-Season/03-backend/challenges/ode.js & Modern JavaScript/solution/package.json @@ -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" + } +}