-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
151 lines (126 loc) · 5.38 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import requests
import string
import datetime
from flask import Flask, render_template, request, redirect, url_for, flash
from flask_sqlalchemy import SQLAlchemy
from dotenv import load_dotenv
load_dotenv()
OWM_ENDPOINT = "https://api.openweathermap.org/data/2.5/weather"
OWM_FORECAST_ENDPOINT = "https://api.openweathermap.org/data/2.5/forecast"
GEOCODING_API_ENDPOINT = "http://api.openweathermap.org/geo/1.0/direct"
api_key = "1b7327427012cd35c4de782378022165"
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///weather.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'thisisasecret'
db = SQLAlchemy(app)
class city(db.Model) :
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50),nullable=False)
def get_weather_data(city):
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&units=metric&appid=1b7327427012cd35c4de782378022165"
r = requests.get(url).json()
return r
@app.route('/')
def index_get():
cities = city.query.distinct().order_by(city.id.desc()).limit(3).all()
weather_data = []
for c in cities:
r = get_weather_data(c.name)
weather = {
'city' : c.name,
'temperature' : r['main']['temp'],
'description' : r['weather'][0]['description'],
'icon' : r['weather'][0]['icon'],
}
weather_data.append(weather)
return render_template('weather.html', weather_data=weather_data)
@app.route("/<city>", methods=["GET", "POST"])
def get_weather(city):
# Format city name and get current date to display on page
city_name = string.capwords(city)
today = datetime.datetime.now()
current_date = today.strftime("%A, %B %d")
# Get latitude and longitude for city
location_params = {
"q": city_name,
"appid": api_key,
"limit": 3,
}
location_response = requests.get(GEOCODING_API_ENDPOINT, params=location_params)
location_data = location_response.json()
# Prevent IndexError if user entered a city name with no coordinates by redirecting to error page
if not location_data:
return redirect(url_for("error"))
else:
lat = location_data[0]['lat']
lon = location_data[0]['lon']
# Get OpenWeather API data
weather_params = {
"lat": lat,
"lon": lon,
"appid": api_key,
"units": "metric",
}
weather_response = requests.get(OWM_ENDPOINT, weather_params)
weather_response.raise_for_status()
weather_data = weather_response.json()
# Get current weather data
current_temp = round(weather_data['main']['temp'])
current_weather = weather_data['weather'][0]['main']
min_temp = round(weather_data['main']['temp_min'])
max_temp = round(weather_data['main']['temp_max'])
wind_speed = weather_data['wind']['speed']
# Get five-day weather forecast data
forecast_response = requests.get(OWM_FORECAST_ENDPOINT, weather_params)
forecast_data = forecast_response.json()
# Make lists of temperature and weather description data to show user
five_day_temp_list = [round(item['main']['temp']) for item in forecast_data['list'] if '12:00:00' in item['dt_txt']]
five_day_weather_list = [item['weather'][0]['main'] for item in forecast_data['list']
if '12:00:00' in item['dt_txt']]
# Get next four weekdays to show user alongside weather data
five_day_unformatted = [today, today + datetime.timedelta(days=1), today + datetime.timedelta(days=2),
today + datetime.timedelta(days=3), today + datetime.timedelta(days=4)]
five_day_dates_list = [date.strftime("%a") for date in five_day_unformatted]
return render_template("city.html", city_name=city_name, current_date=current_date, current_temp=current_temp,
current_weather=current_weather, min_temp=min_temp, max_temp=max_temp, wind_speed=wind_speed,
five_day_temp_list=five_day_temp_list, five_day_weather_list=five_day_weather_list,
five_day_dates_list=five_day_dates_list)
@app.route('/', methods=["POST"])
def index_post():
err_msg = ''
new_city = request.form.get('city')
new_city = new_city.lower()
new_city = string.capwords(new_city)
if new_city:
existing_city = city.query.filter_by(name=new_city).first()
if existing_city:
db.session.delete(existing_city)
db.session.commit()
new_city_data = get_weather_data(new_city)
if new_city_data['cod'] == 200:
new_city_obj = city(name=new_city)
db.session.add(new_city_obj)
db.session.commit()
else:
return redirect(url_for("error"))
if err_msg:
return redirect(url_for("error"))
else:
#flash('City added successfully!', 'success')
if request.method == "POST":
c = request.form.get("search")
return redirect(url_for("get_weather", city=new_city))
else:
return redirect(url_for('index_get'))
@app.route('/delete/<name>')
def delete_city( name ):
c = city.query.filter_by(name=name).first()
db.session.delete(c)
db.session.commit()
flash(f'Successfully deleted { c.name }!', 'success')
return redirect(url_for('index_get'))
@app.route("/error")
def error():
return render_template("error.html")