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 00000000..81528c7e Binary files /dev/null and b/Weather_App/requirements.txt differ diff --git a/Weather_App/test_main.py b/Weather_App/test_main.py new file mode 100644 index 00000000..f287f85f --- /dev/null +++ b/Weather_App/test_main.py @@ -0,0 +1,50 @@ +import requests +from unittest.mock import patch +from main import get_weather + +mock_response_data = { + 'name': 'London', + 'sys': {'country': 'GB'}, + 'main': { + 'temp': 15.0, + 'feels_like': 13.0, + 'humidity': 82 + }, + 'weather': [{'description': 'light rain'}], + 'wind': {'speed': 4.1} +} + +@patch('main.requests.get') +def test_get_weather_success(mock_get): + mock_get.return_value.status_code = 200 + mock_get.return_value.json.return_value = mock_response_data + + result = get_weather('London', 'fake_api_key') + assert result == { + 'City': 'London', + 'Country': 'GB', + 'Temperature (°C)': 15.0, + 'Feels Like (°C)': 13.0, + 'Humidity (%)': 82, + 'Weather': 'Light Rain', + 'Wind Speed (m/s)': 4.1 + } + +@patch('main.requests.get') +def test_get_weather_http_error(mock_get): + mock_get.return_value.raise_for_status.side_effect = requests.exceptions.HTTPError("404 Client Error") + result = get_weather('InvalidCity', 'fake_api_key') + assert result is None + +@patch('main.requests.get') +def test_get_weather_request_exception(mock_get): + mock_get.side_effect = requests.exceptions.RequestException("Network error") + result = get_weather('London', 'fake_api_key') + assert result is None + +@patch('main.requests.get') +def test_get_weather_key_error(mock_get): + mock_get.return_value.status_code = 200 + mock_get.return_value.json.return_value = {} + result = get_weather('London', 'fake_api_key') + assert result is None \ No newline at end of file