|
| 1 | +from flask import Flask, render_template, request, redirect, url_for, flash |
| 2 | +import os |
| 3 | +import requests |
| 4 | + |
| 5 | + |
| 6 | +app = Flask(__name__) |
| 7 | +app.config['SECRET_KEY'] = os.environ.get('FLASK_SECRET_KEY', 'dev-secret-key') |
| 8 | + |
| 9 | +API_KEY = os.environ.get('WEATHER_API_KEY', '07c76e93fa3f49e08c0111233252308') |
| 10 | +BASE_URL = 'https://api.weatherapi.com/v1/current.json' |
| 11 | + |
| 12 | + |
| 13 | +@app.route('/', methods=['GET']) |
| 14 | +def home(): |
| 15 | + return render_template('index.html') |
| 16 | + |
| 17 | + |
| 18 | +@app.route('/weather', methods=['GET', 'POST']) |
| 19 | +def weather(): |
| 20 | + if request.method == 'GET': |
| 21 | + return redirect(url_for('home')) |
| 22 | + |
| 23 | + city = (request.form.get('city') or '').strip() |
| 24 | + if not city: |
| 25 | + flash('Please enter a city name to search weather.', 'warning') |
| 26 | + return redirect(url_for('home')) |
| 27 | + |
| 28 | + params = { |
| 29 | + 'key': API_KEY, |
| 30 | + 'q': city, |
| 31 | + 'aqi': 'no', |
| 32 | + } |
| 33 | + |
| 34 | + try: |
| 35 | + response = requests.get(BASE_URL, params=params, timeout=10) |
| 36 | + response.raise_for_status() |
| 37 | + data = response.json() |
| 38 | + |
| 39 | + current = data.get('current', {}) |
| 40 | + location = data.get('location', {}) |
| 41 | + |
| 42 | + result = { |
| 43 | + 'city': f"{location.get('name', '')}, {location.get('region', '')}, {location.get('country', '')}", |
| 44 | + 'localtime': location.get('localtime', ''), |
| 45 | + 'temp_c': current.get('temp_c'), |
| 46 | + 'temp_f': current.get('temp_f'), |
| 47 | + 'condition_text': (current.get('condition') or {}).get('text'), |
| 48 | + 'icon': (current.get('condition') or {}).get('icon'), |
| 49 | + 'wind_kph': current.get('wind_kph'), |
| 50 | + 'humidity': current.get('humidity'), |
| 51 | + 'feelslike_c': current.get('feelslike_c'), |
| 52 | + 'feelslike_f': current.get('feelslike_f'), |
| 53 | + 'cloud': current.get('cloud'), |
| 54 | + 'uv': current.get('uv'), |
| 55 | + 'is_day': current.get('is_day'), |
| 56 | + } |
| 57 | + |
| 58 | + return render_template('weather.html', result=result) |
| 59 | + |
| 60 | + except requests.exceptions.HTTPError as http_err: |
| 61 | + try: |
| 62 | + err = response.json() |
| 63 | + msg = (err.get('error') or {}).get('message') or 'Unable to fetch weather.' |
| 64 | + except Exception: |
| 65 | + msg = f'HTTP error: {http_err}' |
| 66 | + flash(msg, 'danger') |
| 67 | + return redirect(url_for('home')) |
| 68 | + except requests.exceptions.RequestException: |
| 69 | + flash('Network error. Please check your connection and try again.', 'danger') |
| 70 | + return redirect(url_for('home')) |
| 71 | + except Exception: |
| 72 | + flash('Unexpected error occurred. Please try again later.', 'danger') |
| 73 | + return redirect(url_for('home')) |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + app.run(debug=True) |
| 78 | + |
| 79 | + |
0 commit comments