|
| 1 | +from flask import Flask, render_template, request |
1 | 2 | import requests |
2 | | -import json |
3 | | -import time |
4 | 3 | import os |
| 4 | +import logging |
5 | 5 | from dotenv import load_dotenv |
6 | 6 |
|
| 7 | +app = Flask(__name__) |
| 8 | + |
7 | 9 | # Load environment variables from .env file |
8 | 10 | load_dotenv() |
9 | 11 |
|
|
16 | 18 | UNIT = 'metric' |
17 | 19 | BASE_URL = 'https://api.openweathermap.org/data/2.5/find' |
18 | 20 |
|
19 | | -# Load additional environment variables |
20 | | -CITY = os.getenv('CITY', 'London') # Default to 'London' if not provided |
21 | | -TEMP_THRESHOLD = float(os.getenv('TEMP_THRESHOLD', 30.0)) # Default to 30°C |
22 | | -WIND_SPEED_THRESHOLD = float(os.getenv('WIND_SPEED_THRESHOLD', 10.0)) # Default to 10 m/s |
| 21 | +# Set up basic logging |
| 22 | +logging.basicConfig(level=logging.INFO) |
23 | 23 |
|
| 24 | +@app.route('/', methods=['GET', 'POST']) |
| 25 | +def index(): |
| 26 | + alerts = [] |
| 27 | + if request.method == 'POST': |
| 28 | + city = request.form.get('city', '').strip() |
| 29 | + if city: |
| 30 | + weather_data = fetch_weather(city) |
| 31 | + alerts = check_alerts(weather_data) |
| 32 | + return render_template('index.html', alerts=alerts) |
24 | 33 |
|
25 | 34 | def fetch_weather(city): |
26 | 35 | """Fetches weather data for a given city.""" |
27 | 36 | url = f"{BASE_URL}?q={city}&appid={API_KEY}&units={UNIT}" |
| 37 | + logging.info(f"Fetching weather for {city}: {url}") # Log the URL being called |
28 | 38 | response = requests.get(url) |
29 | 39 | if response.status_code == 200: |
30 | 40 | return response.json() |
31 | 41 | else: |
32 | | - print(f"Error fetching data: {response.status_code}") |
| 42 | + logging.error(f"Error fetching data: {response.status_code}, {response.text}") # Log the error response |
33 | 43 | return None |
34 | 44 |
|
35 | | - |
36 | | -def check_alerts(data, temp_threshold, wind_speed_threshold): |
| 45 | +def check_alerts(data): |
37 | 46 | """Checks for temperature and wind speed alerts in weather data.""" |
38 | 47 | if not data or 'list' not in data or not data['list']: |
39 | | - print("No data available to check alerts.") |
40 | | - return |
| 48 | + return ["No data available to check alerts."] |
41 | 49 |
|
42 | 50 | weather_info = data['list'][0] |
43 | 51 | temp = weather_info['main']['temp'] |
44 | 52 | wind_speed = weather_info['wind']['speed'] |
45 | 53 |
|
46 | 54 | alerts = [] |
| 55 | + temp_threshold = float(os.getenv('TEMP_THRESHOLD', 30.0)) # Default to 30°C |
| 56 | + wind_speed_threshold = float(os.getenv('WIND_SPEED_THRESHOLD', 10.0)) # Default to 10 m/s |
| 57 | + |
47 | 58 | if temp > temp_threshold: |
48 | 59 | alerts.append(f"Temperature alert! Current temperature: {temp}°C") |
49 | 60 | if wind_speed > wind_speed_threshold: |
50 | 61 | alerts.append(f"Wind speed alert! Current wind speed: {wind_speed} m/s") |
51 | 62 |
|
52 | | - if alerts: |
53 | | - print("\n".join(alerts)) |
54 | | - else: |
55 | | - print("No alerts. Weather conditions are normal.") |
56 | | - |
57 | | - |
58 | | -def main(): |
59 | | - """Continuously checks for weather alerts based on environment variables.""" |
60 | | - while True: |
61 | | - weather_data = fetch_weather(CITY) |
62 | | - check_alerts(weather_data, TEMP_THRESHOLD, WIND_SPEED_THRESHOLD) |
63 | | - print("Waiting for the next check...") |
64 | | - time.sleep(3600) # check every hour |
65 | | - |
| 63 | + return alerts if alerts else ["No alerts. Weather conditions are normal."] |
66 | 64 |
|
67 | 65 | if __name__ == "__main__": |
68 | | - main() |
| 66 | + app.run(host='0.0.0.0', port=80, debug=True) |
69 | 67 |
|
0 commit comments