def send_to_airtable(fields):
payload = {
"records": [
{
"fields": fields
}
]
}
r = http://requests.post(AIRTABLE_URL, headers=headers, data=json.dumps(payload))
if r.status_code in (200, 201):
print("Uploaded:", fields.get("Hex"))
else:
print("Airtable ERROR:", r.status_code, r.text)
def fetch_data():
try:
r = requests.get(PIAWARE_URL, timeout=3)
r.raise_for_status()
data = r.json()
except Exception as e:
print("Error fetching:", e)
return
aircraft_list = data.get("aircraft", [])
for ac in aircraft_list:
hex_code = ac.get("hex")
if not hex_code:
continue
callsign = (ac.get("flight") or "").strip() or "N/A"
row = {
"UniqueID": hex_code,
"Callsign": callsign,
"FlightNumberIATA": callsign,
"Hex": hex_code,
"Status": "In Flight" if ac.get("alt_baro") else "N/A",
"Takeoff": None,
"Landing": None,
"FinalApproach": None,
"Airport": "N/A",
"Checking progress": False,
"IsInbound": False,
"IsOutbound": False,
"Note": "Detected via PiAware"
}
send_to_airtable(row)
def loop():
while True:
fetch_data()
time.sleep(30)
loop()