-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
105 lines (80 loc) · 3.29 KB
/
app.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# from flask import Flask, render_template, jsonify, request
# import requests
# app = Flask(__name__)
# # Your OpenWeatherMap API Key
# API_KEY = '2f5531105953fd80c02b75b18dbcce32'
# @app.route('/')
# def index():
# return render_template('index.html')
# @app.route('/weather', methods=['GET'])
# def get_weather():
# city = request.args.get('city')
# if not city:
# return jsonify({"error": "City name is required"}), 400
# # Get city coordinates from OpenWeatherMap API
# geocoding_url = f"http://api.openweathermap.org/geo/1.0/direct?q={city}&limit=1&appid={API_KEY}"
# geo_response = requests.get(geocoding_url)
# geo_data = geo_response.json()
# if not geo_data:
# return jsonify({"error": f"City '{city}' not found"}), 404
# lat = geo_data[0]['lat']
# lon = geo_data[0]['lon']
# country = geo_data[0]['country']
# # Fetch weather data
# weather_url = f"http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API_KEY}"
# weather_response = requests.get(weather_url)
# weather_data = weather_response.json()
# # Fetch air quality data
# air_quality_url = f"http://api.openweathermap.org/data/2.5/air_pollution?lat={lat}&lon={lon}&appid={API_KEY}"
# air_quality_response = requests.get(air_quality_url)
# air_quality_data = air_quality_response.json()
# # Combine both weather and air quality data
# result = {
# "city": city,
# "country": country,
# "weather": weather_data,
# "air_quality": air_quality_data
# }
# return jsonify(result)
# if __name__ == '__main__':
# app.run(debug=True)
from flask import Flask, render_template, jsonify, request
import requests
app = Flask(__name__)
# Your OpenWeatherMap API Key
API_KEY = '2f5531105953fd80c02b75b18dbcce32'
@app.route('/')
def index():
return render_template('index.html')
@app.route('/weather', methods=['GET'])
def get_weather():
city = request.args.get('city')
if not city:
return jsonify({"error": "City name is required"}), 400
# Get city coordinates from OpenWeatherMap API
geocoding_url = f"http://api.openweathermap.org/geo/1.0/direct?q={city}&limit=1&appid={API_KEY}"
geo_response = requests.get(geocoding_url)
geo_data = geo_response.json()
if not geo_data:
return jsonify({"error": f"City '{city}' not found"}), 404
lat = geo_data[0]['lat']
lon = geo_data[0]['lon']
country = geo_data[0]['country']
# Fetch weather data
weather_url = f"http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API_KEY}"
weather_response = requests.get(weather_url)
weather_data = weather_response.json()
# Fetch air quality data
air_quality_url = f"http://api.openweathermap.org/data/2.5/air_pollution?lat={lat}&lon={lon}&appid={API_KEY}"
air_quality_response = requests.get(air_quality_url)
air_quality_data = air_quality_response.json()
# Combine both weather and air quality data
result = {
"city": city,
"country": country,
"weather": weather_data,
"air_quality": air_quality_data
}
return jsonify(result)
if __name__ == '__main__':
app.run(debug=True)