-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
128 lines (102 loc) · 2.68 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from pathlib import Path
from datetime import datetime
import logging
import os
import geocoder
from msgspec import Struct
import ephem
from litestar import Litestar, get
from litestar.static_files.config import StaticFilesConfig
log = logging.getLogger(__name__)
env = os.environ.get("ENVIRONMENT", "local")
if env == "local":
public_dir = Path("./client/dist").resolve()
else:
public_dir = Path("/app/dist").resolve()
log.info(f"public_dir={public_dir}")
class AstronObjectRequest(Struct):
name: str
lon: str
lat: str
elevation: float
when: datetime
class AstronObjectResponse(Struct):
name: str
magnitude: str
size: str
az: str
el: str
ra: str
dec: str
setting_time: datetime
rising_time: datetime
when: datetime
class SearchItem(Struct):
name: str
country: str
sub_division: str
lat: float
lon: float
class SearchResponse(Struct):
items: list[SearchItem]
def init_observer():
observer = ephem.Observer()
observer.pressure = 0
observer.epoch = ephem.J2000
return observer
@get("/search")
async def search(
q: str,
max_results: int = 1,
fuzzy: float = 1.0
) -> SearchResponse:
results = geocoder.geonames(q, key="dillpickle", maxRows=max_results, fuzzy=fuzzy)
items = [SearchItem(
name=g.address,
country=g.country,
sub_division=g.state,
lat=float(g.lat),
lon=float(g.lng)
) for g in results]
return SearchResponse(items=items)
@get('/get_astron_object_data')
async def get_astron_object_data(
name: str,
lon: float,
lat: float,
elevation: float,
when: datetime,
) -> AstronObjectResponse:
log.debug(f"get_astron_object_data")
observer = init_observer()
# we have to do a string conversion for pyephem to work!
observer.lon = str(lon)
observer.lat = str(lat)
observer.elevation = elevation
observer.date = when
astron_obj = getattr(ephem, name.capitalize())()
astron_obj.compute(observer)
res = AstronObjectResponse(
astron_obj.name,
astron_obj.mag,
astron_obj.size,
astron_obj.az,
astron_obj.alt,
astron_obj.ra,
astron_obj.dec,
datetime.strptime(str(observer.next_setting(astron_obj)), "%Y/%m/%d %H:%M:%S"),
datetime.strptime(str(observer.next_rising(astron_obj)), "%Y/%m/%d %H:%M:%S"),
when
)
log.debug(f"get_astron_object_data: {res=}")
return res
app = Litestar(
route_handlers=[search, get_astron_object_data],
static_files_config=[
StaticFilesConfig(
directories=[public_dir],
path="/",
html_mode=True
),
]
)