From 8307bf5b52194b4645c9ed49135b7209e90731f9 Mon Sep 17 00:00:00 2001 From: Evan <17254809+bigun27@users.noreply.github.com> Date: Tue, 28 Jan 2025 21:24:21 -0500 Subject: [PATCH] Add future day selection --- app/routes/client.py | 23 +++++++++++++++++------ app/utils.py | 33 ++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/app/routes/client.py b/app/routes/client.py index b7cb0c4..4577110 100644 --- a/app/routes/client.py +++ b/app/routes/client.py @@ -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.") diff --git a/app/utils.py b/app/utils.py index b13ad8b..b49a87e 100644 --- a/app/utils.py +++ b/app/utils.py @@ -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)