diff --git a/.gitignore b/.gitignore index 55be276..92e76b4 100644 --- a/.gitignore +++ b/.gitignore @@ -152,3 +152,6 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +config.json +subscriptions.db +pid.txt diff --git a/adsms.py b/adsms.py new file mode 100644 index 0000000..48fdae3 --- /dev/null +++ b/adsms.py @@ -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"]) diff --git a/example_config.json b/example_config.json new file mode 100644 index 0000000..ef98b8c --- /dev/null +++ b/example_config.json @@ -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 +} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f229360 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests