adsms/adsms/db.py

49 lines
1.0 KiB
Python

import collections
import sqlite3
import time
Subscription = collections.namedtuple(
"Subscription",
[
"id",
"phone",
"icao",
"description",
"last_seen",
"platform",
],
)
def load_database(file_name):
con = sqlite3.connect(file_name)
cur = con.execute(
"CREATE TABLE IF NOT EXISTS subscriptions(phone VARCHAR, icao VARCHAR, description VARCHAR, last_seen INTEGER)"
)
try:
cur.execute(
"ALTER TABLE subscriptions ADD COLUMN platform DEFAULT 'textbelt'"
)
except sqlite3.OperationalError:
pass
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, platform FROM subscriptions"
).fetchall():
yield Subscription(*subscription)