From 422f98788871e7a7f6800ebcbb38e35d5332ab0b Mon Sep 17 00:00:00 2001 From: Samuel Sloniker Date: Fri, 12 May 2023 18:37:53 -0700 Subject: [PATCH] Separate database access into separate file --- adsms/__init__.py | 32 ++++++-------------------------- adsms/db.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 adsms/db.py diff --git a/adsms/__init__.py b/adsms/__init__.py index f10efc9..511285b 100755 --- a/adsms/__init__.py +++ b/adsms/__init__.py @@ -5,9 +5,9 @@ import os import time import json -import sqlite3 import argparse import requests +import adsms.db def load_config(config_file): @@ -15,24 +15,6 @@ def load_config(config_file): return json.load(f) -def load_database(config): - con = sqlite3.connect(config["database"]) - - con.execute( - "CREATE TABLE IF NOT EXISTS subscriptions(phone VARCHAR, icao VARCHAR, description VARCHAR, last_seen INTEGER)" - ) - con.commit() - - return con - - -def update_last_seen_time(con, sub_id): - con.execute( - "UPDATE subscriptions SET last_seen = ? WHERE rowid = ?", - (time.time(), sub_id), - ) - - def send_text_message(phone, message, key): request = {"phone": phone, "message": message, "key": key} resp = requests.post("https://textbelt.com/text", request) @@ -40,11 +22,9 @@ def send_text_message(phone, message, key): def process_subscriptions(con, config, data): - cur = con.execute( - "SELECT rowid, phone, icao, description, last_seen FROM subscriptions" - ) - - for sub_id, phone, icao, description, last_seen in cur.fetchall(): + for sub_id, phone, icao, description, last_seen in db.get_subscriptions( + con + ): 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}" @@ -53,7 +33,7 @@ def process_subscriptions(con, config, data): print(f"{phone}: {message}") - update_last_seen_time(con, sub_id) + db.update_last_seen_time(con, sub_id) con.commit() @@ -65,7 +45,7 @@ def get_current_data(config): def run(config): - con = load_database(config) + con = adsms.db.load_database(config["database"]) while True: data = get_current_data(config) diff --git a/adsms/db.py b/adsms/db.py new file mode 100644 index 0000000..e356ef5 --- /dev/null +++ b/adsms/db.py @@ -0,0 +1,37 @@ +import collections + +Subscription = collections.namedtuple( + "Subscription", + [ + "id", + "phone", + "icao", + "description", + "last_seen", + ], +) + + +def load_database(file_name): + con = sqlite3.connect(file_name) + + con.execute( + "CREATE TABLE IF NOT EXISTS subscriptions(phone VARCHAR, icao VARCHAR, description VARCHAR, last_seen INTEGER)" + ) + con.commit() + + return con + + +def update_last_seen_time(con, sub_id): + con.execute( + "UPDATE subscriptions SET last_seen = ? WHERE rowid = ?", + (time.time(), sub_id), + ) + + +def get_subscriptions(con): + for subscription in con.execute( + "SELECT rowid, phone, icao, description, last_seen FROM subscriptions" + ).fetchall(): + return Subscription(*subscription)