Separate database access into separate file

This commit is contained in:
Samuel Sloniker 2023-05-12 18:37:53 -07:00
parent 5748696970
commit 422f987888
2 changed files with 43 additions and 26 deletions

View File

@ -5,9 +5,9 @@
import os import os
import time import time
import json import json
import sqlite3
import argparse import argparse
import requests import requests
import adsms.db
def load_config(config_file): def load_config(config_file):
@ -15,24 +15,6 @@ def load_config(config_file):
return json.load(f) 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): def send_text_message(phone, message, key):
request = {"phone": phone, "message": message, "key": key} request = {"phone": phone, "message": message, "key": key}
resp = requests.post("https://textbelt.com/text", request) 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): def process_subscriptions(con, config, data):
cur = con.execute( for sub_id, phone, icao, description, last_seen in db.get_subscriptions(
"SELECT rowid, phone, icao, description, last_seen FROM subscriptions" con
) ):
for sub_id, phone, icao, description, last_seen in cur.fetchall():
if icao in data and data[icao]["seen"] < config["max_age"]: if icao in data and data[icao]["seen"] < config["max_age"]:
if last_seen + config["min_disappearance"] < time.time(): if last_seen + config["min_disappearance"] < time.time():
message = f"{description}\n{config['tracker']}?icao={icao}" message = f"{description}\n{config['tracker']}?icao={icao}"
@ -53,7 +33,7 @@ def process_subscriptions(con, config, data):
print(f"{phone}: {message}") print(f"{phone}: {message}")
update_last_seen_time(con, sub_id) db.update_last_seen_time(con, sub_id)
con.commit() con.commit()
@ -65,7 +45,7 @@ def get_current_data(config):
def run(config): def run(config):
con = load_database(config) con = adsms.db.load_database(config["database"])
while True: while True:
data = get_current_data(config) data = get_current_data(config)

37
adsms/db.py Normal file
View File

@ -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)