Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,91 @@
|
|
| 1 |
-
from flask import Flask, render_template, request, jsonify
|
| 2 |
-
from weather_api import get_weather_data, save_weather_data, get_cached_weather_data
|
| 3 |
-
from flask_mail import Mail, Message
|
| 4 |
-
|
| 5 |
-
app = Flask(__name__)
|
| 6 |
-
|
| 7 |
-
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
|
| 8 |
-
app.config['MAIL_PORT'] = 587
|
| 9 |
-
app.config['MAIL_USE_TLS'] = True
|
| 10 |
-
app.config['MAIL_USERNAME'] = 'your_email@example.com'
|
| 11 |
-
app.config['MAIL_PASSWORD'] = 'your_password'
|
| 12 |
-
|
| 13 |
-
mail = Mail(app)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
@app.route('/
|
| 20 |
-
def
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
return jsonify(
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
return jsonify(
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
app.run(debug=True, host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
+
from weather_api import get_weather_data, save_weather_data, get_cached_weather_data,more_weather_data
|
| 3 |
+
from flask_mail import Mail, Message
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
|
| 8 |
+
app.config['MAIL_PORT'] = 587
|
| 9 |
+
app.config['MAIL_USE_TLS'] = True
|
| 10 |
+
app.config['MAIL_USERNAME'] = 'your_email@example.com'
|
| 11 |
+
app.config['MAIL_PASSWORD'] = 'your_password'
|
| 12 |
+
|
| 13 |
+
mail = Mail(app)
|
| 14 |
+
|
| 15 |
+
city = ''
|
| 16 |
+
lat = ''
|
| 17 |
+
lon = ''
|
| 18 |
+
|
| 19 |
+
@app.route('/')
|
| 20 |
+
def index():
|
| 21 |
+
return render_template('index.html')
|
| 22 |
+
|
| 23 |
+
@app.route('/weather', methods=['GET'])
|
| 24 |
+
def weather():
|
| 25 |
+
global city, lat,lon
|
| 26 |
+
lat = lon = ''
|
| 27 |
+
city = request.args.get('city')
|
| 28 |
+
if city:
|
| 29 |
+
# Check cache first
|
| 30 |
+
cached_data = get_cached_weather_data(city)
|
| 31 |
+
if cached_data:
|
| 32 |
+
return jsonify(cached_data)
|
| 33 |
+
|
| 34 |
+
# If not in cache, fetch from API
|
| 35 |
+
data = get_weather_data(city = city)
|
| 36 |
+
if data:
|
| 37 |
+
save_weather_data(city, data)
|
| 38 |
+
return jsonify(data)
|
| 39 |
+
else:
|
| 40 |
+
return jsonify({'error': 'City not found'}), 404
|
| 41 |
+
else:
|
| 42 |
+
return jsonify({'error': 'No city provided'}), 400
|
| 43 |
+
|
| 44 |
+
@app.route('/weatherlocation', methods=['GET'])
|
| 45 |
+
def weatherlocation():
|
| 46 |
+
global city, lat,lon
|
| 47 |
+
city = ''
|
| 48 |
+
lat = request.args.get('lat')
|
| 49 |
+
lon = request.args.get('lon')
|
| 50 |
+
if lat and lon:
|
| 51 |
+
# Check cache first
|
| 52 |
+
cached_data = get_cached_weather_data(lat+lon)
|
| 53 |
+
if cached_data:
|
| 54 |
+
return jsonify(cached_data)
|
| 55 |
+
|
| 56 |
+
# If not in cache, fetch from API
|
| 57 |
+
data = get_weather_data(lat = lat, lon = lon)
|
| 58 |
+
if data:
|
| 59 |
+
save_weather_data(lat+lon, data)
|
| 60 |
+
return jsonify(data)
|
| 61 |
+
else:
|
| 62 |
+
return jsonify({'error': 'City not found'}), 404
|
| 63 |
+
else:
|
| 64 |
+
return jsonify({'error': 'No city provided'}), 400
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
@app.route('/load_more_forecast', methods=['GET'])
|
| 68 |
+
def more_forecast():
|
| 69 |
+
global city, lat,lon
|
| 70 |
+
if (lat !='' and lon != '') or city != '':
|
| 71 |
+
data = more_weather_data(city = city, lat = lat, lon = lon)
|
| 72 |
+
if data:
|
| 73 |
+
return jsonify(data)
|
| 74 |
+
else:
|
| 75 |
+
return jsonify({'error': 'City not found'}), 404
|
| 76 |
+
else:
|
| 77 |
+
return jsonify({'error': 'No city provided'}), 400
|
| 78 |
+
|
| 79 |
+
@app.route('/subscribe', methods=['POST'])
|
| 80 |
+
def subscribe():
|
| 81 |
+
email = request.json.get('email')
|
| 82 |
+
if email:
|
| 83 |
+
msg = Message('Weather Subscription', sender='your_email@example.com', recipients=[email])
|
| 84 |
+
msg.body = 'Thank you for subscribing to daily weather updates!'
|
| 85 |
+
mail.send(msg)
|
| 86 |
+
return jsonify({'message': 'Subscription successful, please check your email to confirm.'})
|
| 87 |
+
else:
|
| 88 |
+
return jsonify({'error': 'No email provided'}), 400
|
| 89 |
+
|
| 90 |
+
if __name__ == '__main__':
|
| 91 |
app.run(debug=True, host='0.0.0.0', port=7860)
|