-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
50 lines (41 loc) · 1.75 KB
/
main.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
from typing import Union
from fastapi import FastAPI
from starlette.responses import FileResponse
import dns.resolver
import re
app = FastAPI()
COORD_PATTERN = r"(\d+) (\d+) (\d+\.\d+) ([NS]) (\d+) (\d+) (\d+\.\d+) ([EW])"
def dms_to_decimal(degrees: int, minutes: int, seconds: float) -> float:
return degrees + minutes / 60 + seconds / 3600
@app.get("/")
async def read_index():
return FileResponse('map.html')
@app.get("/api/{domain}")
async def get_loc_record(domain: str) -> dict:
try:
response = dns.resolver.query(domain, "LOC")
loc_data = {}
for record in response:
full_record = str(record)
match = re.search(COORD_PATTERN, full_record)
if match:
latitude_degrees = int(match.group(1))
latitude_minutes = int(match.group(2))
latitude_seconds = float(match.group(3))
hemisphere_north_south = match.group(4)
longitude_degrees = int(match.group(5))
longitude_minutes = int(match.group(6))
longitude_seconds = float(match.group(7))
hemisphere_east_west = match.group(8)
latitude = dms_to_decimal(latitude_degrees, latitude_minutes, latitude_seconds)
if hemisphere_north_south == 'S':
latitude *= -1
longitude = dms_to_decimal(longitude_degrees, longitude_minutes, longitude_seconds)
if hemisphere_east_west == 'W':
longitude *= -1
loc_data["latitude"] = str(latitude)
loc_data["longitude"] = str(longitude)
loc_data['full'] = full_record
return loc_data
except Exception as e:
return {"error": str(e)}