This commit is contained in:
2026-08-12 18:22:26 -05:00
parent 7e3b3dff6d
commit 9cc7093c48
6 changed files with 102 additions and 39 deletions
+22 -21
View File
@@ -13,6 +13,7 @@ import asyncio
from nio import AsyncClient, LoginResponse, RoomMessageText, SyncResponse, RoomSendResponse
from post import Post
import requests
import dbutil
default_search_terms = ["!suggest", "!Suggest", "!suggestion", "!Suggestion", "SUGGEST", "!sug"]
@@ -61,7 +62,7 @@ class Room:
return f"suggestions_{safe}.db"
def init_db(self):
conn = sqlite3.connect(self.db_path)
conn = dbutil.connect(self.db_path)
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS suggestions (
@@ -137,7 +138,7 @@ class Room:
UNIQUE(sender, timestamp)
)
''')
conn.commit()
dbutil.commit(conn)
conn.close()
async def login_to_matrix(self):
@@ -297,7 +298,7 @@ class Room:
def write_to_db(self):
conn = sqlite3.connect(self.db_path)
conn = dbutil.connect(self.db_path)
c = conn.cursor()
self.newly_inserted_posts = []
@@ -311,12 +312,12 @@ class Room:
except sqlite3.IntegrityError:
continue
conn.commit()
dbutil.commit(conn)
conn.close()
def update_last_run(self):
room_id = self.config.room_id
conn = sqlite3.connect("last_run.db")
conn = dbutil.connect("last_run.db")
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS last_run (
@@ -333,12 +334,12 @@ class Room:
now = int(_time.time())
now_str = datetime.datetime.now().isoformat(sep=' ', timespec='seconds')
c.execute('INSERT OR REPLACE INTO last_run (room_id, timestamp, datetime) VALUES (?, ?, ?)', (room_id, now, now_str))
conn.commit()
dbutil.commit(conn)
conn.close()
def get_last_run(self):
room_id = self.config.room_id
conn = sqlite3.connect("last_run.db")
conn = dbutil.connect("last_run.db")
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS last_run (
@@ -362,7 +363,7 @@ class Room:
# New: record a !gettitles detection; returns True if newly recorded (not duplicate)
def record_gettitles(self, sender, timestamp, event_id=None):
conn = sqlite3.connect(self.db_path)
conn = dbutil.connect(self.db_path)
c = conn.cursor()
dt_str = datetime.datetime.fromtimestamp(int(timestamp)).isoformat(sep=' ', timespec='seconds')
c.execute('''
@@ -370,14 +371,14 @@ class Room:
VALUES (?, ?, ?, ?)
''', (event_id, sender, int(timestamp), dt_str))
inserted = c.rowcount == 1
conn.commit()
dbutil.commit(conn)
conn.close()
return inserted
# New: record a !mayhem detection; returns True if newly recorded (not duplicate)
def record_mayhem(self, sender, timestamp, event_id=None):
"""Record a !mayhem detection; returns True if newly recorded (not duplicate)"""
conn = sqlite3.connect(self.db_path)
conn = dbutil.connect(self.db_path)
c = conn.cursor()
dt_str = datetime.datetime.fromtimestamp(int(timestamp)).isoformat(sep=' ', timespec='seconds')
c.execute('''
@@ -385,14 +386,14 @@ class Room:
VALUES (?, ?, ?, ?)
''', (event_id, sender, int(timestamp), dt_str))
inserted = c.rowcount == 1
conn.commit()
dbutil.commit(conn)
conn.close()
return inserted
# New: record a !countvotes detection; returns True if newly recorded (not duplicate)
def record_countvotes(self, sender, timestamp, event_id=None):
"""Record a !countvotes detection; returns True if newly recorded (not duplicate)"""
conn = sqlite3.connect(self.db_path)
conn = dbutil.connect(self.db_path)
c = conn.cursor()
dt_str = datetime.datetime.fromtimestamp(int(timestamp)).isoformat(sep=' ', timespec='seconds')
c.execute('''
@@ -400,14 +401,14 @@ class Room:
VALUES (?, ?, ?, ?)
''', (event_id, sender, int(timestamp), dt_str))
inserted = c.rowcount == 1
conn.commit()
dbutil.commit(conn)
conn.close()
return inserted
# New: record a !voteurl detection; returns True if newly recorded (not duplicate)
def record_voteurl(self, sender, timestamp, event_id=None):
"""Record a !voteurl detection; returns True if newly recorded (not duplicate)"""
conn = sqlite3.connect(self.db_path)
conn = dbutil.connect(self.db_path)
c = conn.cursor()
dt_str = datetime.datetime.fromtimestamp(int(timestamp)).isoformat(sep=' ', timespec='seconds')
c.execute('''
@@ -415,7 +416,7 @@ class Room:
VALUES (?, ?, ?, ?)
''', (event_id, sender, int(timestamp), dt_str))
inserted = c.rowcount == 1
conn.commit()
dbutil.commit(conn)
conn.close()
return inserted
@@ -611,7 +612,7 @@ class Room:
self.formatted_sugs = "\n".join(lines)
def read_db(self, since_timestamp=None, min_timestamp=None, only_unposted=False):
conn = sqlite3.connect(self.db_path)
conn = dbutil.connect(self.db_path)
c = conn.cursor()
query = "SELECT user, display_name, message, timestamp, age, is_posted FROM suggestions"
params = []
@@ -633,7 +634,7 @@ class Room:
self.posts = [Post(poster=row[0], display_name=row[1], content=row[2], time=row[3], age=row[4], is_posted=bool(row[5])) for row in rows]
def get_last_post(self):
conn = sqlite3.connect("last_post.db")
conn = dbutil.connect("last_post.db")
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS last_post (
@@ -654,7 +655,7 @@ class Room:
return None
def update_last_post(self):
conn = sqlite3.connect("last_post.db")
conn = dbutil.connect("last_post.db")
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS last_post (
@@ -672,7 +673,7 @@ class Room:
now_str = datetime.datetime.now().isoformat(sep=' ', timespec='seconds')
c.execute('INSERT OR REPLACE INTO last_post (room_id, timestamp, datetime) VALUES (?, ?, ?)',
(self.config.room_id, now, now_str))
conn.commit()
dbutil.commit(conn)
conn.close()
async def post_suggestions(self):
@@ -787,14 +788,14 @@ class Room:
if posts is None:
posts = self.posts
conn = sqlite3.connect(self.db_path)
conn = dbutil.connect(self.db_path)
c = conn.cursor()
for post in posts:
c.execute(
"UPDATE suggestions SET is_posted = 1 WHERE user = ? AND message = ? AND timestamp = ?",
(post.poster, post.content, post.time)
)
conn.commit()
dbutil.commit(conn)
conn.close()
async def close(self):