-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
71 lines (55 loc) · 2.46 KB
/
server.py
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, jsonify, request, send_from_directory
import requests
import os
import datetime
from dotenv import load_dotenv
from flask_cors import CORS, cross_origin
# Load environment variables from .env file
load_dotenv()
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": ["http://localhost:9000", "https://bain-weather-app-1bb2d43eb419.herokuapp.com"]}})
app.config['API_KEY'] = os.getenv('API_KEY')
# Serve build files
@app.route('/', methods=['GET'])
def serve_react_app():
return send_from_directory('public', 'index.html')
# Serve bundle.js
@app.route('/bundle.js', methods=['GET', 'OPTIONS'])
def serve_bundle():
return send_from_directory('dist', 'bundle.js')
# Server weather data
@app.route('/weather', methods=['GET'])
@cross_origin()
def get_weather():
if request.method == 'OPTIONS':
response = jsonify({'message': 'Preflight request allowed'})
response.headers.add('Access-Control-Allow-Methods', 'GET')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type')
return response
city = request.args.get('city')
api_key = app.config['API_KEY']
if not city:
return jsonify({'error': 'City parameter is required'}), 400
# Current weather API call
current_url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}'
current_response = requests.get(current_url)
if current_response.status_code != 200:
return jsonify({'error': 'Failed to fetch current weather data'}), 500
current_data = current_response.json()
# Extract lat and lon from current weather data
lat = current_data['coord']['lat']
lon = current_data['coord']['lon']
# Historical weather API call for the past 5 days
historical_url = f'https://api.openweathermap.org/data/3.0/onecall/timemachine?lat={lat}&lon={lon}&dt='
historical_data = []
for i in range(1, 6):
timestamp = int((datetime.datetime.utcnow() - datetime.timedelta(days=i)).timestamp())
historical_response = requests.get(f'{historical_url}{timestamp}&appid={api_key}')
if historical_response.status_code == 200:
historical_data.append(historical_response.json())
return jsonify({'current_weather': current_data, 'historical_weather': historical_data})
# Run app
if __name__ == '__main__':
# Binds app to the correct port specified by Heroku
port = int(os.environ.get('PORT', 5001))
app.run(host='0.0.0.0', port=port, debug=True)