From ee7715e15b682666655a821aedae52c6ec42b44a Mon Sep 17 00:00:00 2001 From: Prasanta Bandyopadhyay Date: Fri, 25 Oct 2024 15:19:37 +0000 Subject: [PATCH 1/2] Added Dockerfile and changed the folder name --- Weather_Alert/.env.example | 1 + Weather_Alert/Dockerfile | 23 +++++++++ Weather_Alert/README.md | 38 +++++++++++++++ Weather_Alert/main.py | 88 +++++++++++++++++++++++++++++++++++ Weather_Alert/requirement.txt | 2 + Weather_Alert/runtime.txt | 1 + 6 files changed, 153 insertions(+) create mode 100644 Weather_Alert/.env.example create mode 100644 Weather_Alert/Dockerfile create mode 100644 Weather_Alert/README.md create mode 100644 Weather_Alert/main.py create mode 100644 Weather_Alert/requirement.txt create mode 100644 Weather_Alert/runtime.txt diff --git a/Weather_Alert/.env.example b/Weather_Alert/.env.example new file mode 100644 index 0000000..288a952 --- /dev/null +++ b/Weather_Alert/.env.example @@ -0,0 +1 @@ +OPEN_WEATHER_MAP_API_KEY="bd5e378503939ddaee76f12ad7a97608" diff --git a/Weather_Alert/Dockerfile b/Weather_Alert/Dockerfile new file mode 100644 index 0000000..bbc2a1a --- /dev/null +++ b/Weather_Alert/Dockerfile @@ -0,0 +1,23 @@ +#BASEIMAGE + +FROM python:3.7 + +#WORKDIR + +WORKDIR /app + +#COPY CODE + +COPY . /app + +#INSTALL LIBRARIES + +RUN pip install requests python-dotenv +RUN pip install -r requirement.txt + +#RUN THE APP + +CMD ["python","main.py"] + + +EXPOSE 5001 diff --git a/Weather_Alert/README.md b/Weather_Alert/README.md new file mode 100644 index 0000000..ab0149b --- /dev/null +++ b/Weather_Alert/README.md @@ -0,0 +1,38 @@ +# Weather Alert Script 🌦️ + +A simple Python script that fetches weather data for your city and alerts you if the temperature or wind speed crosses your set thresholds. Perfect for staying on top of weather changes and preparing for unexpected conditions! 🚀 + +## Features + +- **Fetch Weather Data**: Retrieves up-to-date weather information for any city using the OpenWeatherMap API. +- **Custom Alerts**: Set your own temperature and wind speed thresholds. If the weather conditions exceed these, the script will alert you! +- **Hourly Updates**: The script automatically checks the weather every hour, so you’re always in the loop. + +## Getting Started + +### Prerequisites + +- Python 3.x +- `requests` and `python-dotenv` libraries + +Install the required libraries using: + +```bash +pip install requests python-dotenv +``` + +## Setup + +- Clone or download this repository. +- Get an API key from OpenWeatherMap. (It’s free!) +- Create a .env file in the root directory and add your API key like shown in `.env.example` +- Run the script + +``` +python weather_alert.py +``` + +## Troubleshooting + +- Missing API Key Error: Double-check that your .env file contains the correct OPEN_WEATHER_MAP_API_KEY. Make sure there are no typos or extra spaces. +- Error fetching data: This could happen if the API key is incorrect or if the city name is misspelled. Verify both and try again. diff --git a/Weather_Alert/main.py b/Weather_Alert/main.py new file mode 100644 index 0000000..2d9b16e --- /dev/null +++ b/Weather_Alert/main.py @@ -0,0 +1,88 @@ +import requests +import json +import time +import os +from dotenv import load_dotenv + +# Load environment variables from .env file +load_dotenv() + +# Load API key from environment variable (security fix) +API_KEY = os.getenv('OPEN_WEATHER_MAP_API_KEY') + +if not API_KEY: + raise ValueError("Missing environment variable 'OPEN_WEATHER_MAP_API_KEY'") + +UNIT = 'metric' +BASE_URL = 'https://api.openweathermap.org/data/2.5/find' + + +def fetch_weather(city): + """Fetches weather data for a given city. + + Args: + city (str): Name of the city. + + Returns: + dict: Weather data if successful, None otherwise. + """ + + url = f"{BASE_URL}?q={city}&appid={API_KEY}&units={UNIT}" + response = requests.get(url) + + if response.status_code == 200: + return response.json() + else: + print(f"Error fetching data: {response.status_code}") + return None + + +def check_alerts(data, temp_threshold, wind_speed_threshold): + """Checks for temperature and wind speed alerts in weather data. + + Args: + data (dict): Weather data. + temp_threshold (float): Temperature threshold in °C. + wind_speed_threshold (float): Wind speed threshold in m/s. + + Prints alerts if any, otherwise prints a message indicating normal weather conditions. + """ + + if not data or 'list' not in data or not data['list']: + print("No data available to check alerts.") + return + + weather_info = data['list'][0] + temp = weather_info['main']['temp'] + wind_speed = weather_info['wind']['speed'] + + alerts = [] + if temp > temp_threshold: + alerts.append(f"Temperature alert! Current temperature: {temp}°C") + if wind_speed > wind_speed_threshold: + alerts.append(f"Wind speed alert! Current wind speed: {wind_speed} m/s") + + if alerts: + print("\n".join(alerts)) + else: + print("No alerts. Weather conditions are normal.") + + +def main(): + """Prompts user for city name, temperature and wind speed thresholds, + and continuously checks for alerts. + """ + + city = input("Enter city name: ") + temp_threshold = float(input("Enter temperature threshold (°C): ")) + wind_speed_threshold = float(input("Enter wind speed threshold (m/s): ")) + + while True: + weather_data = fetch_weather(city) + check_alerts(weather_data, temp_threshold, wind_speed_threshold) + print("Waiting for the next check...") + time.sleep(3600) # check every hour + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/Weather_Alert/requirement.txt b/Weather_Alert/requirement.txt new file mode 100644 index 0000000..d44fe44 --- /dev/null +++ b/Weather_Alert/requirement.txt @@ -0,0 +1,2 @@ +requests +python-dotenv \ No newline at end of file diff --git a/Weather_Alert/runtime.txt b/Weather_Alert/runtime.txt new file mode 100644 index 0000000..f023023 --- /dev/null +++ b/Weather_Alert/runtime.txt @@ -0,0 +1 @@ +python-3.10.7 \ No newline at end of file From e0e0ac29c6e09c27d81504fafb0c1934af1a51ae Mon Sep 17 00:00:00 2001 From: Prasanta Bandyopadhyay <141817654+prasantabdev@users.noreply.github.com> Date: Fri, 25 Oct 2024 20:55:44 +0530 Subject: [PATCH 2/2] Update Dockerfile --- Weather_Alert/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Weather_Alert/Dockerfile b/Weather_Alert/Dockerfile index bbc2a1a..114e664 100644 --- a/Weather_Alert/Dockerfile +++ b/Weather_Alert/Dockerfile @@ -20,4 +20,4 @@ RUN pip install -r requirement.txt CMD ["python","main.py"] -EXPOSE 5001 +