From 20fba31fa4a0489a4c2f642276e99eceb33a9db6 Mon Sep 17 00:00:00 2001 From: shra012 Date: Sat, 12 Apr 2025 10:55:45 -0700 Subject: [PATCH] Added Weather App --- Weather_App/main.py | 51 +++++++++++++++++++++++++++++++++++ Weather_App/readme.md | 32 ++++++++++++++++++++++ Weather_App/requirements.txt | Bin 0 -> 34 bytes Weather_App/test_main.py | 50 ++++++++++++++++++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 Weather_App/main.py create mode 100644 Weather_App/readme.md create mode 100644 Weather_App/requirements.txt create mode 100644 Weather_App/test_main.py diff --git a/Weather_App/main.py b/Weather_App/main.py new file mode 100644 index 00000000..ecf7ec17 --- /dev/null +++ b/Weather_App/main.py @@ -0,0 +1,51 @@ +import requests + +def get_weather(city_name, api_key): + base_url = "https://api.openweathermap.org/data/2.5/weather" + params = { + 'q': city_name, + 'appid': api_key, + 'units': 'metric' # Use 'imperial' for Fahrenheit + } + + try: + response = requests.get(base_url, params=params) + response.raise_for_status() + data = response.json() + + # Extract relevant data + weather = { + 'City': data['name'], + 'Country': data['sys']['country'], + 'Temperature (°C)': data['main']['temp'], + 'Feels Like (°C)': data['main']['feels_like'], + 'Humidity (%)': data['main']['humidity'], + 'Weather': data['weather'][0]['description'].title(), + 'Wind Speed (m/s)': data['wind']['speed'] + } + + return weather + + except requests.exceptions.HTTPError as http_err: + print(f"HTTP error occurred: {http_err}") + except requests.exceptions.RequestException as err: + print(f"Error occurred: {err}") + except KeyError: + print("Invalid response received from the API.") + return None + +def main(): + api_key = input("Enter your OpenWeatherMap API key: ").strip() + city_name = input("Enter the city name: ").strip() + + weather_info = get_weather(city_name, api_key) + + if weather_info: + print("\nCurrent Weather Information:") + for key, value in weather_info.items(): + print(f"{key}: {value}") + else: + print("Failed to retrieve weather data.") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/Weather_App/readme.md b/Weather_App/readme.md new file mode 100644 index 00000000..39e6653f --- /dev/null +++ b/Weather_App/readme.md @@ -0,0 +1,32 @@ +# Simple Weather App + +## Project Overview +- **Purpose:** This is a simple weather app created to foster the understanding of the fundamentals of Weather API. +- **Tools Used:** Python, requests library, OpenWeatherMap API. +- **API Used:** OpenWeatherMap’s Current Weather Data API. + +## Setup Instructions +1. Installing Requirements +```bash +pip install requests +``` +2. Obtain an API Key: +Sign up at [OpenWeatherMap](https://openweathermap.org/api) to get your free API key. + +Project Structure +```bash +├── main.py +├── readme.md +├── requirements.txt +└── test_main.py +``` + +## Running the app +```bash +python main.py +``` + +## Running tests +```bash +pytest +``` \ No newline at end of file diff --git a/Weather_App/requirements.txt b/Weather_App/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..81528c7e28b7d447d8f638dcce7453a97d9a490b GIT binary patch literal 34 hcmezWuZSU)p^%{zNES1c05LBE7efI