This commit is contained in:
Evan 2025-01-28 20:29:51 -05:00
parent 1d5bc48e8e
commit 1631534f9b
2 changed files with 31 additions and 15 deletions

View file

@ -1,6 +1,14 @@
from flask import jsonify, request from flask import jsonify, request
from utils import logger, validate_data_presence, _get_weather, _get_cords, _get_date, _get_time, strq from utils import (
logger,
validate_data_presence,
_get_weather,
_get_cords,
_get_date,
_get_time,
strq,
)
# from config import # from config import
from . import routes as app from . import routes as app
@ -11,7 +19,7 @@ from . import by_path_counter
@by_path_counter @by_path_counter
def time(): def time():
if "caller" in request.values: if "caller" in request.values:
phone_number = request.values["caller"] phone_number = str(request.values["caller"])
else: else:
phone_number = "1" phone_number = "1"
@ -28,6 +36,7 @@ def date():
return strq(_get_date(phone_number)) return strq(_get_date(phone_number))
@app.route("/city", methods=["GET"]) @app.route("/city", methods=["GET"])
@by_path_counter @by_path_counter
def city(): def city():
@ -67,7 +76,10 @@ def zipcode():
else: else:
return strq("A zipcode is required.") return strq("A zipcode is required.")
@app.route("/test", methods=["GET"]) @app.route("/test", methods=["GET"])
@by_path_counter @by_path_counter
def test(): def test():
return strq("Successful test. By the way, do you know the muffin man? The muffin man? The muffin man? Do you know the muffin man? He doesn't exist.") return strq(
"Successful test. By the way, do you know the muffin man? The muffin man? The muffin man? Do you know the muffin man? He doesn't exist."
)

View file

@ -57,7 +57,7 @@ def validate_data_presence(data: t.Dict[str, t.Any], keys: list[str]) -> bool:
def strq(str: str) -> str: def strq(str: str) -> str:
return "\"" + str + "\"" return '"' + str + '"'
def _get_time(phone_number): def _get_time(phone_number):
@ -114,7 +114,9 @@ def _get_phone_time(phone_number):
def _get_weather(lat, long): def _get_weather(lat, long):
try: try:
if lat is None or long is None: if lat is None or long is None:
return "An error has occured and the provided zipcode could not be understood." return (
"An error has occured and the provided zipcode could not be understood."
)
weather_json = _get_weather_json(lat, long) weather_json = _get_weather_json(lat, long)
if weather_json is None: if weather_json is None:
@ -148,7 +150,9 @@ def _get_weather_json(lat, long):
try: try:
w = Weather.query.filter(Weather.lat_long == lat_long).first() w = Weather.query.filter(Weather.lat_long == lat_long).first()
if w and w.last_timestamp.replace(tzinfo=timezone.utc) >= current_time - timedelta(minutes=env_CACHE_TIME): if w and w.last_timestamp.replace(
tzinfo=timezone.utc
) >= current_time - timedelta(minutes=env_CACHE_TIME):
logger.info("Weather cache hit!") logger.info("Weather cache hit!")
return w.results return w.results
logger.info("Weather cache miss!") logger.info("Weather cache miss!")