Add /time and /date endpoints
Some checks failed
SonarQube Scan / SonarQube Trigger (push) Failing after 28s
Build image - Testing / build-api-testing (push) Successful in 45s

This commit is contained in:
Evan 2025-01-26 20:55:14 -05:00
parent 7195875792
commit 1d5bc48e8e
3 changed files with 80 additions and 2 deletions

View file

@ -3,3 +3,5 @@ flask_sqlalchemy==3.1.1
gunicorn==23.0.0 gunicorn==23.0.0
prometheus-flask-exporter==0.23.1 prometheus-flask-exporter==0.23.1
requests==2.32.3 requests==2.32.3
phonenumbers==8.13.53
pytz==2024.2

View file

@ -1,12 +1,33 @@
from flask import jsonify, request from flask import jsonify, request
from utils import logger, validate_data_presence, _get_weather, _get_cords, 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
from . import by_path_counter from . import by_path_counter
@app.route("/time", methods=["GET"])
@by_path_counter
def time():
if "caller" in request.values:
phone_number = request.values["caller"]
else:
phone_number = "1"
return strq(_get_time(phone_number))
@app.route("/date", methods=["GET"])
@by_path_counter
def date():
if "caller" in request.values:
phone_number = request.values["caller"]
else:
phone_number = 1
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():

View file

@ -6,6 +6,9 @@ import re
import typing as t import typing as t
import requests import requests
import traceback import traceback
import phonenumbers
from phonenumbers import timezone as pntz
import pytz
from models import db, Stats, Weather, Zipcode from models import db, Stats, Weather, Zipcode
from config import env_OWM_KEY, env_OWM_UNITS, env_CACHE_TIME from config import env_OWM_KEY, env_OWM_UNITS, env_CACHE_TIME
@ -56,6 +59,58 @@ 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):
try:
# Get and print the current time in that timezone
time_object = _get_phone_time(phone_number)
time = f"{time_object.strftime('The time is %I:%M %p.')}"
return time
except Exception as e:
logger.error("Error in _get_time: " + str(e))
logger.error(traceback.format_exc())
return "An error has occured and the time could not be retrieved."
def _get_date(phone_number):
try:
# Get and print the current date in that timezone
time_object = _get_phone_time(phone_number)
date = f"{time_object.strftime('Today is %A, %B %d.')}"
return date
except Exception as e:
logger.error("Error in _get_date: " + str(e))
logger.error(traceback.format_exc())
return "An error has occured and the date could not be retrieved."
def _get_phone_time(phone_number):
try:
# Parse the cleaned phone number into a PhoneNumber object
number = phonenumbers.parse(phone_number, "US")
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]
except Exception as e:
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): def _get_weather(lat, long):
try: try:
if lat is None or long is None: if lat is None or long is None: