File size: 2,361 Bytes
320a5a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from flask import Flask, render_template, request, jsonify
from weather_api import get_weather_data, save_weather_data, get_cached_weather_data
from flask_mail import Mail, Message

app = Flask(__name__)

app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = '[email protected]'
app.config['MAIL_PASSWORD'] = 'your_password'

mail = Mail(app)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/weather', methods=['GET'])
def weather():
    city = request.args.get('city')
    if city:
        # Check cache first
        cached_data = get_cached_weather_data(city)
        if cached_data:
            return jsonify(cached_data)
        
        # If not in cache, fetch from API
        data = get_weather_data(city = city)
        if data:
            save_weather_data(city, data)
            return jsonify(data)
        else:
            return jsonify({'error': 'City not found'}), 404
    else:
        return jsonify({'error': 'No city provided'}), 400

@app.route('/weatherlocation', methods=['GET'])
def weatherlocation():
    lat = request.args.get('lat')
    lon = request.args.get('lon')
    if lat and lon:
        # Check cache first
        cached_data = get_cached_weather_data(lat+lon)
        if cached_data:
            return jsonify(cached_data)
        
        # If not in cache, fetch from API
        data = get_weather_data(lat = lat, lon = lon)
        if data:
            save_weather_data(lat+lon, data)
            return jsonify(data)
        else:
            return jsonify({'error': 'City not found'}), 404
    else:
        return jsonify({'error': 'No city provided'}), 400

@app.route('/subscribe', methods=['POST'])
def subscribe():
    email = request.json.get('email')
    if email:
        msg = Message('Weather Subscription', sender='[email protected]', recipients=[email])
        msg.body = 'Thank you for subscribing to daily weather updates!'
        mail.send(msg)
        return jsonify({'message': 'Subscription successful, please check your email to confirm.'})
    else:
        return jsonify({'error': 'No email provided'}), 400

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=7860)