Add future day selection
Some checks failed
SonarQube Scan / SonarQube Trigger (push) Failing after 29s
Build image - Testing / build-api-testing (push) Successful in 42s

This commit is contained in:
Evan 2025-01-28 21:24:21 -05:00
parent 7f13219ec3
commit 8307bf5b52
2 changed files with 39 additions and 17 deletions

View file

@ -42,21 +42,27 @@ def date():
def city():
if "city" in request.values:
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
# Asheboro
if city == "1":
weather = _get_weather(35.6396, -79.8509)
weather = _get_weather(day, 35.6396, -79.8509)
return strq(weather)
# Lynchburg
elif city == "2":
weather = _get_weather(37.3490, -79.1787)
weather = _get_weather(day, 37.3490, -79.1787)
return strq(weather)
# Cullowhee
elif city == "3":
weather = _get_weather(35.3087, -83.1861)
weather = _get_weather(day, 35.3087, -83.1861)
return strq(weather)
else:
return strq("Your selection is invalid.")
return strq("Your city selection is invalid.")
else:
return strq("A city is required.")
@ -66,10 +72,15 @@ def city():
def zipcode():
if "zipcode" in request.values:
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:
lat, long = _get_cords(zipcode)
weather = _get_weather(lat, long)
weather = _get_weather(day, lat, long)
return strq(weather)
else:
return strq("Sorry, I don't understand that zipcode.")

View file

@ -111,25 +111,36 @@ def _get_phone_time(phone_number):
return local_time
def _get_weather(lat, long):
def _get_weather(day, lat, long):
try:
if lat is None or long is None:
if day is None or lat is None or long is None:
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)
if weather_json is None:
return "An error has occured and the weather could not be retrieved."
weather = weather_template.format(
round(weather_json["current"]["temp"]),
round(weather_json["current"]["feels_like"]),
round(weather_json["daily"][0]["temp"]["max"]),
round(weather_json["daily"][0]["temp"]["min"]),
round(weather_json["current"]["humidity"]),
weather_json["daily"][0]["summary"],
)
if day == 0:
weather = weather_template.format(
round(weather_json["current"]["temp"]),
round(weather_json["current"]["feels_like"]),
round(weather_json["daily"][0]["temp"]["max"]),
round(weather_json["daily"][0]["temp"]["min"]),
round(weather_json["current"]["humidity"]),
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)
s = Stats(lat_long=lat_long)
db.session.add(s)