Upload deploy/ingest/flights.py with huggingface_hub
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758"""OpenSky:全球在空航班位置。
匿名配额按积分算(全球一次 4 分,约 100 次/天),所以 20 分钟拉一次,写在 run.py 的鲜度判断里。输出 /data/apps/globe/data/flights.json。
字段用定长数组,前端按顺序取值: [lon, lat, 高度m, 速度m/s, 航向deg, 呼号, 国家, 应答机]"""import jsonimport osimport sysimport timeimport urllib.errorimport urllib.request
OUT = "/data/apps/globe/data"URL = "https://opensky-network.org/api/states/all"UA = "Mozilla/5.0 (compatible; geoevents/1.0; research)"
def main(): os.makedirs(OUT, exist_ok=True) req = urllib.request.Request(URL, headers={"User-Agent": UA}) try: with urllib.request.urlopen(req, timeout=90) as r: data = json.loads(r.read().decode("utf-8")) except urllib.error.HTTPError as e: print("opensky http", e.code, file=sys.stderr) sys.exit(1)
states = data.get("states") or [] out = [] for s in states: try: lon, lat = s[5], s[6] if lon is None or lat is None or s[8]: # 8 = on_ground continue alt = s[7] if s[7] is not None else 0 vel = s[9] if s[9] is not None else 0 trk = s[10] if s[10] is not None else 0 out.append([round(lon, 3), round(lat, 3), int(alt), int(vel), int(trk), (s[1] or "").strip(), s[2] or "", s[14] or ""]) except (IndexError, TypeError): continue
payload = {"generated": int(time.time()), "count": len(out), "flights": out} with open(os.path.join(OUT, "flights.json"), "w", encoding="utf-8") as f: json.dump(payload, f, ensure_ascii=False, separators=(",", ":")) emg = sum(1 for f in out if f[7] in ("7700", "7600", "7500")) print(json.dumps({"raw": len(states), "count": len(out), "emergency": emg}))
if __name__ == "__main__": try: main() except Exception as e: # noqa: BLE001 print("flights failed:", repr(e)[:200], file=sys.stderr) sys.exit(1)deploy/ingest/flights.pyAdded+58