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"])