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 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,10 +19,10 @@ 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"
|
||||||
|
|
||||||
return strq(_get_time(phone_number))
|
return strq(_get_time(phone_number))
|
||||||
|
|
||||||
|
|
||||||
|
@ -25,9 +33,10 @@ def date():
|
||||||
phone_number = request.values["caller"]
|
phone_number = request.values["caller"]
|
||||||
else:
|
else:
|
||||||
phone_number = 1
|
phone_number = 1
|
||||||
|
|
||||||
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."
|
||||||
|
)
|
||||||
|
|
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:
|
def strq(str: str) -> str:
|
||||||
return "\"" + str + "\""
|
return '"' + str + '"'
|
||||||
|
|
||||||
|
|
||||||
def _get_time(phone_number):
|
def _get_time(phone_number):
|
||||||
|
@ -85,7 +85,7 @@ def _get_date(phone_number):
|
||||||
|
|
||||||
|
|
||||||
def _get_phone_time(phone_number):
|
def _get_phone_time(phone_number):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Parse the cleaned phone number into a PhoneNumber object
|
# Parse the cleaned phone number into a PhoneNumber object
|
||||||
number = phonenumbers.parse(phone_number, "US")
|
number = phonenumbers.parse(phone_number, "US")
|
||||||
|
@ -93,7 +93,7 @@ def _get_phone_time(phone_number):
|
||||||
valid = phonenumbers.is_valid_number(number)
|
valid = phonenumbers.is_valid_number(number)
|
||||||
if not valid:
|
if not valid:
|
||||||
raise TypeError("Number not valid")
|
raise TypeError("Number not valid")
|
||||||
|
|
||||||
# Determine the timezone based on the area code or country code
|
# Determine the timezone based on the area code or country code
|
||||||
tz_str = pntz.time_zones_for_number(number)
|
tz_str = pntz.time_zones_for_number(number)
|
||||||
tz_str = tz_str[0]
|
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))
|
logger.error("Error parsing phone number " + str(phone_number) + " : " + str(e))
|
||||||
# Default to America/New_York if there's an error during conversion
|
# Default to America/New_York if there's an error during conversion
|
||||||
tz_str = "America/New_York"
|
tz_str = "America/New_York"
|
||||||
|
|
||||||
tz = pytz.timezone(tz_str)
|
tz = pytz.timezone(tz_str)
|
||||||
|
|
||||||
# Get current datetime in the target timezone
|
# Get current datetime in the target timezone
|
||||||
now_utc = datetime.now(timezone.utc)
|
now_utc = datetime.now(timezone.utc)
|
||||||
local_time = now_utc.astimezone(tz)
|
local_time = now_utc.astimezone(tz)
|
||||||
|
|
||||||
return local_time
|
return local_time
|
||||||
|
|
||||||
|
|
||||||
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,11 +150,13 @@ 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!")
|
||||||
|
|
||||||
response = requests.get(url)
|
response = requests.get(url)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
|
@ -185,7 +189,7 @@ def _get_cords(zipcode):
|
||||||
logger.info("Zipcode cache hit!")
|
logger.info("Zipcode cache hit!")
|
||||||
return z.results["lat"], z.results["lon"]
|
return z.results["lat"], z.results["lon"]
|
||||||
logger.info("Zipcode cache miss!")
|
logger.info("Zipcode cache miss!")
|
||||||
|
|
||||||
response = requests.get(url)
|
response = requests.get(url)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
|
|
Loading…
Reference in a new issue