Black
This commit is contained in:
parent
1d5bc48e8e
commit
1631534f9b
2 changed files with 31 additions and 15 deletions
|
@ -1,6 +1,14 @@
|
|||
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 . import routes as app
|
||||
|
@ -11,10 +19,10 @@ from . import by_path_counter
|
|||
@by_path_counter
|
||||
def time():
|
||||
if "caller" in request.values:
|
||||
phone_number = request.values["caller"]
|
||||
phone_number = str(request.values["caller"])
|
||||
else:
|
||||
phone_number = "1"
|
||||
|
||||
|
||||
return strq(_get_time(phone_number))
|
||||
|
||||
|
||||
|
@ -25,9 +33,10 @@ def date():
|
|||
phone_number = request.values["caller"]
|
||||
else:
|
||||
phone_number = 1
|
||||
|
||||
|
||||
return strq(_get_date(phone_number))
|
||||
|
||||
|
||||
@app.route("/city", methods=["GET"])
|
||||
@by_path_counter
|
||||
def city():
|
||||
|
@ -67,7 +76,10 @@ def zipcode():
|
|||
else:
|
||||
return strq("A zipcode is required.")
|
||||
|
||||
|
||||
@app.route("/test", methods=["GET"])
|
||||
@by_path_counter
|
||||
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."
|
||||
)
|
||||
|
|
24
app/utils.py
24
app/utils.py
|
@ -57,7 +57,7 @@ def validate_data_presence(data: t.Dict[str, t.Any], keys: list[str]) -> bool:
|
|||
|
||||
|
||||
def strq(str: str) -> str:
|
||||
return "\"" + str + "\""
|
||||
return '"' + str + '"'
|
||||
|
||||
|
||||
def _get_time(phone_number):
|
||||
|
@ -85,7 +85,7 @@ def _get_date(phone_number):
|
|||
|
||||
|
||||
def _get_phone_time(phone_number):
|
||||
|
||||
|
||||
try:
|
||||
# Parse the cleaned phone number into a PhoneNumber object
|
||||
number = phonenumbers.parse(phone_number, "US")
|
||||
|
@ -93,7 +93,7 @@ def _get_phone_time(phone_number):
|
|||
valid = phonenumbers.is_valid_number(number)
|
||||
if not valid:
|
||||
raise TypeError("Number not valid")
|
||||
|
||||
|
||||
# Determine the timezone based on the area code or country code
|
||||
tz_str = pntz.time_zones_for_number(number)
|
||||
tz_str = tz_str[0]
|
||||
|
@ -101,20 +101,22 @@ def _get_phone_time(phone_number):
|
|||
logger.error("Error parsing phone number " + str(phone_number) + " : " + str(e))
|
||||
# Default to America/New_York if there's an error during conversion
|
||||
tz_str = "America/New_York"
|
||||
|
||||
|
||||
tz = pytz.timezone(tz_str)
|
||||
|
||||
|
||||
# Get current datetime in the target timezone
|
||||
now_utc = datetime.now(timezone.utc)
|
||||
local_time = now_utc.astimezone(tz)
|
||||
|
||||
|
||||
return local_time
|
||||
|
||||
|
||||
def _get_weather(lat, long):
|
||||
try:
|
||||
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)
|
||||
if weather_json is None:
|
||||
|
@ -148,11 +150,13 @@ def _get_weather_json(lat, long):
|
|||
|
||||
try:
|
||||
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!")
|
||||
return w.results
|
||||
logger.info("Weather cache miss!")
|
||||
|
||||
|
||||
response = requests.get(url)
|
||||
|
||||
if response.status_code == 200:
|
||||
|
@ -185,7 +189,7 @@ def _get_cords(zipcode):
|
|||
logger.info("Zipcode cache hit!")
|
||||
return z.results["lat"], z.results["lon"]
|
||||
logger.info("Zipcode cache miss!")
|
||||
|
||||
|
||||
response = requests.get(url)
|
||||
|
||||
if response.status_code == 200:
|
||||
|
|
Loading…
Reference in a new issue