48 lines
993 B
Python
Executable file
48 lines
993 B
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
import requests
|
|
import os
|
|
import time
|
|
from requests.auth import HTTPBasicAuth
|
|
from dotenv import load_dotenv
|
|
|
|
def get_latest_location():
|
|
|
|
r = requests.get(
|
|
os.getenv("URL"),
|
|
auth=HTTPBasicAuth(os.getenv("USERNAME"), os.getenv("PASSWORD")),
|
|
headers={
|
|
"Accept": "application/json",
|
|
"User-Agent": "owntracks-poller/1.0",
|
|
},
|
|
timeout=int(os.getenv("TIMEOUT")),
|
|
)
|
|
|
|
r.raise_for_status()
|
|
|
|
data = r.json()
|
|
|
|
if not data:
|
|
raise RuntimeError("No location data returned")
|
|
|
|
latest = data[0]
|
|
|
|
return {
|
|
"lat": latest.get("lat"),
|
|
"lon": latest.get("lon"),
|
|
"timestamp": latest.get("tst"),
|
|
"accuracy": latest.get("acc"),
|
|
"battery": latest.get("batt"),
|
|
}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
load_dotenv()
|
|
|
|
while True:
|
|
loc = get_latest_location()
|
|
print(
|
|
f"{loc['lat']}, {loc['lon']}"
|
|
)
|
|
time.sleep(1)
|