mirror of
https://github.com/TheCommsChannel/TC2-APRS-BBS.git
synced 2025-07-22 22:31:00 -04:00
Add files via upload
This commit is contained in:
commit
635e7f42d2
9 changed files with 2399 additions and 0 deletions
81
database.py
Normal file
81
database.py
Normal file
|
@ -0,0 +1,81 @@
|
|||
import sqlite3
|
||||
from datetime import datetime, timedelta
|
||||
import config
|
||||
|
||||
def init_db():
|
||||
conn = sqlite3.connect('aprs.db')
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS bulletins (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
text TEXT NOT NULL,
|
||||
poster TEXT NOT NULL,
|
||||
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
""")
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
sender TEXT NOT NULL,
|
||||
recipient TEXT NOT NULL,
|
||||
text TEXT NOT NULL,
|
||||
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
""")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def format_timestamp(timestamp):
|
||||
dt = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S")
|
||||
return dt.strftime("%b%d %H:%M").upper()
|
||||
|
||||
def add_bulletin(callsign, text):
|
||||
conn = sqlite3.connect('aprs.db')
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("INSERT INTO bulletins (text, poster) VALUES (?, ?)", (text, callsign))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
def get_bulletins():
|
||||
conn = sqlite3.connect('aprs.db')
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT text, poster, timestamp FROM bulletins ORDER BY timestamp DESC")
|
||||
results = cursor.fetchall()
|
||||
conn.close()
|
||||
return [(text, poster, format_timestamp(ts)) for text, poster, ts in results]
|
||||
|
||||
|
||||
def add_message(sender, recipient, text):
|
||||
conn = sqlite3.connect('aprs.db')
|
||||
cursor = conn.cursor()
|
||||
normalized_recipient = normalize_callsign(recipient)
|
||||
cursor.execute("INSERT INTO messages (sender, recipient, text) VALUES (?, ?, ?)", (sender, normalized_recipient, text))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def get_messages_for_user(callsign):
|
||||
conn = sqlite3.connect('aprs.db')
|
||||
cursor = conn.cursor()
|
||||
normalized_callsign = normalize_callsign(callsign)
|
||||
cursor.execute("""
|
||||
SELECT sender, text, timestamp
|
||||
FROM messages
|
||||
WHERE UPPER(recipient) = ?
|
||||
ORDER BY timestamp DESC
|
||||
""", (normalized_callsign,))
|
||||
results = cursor.fetchall()
|
||||
conn.close()
|
||||
return [(normalize_callsign(sender), text, format_timestamp(ts)) for sender, text, ts in results]
|
||||
|
||||
def normalize_callsign(callsign):
|
||||
return callsign.split('-')[0].upper()
|
||||
|
||||
def delete_expired_bulletins():
|
||||
"""Delete bulletins older than the configured expiration time."""
|
||||
expiration_threshold = datetime.now() - timedelta(days=config.BULLETIN_EXPIRATION_DAYS)
|
||||
conn = sqlite3.connect('aprs.db')
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("DELETE FROM bulletins WHERE timestamp < ?", (expiration_threshold,))
|
||||
conn.commit()
|
||||
conn.close()
|
Loading…
Add table
Add a link
Reference in a new issue