duongve commited on
Commit
a137f8c
·
verified ·
1 Parent(s): 3067763

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -69
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'] = '[email protected]'
11
- app.config['MAIL_PASSWORD'] = 'your_password'
12
-
13
- mail = Mail(app)
14
-
15
- @app.route('/')
16
- def index():
17
- return render_template('index.html')
18
-
19
- @app.route('/weather', methods=['GET'])
20
- def weather():
21
- city = request.args.get('city')
22
- if city:
23
- # Check cache first
24
- cached_data = get_cached_weather_data(city)
25
- if cached_data:
26
- return jsonify(cached_data)
27
-
28
- # If not in cache, fetch from API
29
- data = get_weather_data(city = city)
30
- if data:
31
- save_weather_data(city, data)
32
- return jsonify(data)
33
- else:
34
- return jsonify({'error': 'City not found'}), 404
35
- else:
36
- return jsonify({'error': 'No city provided'}), 400
37
-
38
- @app.route('/weatherlocation', methods=['GET'])
39
- def weatherlocation():
40
- lat = request.args.get('lat')
41
- lon = request.args.get('lon')
42
- if lat and lon:
43
- # Check cache first
44
- cached_data = get_cached_weather_data(lat+lon)
45
- if cached_data:
46
- return jsonify(cached_data)
47
-
48
- # If not in cache, fetch from API
49
- data = get_weather_data(lat = lat, lon = lon)
50
- if data:
51
- save_weather_data(lat+lon, data)
52
- return jsonify(data)
53
- else:
54
- return jsonify({'error': 'City not found'}), 404
55
- else:
56
- return jsonify({'error': 'No city provided'}), 400
57
-
58
- @app.route('/subscribe', methods=['POST'])
59
- def subscribe():
60
- email = request.json.get('email')
61
- if email:
62
- msg = Message('Weather Subscription', sender='[email protected]', recipients=[email])
63
- msg.body = 'Thank you for subscribing to daily weather updates!'
64
- mail.send(msg)
65
- return jsonify({'message': 'Subscription successful, please check your email to confirm.'})
66
- else:
67
- return jsonify({'error': 'No email provided'}), 400
68
-
69
- if __name__ == '__main__':
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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'] = '[email protected]'
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='[email protected]', 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)