summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--iss/server.py4
-rw-r--r--iss/tests/test_utils.py27
-rw-r--r--iss/utils.py8
3 files changed, 37 insertions, 2 deletions
diff --git a/iss/server.py b/iss/server.py
index f014d7f..5597baa 100644
--- a/iss/server.py
+++ b/iss/server.py
@@ -3,6 +3,7 @@ from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from .predictions import Predictions
+from .utils import normalize_lat_lng
app = FastAPI()
@@ -20,7 +21,8 @@ async def home(request: Request):
@app.get("/passes/{lat}/{lng}")
-async def passes(request: Request, lat: float, lng: float):
+async def passes(request: Request, lat: str, lng: str):
+ lat, lng = normalize_lat_lng(lat, lng)
preds = Predictions(lat, lng, altitude=0, days=5).get_grouped_predictions()
return templates.TemplateResponse(
"passes.html",
diff --git a/iss/tests/test_utils.py b/iss/tests/test_utils.py
index ee4971f..17dadf8 100644
--- a/iss/tests/test_utils.py
+++ b/iss/tests/test_utils.py
@@ -1,4 +1,6 @@
-from ..utils import deg_to_cardinal, seconds_to_minutes
+import pytest
+
+from ..utils import deg_to_cardinal, seconds_to_minutes, normalize_lat_lng
def test_seconds_to_minutes():
@@ -14,6 +16,29 @@ def test_seconds_to_minutes():
assert seconds_to_minutes(secs) == s
+def test_normalize_lat_lng():
+ cases = [
+ ("N12.345", "E67.890", 12.345, 67.890),
+ ("S12.345", "W67.890", -12.345, -67.890),
+ ("S11", "W22", -11, -22),
+ ("N0.987", "W0.123", 0.987, -0.123),
+ ("S88.765", "E177.654", -88.765, 177.654),
+ ]
+ for lat, lng, nlat, nlng in cases:
+ assert normalize_lat_lng(lat, lng) == (nlat, nlng)
+
+
+def test_fail_normalize_lat_lng():
+ failures = [
+ ("12", "E45"),
+ ("B12", "E45"),
+ ("12", "X45"),
+ ]
+ for lat, lng in failures:
+ with pytest.raises(Exception):
+ normalize_lat_lng(lat, lng)
+
+
def test_deg_to_cardinal():
cases = [
(0, "N"),
diff --git a/iss/utils.py b/iss/utils.py
index 326d835..656d4e3 100644
--- a/iss/utils.py
+++ b/iss/utils.py
@@ -7,6 +7,14 @@ def seconds_to_minutes(secs):
return f"{secs // 60}:{secs % 60:02}"
+def normalize_lat_lng(lat, lng):
+ if lat[0] not in "NS" or lng[0] not in "EW":
+ raise Exception("Lat/lng must be formatted as N12.345 and E67.890")
+ nlat = float(lat[1:]) * (-1 if lat[0] == "S" else 1)
+ nlng = float(lng[1:]) * (-1 if lng[0] == "W" else 1)
+ return nlat, nlng
+
+
def deg_to_cardinal(deg):
cardinals = [
"N",