diff --git a/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/Dockerfile b/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/Dockerfile new file mode 100644 index 0000000..9719ff3 --- /dev/null +++ b/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/Dockerfile @@ -0,0 +1,15 @@ +# Base Image +FROM python:3.10 + +# Working directory +WORKDIR /app + +# Code +COPY . . + +# Install Libraries +RUN pip install -r requirement.txt + +# Run app +CMD ["python", "main.py"] + diff --git a/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/README.md b/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/README.md new file mode 100644 index 0000000..ef24863 --- /dev/null +++ b/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/README.md @@ -0,0 +1,68 @@ +```markdown +# 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. +- **User Input for City**: Now includes a web form for users to input their desired city, making it easier to get real-time weather alerts for specific locations. +- **Dynamic Web Application**: The application runs on Flask, serving a simple web interface to display alerts. + +## Getting Started + +### Prerequisites + +- Python 3.x +- `requests`, `python-dotenv`, and `Flask` libraries + +Install the required libraries using: + +```bash +pip install requests python-dotenv Flask +``` + +## 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` +- To run the web application, execute the following command: + +```bash +python main.py +``` + +- Access the application by navigating to `http://localhost:80` in your web browser. + +## Docker Setup + +To run the application using Docker: + +1. Ensure Docker is installed on your machine. +2. Build the Docker image: + +```bash +docker build -t weather-app . +``` + +3. Run the Docker container: + +```bash +docker run -it -p 80:80 -e OPEN_WEATHER_MAP_API_KEY=your_api_key -e CITY="Your City" -e TEMP_THRESHOLD=30 -e WIND_SPEED_THRESHOLD=10 weather-app +``` + +## 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. +- **Internal Server Error**: Ensure that the `index.html` file is present in the correct directory and that the Flask app is properly set up to render it. + +## Future Improvements + +- Consider deploying the application to a cloud platform for wider accessibility. +- Implement AJAX to allow for real-time updates without requiring a page refresh. +- Suggestion: Explore developing this application using Django for better structure and scalability. I would be happy to assist in making improvements in that framework. + diff --git a/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/main.py b/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/main.py new file mode 100644 index 0000000..976ce1f --- /dev/null +++ b/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/main.py @@ -0,0 +1,67 @@ +from flask import Flask, render_template, request +import requests +import os +import logging +from dotenv import load_dotenv + +app = Flask(__name__) + +# Load environment variables from .env file +load_dotenv() + +# Load API key from environment variable +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' + +# Set up basic logging +logging.basicConfig(level=logging.INFO) + +@app.route('/', methods=['GET', 'POST']) +def index(): + alerts = [] + if request.method == 'POST': + city = request.form.get('city', '').strip() + if city: + weather_data = fetch_weather(city) + alerts = check_alerts(weather_data) + return render_template('index.html', alerts=alerts) + +def fetch_weather(city): + """Fetches weather data for a given city.""" + url = f"{BASE_URL}?q={city}&appid={API_KEY}&units={UNIT}" + logging.info(f"Fetching weather for {city}: {url}") # Log the URL being called + response = requests.get(url) + if response.status_code == 200: + return response.json() + else: + logging.error(f"Error fetching data: {response.status_code}, {response.text}") # Log the error response + return None + +def check_alerts(data): + """Checks for temperature and wind speed alerts in weather data.""" + if not data or 'list' not in data or not data['list']: + return ["No data available to check alerts."] + + weather_info = data['list'][0] + temp = weather_info['main']['temp'] + wind_speed = weather_info['wind']['speed'] + + alerts = [] + temp_threshold = float(os.getenv('TEMP_THRESHOLD', 30.0)) # Default to 30°C + wind_speed_threshold = float(os.getenv('WIND_SPEED_THRESHOLD', 10.0)) # Default to 10 m/s + + 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") + + return alerts if alerts else ["No alerts. Weather conditions are normal."] + +if __name__ == "__main__": + app.run(host='0.0.0.0', port=80, debug=True) + diff --git a/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/requirement.txt b/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/requirement.txt new file mode 100644 index 0000000..c2594b2 --- /dev/null +++ b/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/requirement.txt @@ -0,0 +1,4 @@ +Flask +requests +python-dotenv + diff --git a/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/runtime.txt b/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/runtime.txt new file mode 100644 index 0000000..f023023 --- /dev/null +++ b/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/runtime.txt @@ -0,0 +1 @@ +python-3.10.7 \ No newline at end of file diff --git a/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/templates/index.html b/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/templates/index.html new file mode 100644 index 0000000..d99fd34 --- /dev/null +++ b/Weather Alert/Amitabh-DevOps/Weather-Alert-Dockerfile-Web/templates/index.html @@ -0,0 +1,24 @@ + + +
+ + +