2025-11-14 04:03:20 +00:00
|
|
|
import os
|
|
|
|
|
import sqlite3
|
2025-11-14 04:03:20 +00:00
|
|
|
import datetime
|
2025-11-14 04:03:20 +00:00
|
|
|
|
|
|
|
|
SQLITE_DB = os.environ.get('SQLITE_DB', 'db.sqlite3')
|
|
|
|
|
|
|
|
|
|
TABLE_REPEATS = '''
|
|
|
|
|
CREATE TABLE IF NOT EXISTS repeats (
|
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
probability FLOAT NOT NULL,
|
|
|
|
|
orders TEXT NOT NULL,
|
|
|
|
|
count INTEGER DEFAULT 0
|
|
|
|
|
);
|
|
|
|
|
'''
|
|
|
|
|
|
2025-11-14 04:03:20 +00:00
|
|
|
TABLE_ORDER_STATUS = '''
|
|
|
|
|
CREATE TABLE IF NOT EXISTS order_status (
|
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
mastodon_id TEXT NOT NULL,
|
2025-11-14 04:03:20 +00:00
|
|
|
created_at TIMESTAMP NOT NULL,
|
2025-11-14 04:03:20 +00:00
|
|
|
text TEXT NOT NULL,
|
2025-11-14 04:03:20 +00:00
|
|
|
confirmed_at TIMESTAMP,
|
|
|
|
|
punishment_id INTEGER,
|
|
|
|
|
FOREIGN KEY(punishment_id) REFERENCES punishment_status(id)
|
|
|
|
|
);
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
TABLE_PUNISHMENT_STATUS = '''
|
|
|
|
|
CREATE TABLE IF NOT EXISTS punishment_status (
|
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
mastodon_id TEXT NOT NULL,
|
|
|
|
|
created_at TIMESTAMP NOT NULL,
|
|
|
|
|
text TEXT NOT NULL,
|
|
|
|
|
confirmed_at TIMESTAMP
|
2025-11-14 04:03:20 +00:00
|
|
|
);
|
|
|
|
|
'''
|
|
|
|
|
|
2025-11-14 04:03:20 +00:00
|
|
|
class Database:
|
|
|
|
|
def __init__(self):
|
2025-11-14 04:03:20 +00:00
|
|
|
self.conn = sqlite3.connect(SQLITE_DB)
|
2025-11-14 04:03:20 +00:00
|
|
|
self.conn.row_factory = sqlite3.Row
|
|
|
|
|
self.table_init(TABLE_REPEATS)
|
2025-11-14 04:03:20 +00:00
|
|
|
self.table_init(TABLE_PUNISHMENT_STATUS)
|
2025-11-14 04:03:20 +00:00
|
|
|
self.table_init(TABLE_ORDER_STATUS)
|
2025-11-14 04:03:20 +00:00
|
|
|
|
|
|
|
|
def table_init(self, table_sql):
|
|
|
|
|
c = self.conn.cursor()
|
|
|
|
|
c.execute(table_sql)
|
|
|
|
|
self.conn.commit()
|
|
|
|
|
|
2025-11-14 04:03:20 +00:00
|
|
|
def update(self, sql, args=[]):
|
|
|
|
|
c = self.conn.cursor()
|
|
|
|
|
c.execute(sql, args)
|
|
|
|
|
self.conn.commit()
|
2025-11-14 04:03:20 +00:00
|
|
|
return c.lastrowid
|
2025-11-14 04:03:20 +00:00
|
|
|
|
2025-11-14 04:03:20 +00:00
|
|
|
def repeat_get(self):
|
|
|
|
|
c = self.conn.cursor()
|
|
|
|
|
sql = 'SELECT id, probability, orders, count FROM repeats LIMIT 1'
|
|
|
|
|
c.execute(sql)
|
|
|
|
|
return c.fetchone()
|
|
|
|
|
|
|
|
|
|
def repeat_increment(self):
|
2025-11-14 04:03:20 +00:00
|
|
|
self.update('UPDATE repeats SET count = count + 1')
|
2025-11-14 04:03:20 +00:00
|
|
|
|
|
|
|
|
def repeat_put(self, probability, orders):
|
2025-11-14 04:03:20 +00:00
|
|
|
self.update(
|
|
|
|
|
'INSERT INTO repeats (probability, orders) VALUES (?, ?);',
|
|
|
|
|
[probability, orders]
|
|
|
|
|
)
|
2025-11-14 04:03:20 +00:00
|
|
|
|
|
|
|
|
def repeat_clear(self):
|
2025-11-14 04:03:20 +00:00
|
|
|
self.update('DELETE FROM repeats')
|
|
|
|
|
|
|
|
|
|
def order_status_put(self, mastodon_id, created_at, text):
|
|
|
|
|
self.update(
|
|
|
|
|
'''
|
|
|
|
|
INSERT INTO order_status
|
|
|
|
|
(mastodon_id, created_at, text)
|
|
|
|
|
VALUES (?, ?, ?);
|
|
|
|
|
''',
|
2025-11-14 04:03:20 +00:00
|
|
|
[
|
|
|
|
|
mastodon_id,
|
|
|
|
|
created_at,
|
|
|
|
|
text
|
|
|
|
|
]
|
2025-11-14 04:03:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def order_status_outstanding(self):
|
2025-11-14 04:03:20 +00:00
|
|
|
c = self.conn.cursor()
|
2025-11-14 04:03:20 +00:00
|
|
|
sql = '''
|
|
|
|
|
SELECT id, mastodon_id, created_at, confirmed_at
|
|
|
|
|
FROM order_status
|
2025-11-14 04:03:20 +00:00
|
|
|
WHERE confirmed_at IS NULL AND punishment_id IS NULL
|
2025-11-14 04:03:20 +00:00
|
|
|
'''
|
2025-11-14 04:03:20 +00:00
|
|
|
c.execute(sql)
|
2025-11-14 04:03:20 +00:00
|
|
|
return c.fetchall()
|
|
|
|
|
|
|
|
|
|
def order_status_confirm(self, id, confirmed_at):
|
|
|
|
|
self.update(
|
|
|
|
|
'''
|
|
|
|
|
UPDATE order_status
|
|
|
|
|
SET confirmed_at=?
|
|
|
|
|
WHERE id=?;
|
|
|
|
|
''',
|
2025-11-14 04:03:20 +00:00
|
|
|
[
|
|
|
|
|
confirmed_at,
|
|
|
|
|
id
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def punishment_status_put(self, order_status_id, mastodon_id, created_at, text):
|
|
|
|
|
punishment_status_id = self.update(
|
|
|
|
|
'''
|
|
|
|
|
INSERT INTO punishment_status
|
|
|
|
|
(mastodon_id, created_at, text)
|
|
|
|
|
VALUES (?, ?, ?);
|
|
|
|
|
''',
|
|
|
|
|
[
|
|
|
|
|
mastodon_id,
|
|
|
|
|
created_at,
|
|
|
|
|
text
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
self.update(
|
|
|
|
|
'''
|
|
|
|
|
UPDATE order_status
|
|
|
|
|
SET punishment_id=?
|
|
|
|
|
WHERE id=?
|
|
|
|
|
''',
|
|
|
|
|
[punishment_status_id, order_status_id]
|
|
|
|
|
)
|