Add future day selection
This commit is contained in:
parent
7f13219ec3
commit
8307bf5b52
2 changed files with 39 additions and 17 deletions
|
@ -42,21 +42,27 @@ def date():
|
||||||
def city():
|
def city():
|
||||||
if "city" in request.values:
|
if "city" in request.values:
|
||||||
city = request.values["city"]
|
city = request.values["city"]
|
||||||
|
if "day" in request.values:
|
||||||
|
day = int(request.values["day"])
|
||||||
|
if day < 0 or day > 7:
|
||||||
|
return strq("Your day selection is invalid.")
|
||||||
|
else:
|
||||||
|
day = 0
|
||||||
# Return a different message depending on the caller's choice
|
# Return a different message depending on the caller's choice
|
||||||
# Asheboro
|
# Asheboro
|
||||||
if city == "1":
|
if city == "1":
|
||||||
weather = _get_weather(35.6396, -79.8509)
|
weather = _get_weather(day, 35.6396, -79.8509)
|
||||||
return strq(weather)
|
return strq(weather)
|
||||||
# Lynchburg
|
# Lynchburg
|
||||||
elif city == "2":
|
elif city == "2":
|
||||||
weather = _get_weather(37.3490, -79.1787)
|
weather = _get_weather(day, 37.3490, -79.1787)
|
||||||
return strq(weather)
|
return strq(weather)
|
||||||
# Cullowhee
|
# Cullowhee
|
||||||
elif city == "3":
|
elif city == "3":
|
||||||
weather = _get_weather(35.3087, -83.1861)
|
weather = _get_weather(day, 35.3087, -83.1861)
|
||||||
return strq(weather)
|
return strq(weather)
|
||||||
else:
|
else:
|
||||||
return strq("Your selection is invalid.")
|
return strq("Your city selection is invalid.")
|
||||||
else:
|
else:
|
||||||
return strq("A city is required.")
|
return strq("A city is required.")
|
||||||
|
|
||||||
|
@ -66,10 +72,15 @@ def city():
|
||||||
def zipcode():
|
def zipcode():
|
||||||
if "zipcode" in request.values:
|
if "zipcode" in request.values:
|
||||||
zipcode = request.values["zipcode"]
|
zipcode = request.values["zipcode"]
|
||||||
|
if "day" in request.values:
|
||||||
|
day = int(request.values["day"])
|
||||||
|
if day < 0 or day > 7:
|
||||||
|
return strq("Your day selection is invalid.")
|
||||||
|
else:
|
||||||
|
day = 0
|
||||||
if len(zipcode) == 5:
|
if len(zipcode) == 5:
|
||||||
lat, long = _get_cords(zipcode)
|
lat, long = _get_cords(zipcode)
|
||||||
weather = _get_weather(lat, long)
|
weather = _get_weather(day, lat, long)
|
||||||
return strq(weather)
|
return strq(weather)
|
||||||
else:
|
else:
|
||||||
return strq("Sorry, I don't understand that zipcode.")
|
return strq("Sorry, I don't understand that zipcode.")
|
||||||
|
|
17
app/utils.py
17
app/utils.py
|
@ -111,17 +111,18 @@ def _get_phone_time(phone_number):
|
||||||
return local_time
|
return local_time
|
||||||
|
|
||||||
|
|
||||||
def _get_weather(lat, long):
|
def _get_weather(day, lat, long):
|
||||||
try:
|
try:
|
||||||
if lat is None or long is None:
|
if day is None or lat is None or long is None:
|
||||||
return (
|
return (
|
||||||
"An error has occured and the provided zipcode could not be understood."
|
"An error has occured and the request could not be fulfilled."
|
||||||
)
|
)
|
||||||
|
|
||||||
weather_json = _get_weather_json(lat, long)
|
weather_json = _get_weather_json(lat, long)
|
||||||
if weather_json is None:
|
if weather_json is None:
|
||||||
return "An error has occured and the weather could not be retrieved."
|
return "An error has occured and the weather could not be retrieved."
|
||||||
|
|
||||||
|
if day == 0:
|
||||||
weather = weather_template.format(
|
weather = weather_template.format(
|
||||||
round(weather_json["current"]["temp"]),
|
round(weather_json["current"]["temp"]),
|
||||||
round(weather_json["current"]["feels_like"]),
|
round(weather_json["current"]["feels_like"]),
|
||||||
|
@ -130,6 +131,16 @@ def _get_weather(lat, long):
|
||||||
round(weather_json["current"]["humidity"]),
|
round(weather_json["current"]["humidity"]),
|
||||||
weather_json["daily"][0]["summary"],
|
weather_json["daily"][0]["summary"],
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
tz = pytz.timezone(weather_json["timezone"])
|
||||||
|
now_utc = datetime.now(timezone.utc)
|
||||||
|
forcastdate = now_utc.astimezone(tz) + timedelta(days=day)
|
||||||
|
weather = "The forcast on {0} is a high of: {1} and low of: {2}. The summary is: {3}.".format(
|
||||||
|
forcastdate.strftime("%A"),
|
||||||
|
round(weather_json["daily"][day]["temp"]["max"]),
|
||||||
|
round(weather_json["daily"][day]["temp"]["min"]),
|
||||||
|
weather_json["daily"][day]["summary"],
|
||||||
|
)
|
||||||
lat_long = str(lat) + "," + str(long)
|
lat_long = str(lat) + "," + str(long)
|
||||||
s = Stats(lat_long=lat_long)
|
s = Stats(lat_long=lat_long)
|
||||||
db.session.add(s)
|
db.session.add(s)
|
||||||
|
|
Loading…
Reference in a new issue