Samuel Sloniker
1 year ago
4 changed files with 75 additions and 0 deletions
@ -0,0 +1,61 @@
|
||||
import os |
||||
import time |
||||
import json |
||||
import sqlite3 |
||||
import argparse |
||||
import requests |
||||
|
||||
parser = argparse.ArgumentParser() |
||||
parser.add_argument("config_file") |
||||
args = parser.parse_args() |
||||
|
||||
with open(args.config_file) as f: |
||||
config = json.load(f) |
||||
|
||||
try: |
||||
if config["pid_file"]: |
||||
with open(config["pid_file"], "w+") as f: |
||||
f.write(str(os.getpid())) |
||||
|
||||
con = sqlite3.connect(config["database"]) |
||||
|
||||
con.execute( |
||||
"CREATE TABLE IF NOT EXISTS subscriptions(phone VARCHAR, icao VARCHAR, description VARCHAR, last_seen INTEGER)" |
||||
) |
||||
con.commit() |
||||
|
||||
while True: |
||||
response = requests.get(config["data"]) |
||||
|
||||
planes = response.json()["aircraft"] |
||||
data = {plane["hex"]: plane for plane in planes} |
||||
|
||||
cur = con.execute( |
||||
"SELECT rowid, phone, icao, description, last_seen FROM subscriptions" |
||||
) |
||||
for sub_id, phone, icao, description, last_seen in cur.fetchall(): |
||||
if icao in data and data[icao]["seen"] < config["max_age"]: |
||||
if last_seen + config["min_disappearance"] < time.time(): |
||||
message = f"{description}\n{config['tracker']}?icao={icao}" |
||||
|
||||
request = { |
||||
"phone": phone, |
||||
"message": message, |
||||
"key": config["textbelt_key"], |
||||
} |
||||
|
||||
resp = requests.post("https://textbelt.com/text", request) |
||||
|
||||
print(f"{phone}: {message}") |
||||
print(resp.json()) |
||||
|
||||
con.execute( |
||||
"UPDATE subscriptions SET last_seen = ? WHERE rowid = ?", |
||||
(time.time(), sub_id), |
||||
) |
||||
con.commit() |
||||
|
||||
time.sleep(config["delay"]) |
||||
finally: |
||||
if config["pid_file"]: |
||||
os.unlink(config["pid_file"]) |
@ -0,0 +1,10 @@
|
||||
{ |
||||
"textbelt_key": "", |
||||
"data": "http://localhost/tar1090/data/aircraft.json", |
||||
"tracker": "https://globe.theairtraffic.com/", |
||||
"database": "subscriptions.db", |
||||
"pid_file": "pid.txt", |
||||
"max_age": 30, |
||||
"min_disappearance": 120, |
||||
"delay": 1 |
||||
} |
Loading…
Reference in new issue