Separate database access into separate file
This commit is contained in:
parent
5748696970
commit
422f987888
|
@ -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)
|
||||
|
|
37
adsms/db.py
Normal file
37
adsms/db.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user