Files
chanbans/chanbans/db.py
Alek Ratzloff c5e7080663 http: Add base HTTP server implementation.
* Add HTTP server base implementation with some basic pages
* Add Jinja2 dependency
* Things are mostly working, except for static resources. Those will
  have to come next.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2023-07-31 18:04:53 -07:00

107 lines
2.6 KiB
Python

import sqlite3
import sys
from typing import Optional
DB_PATH = "bans.db"
def get_db(db_path: str = DB_PATH):
db = sqlite3.connect(db_path)
# ensure that the database exists
db.executescript(
"""
create table if not exists bans(
id integer primary key,
action varchar(5),
board varchar(10),
length varchar(10),
now varchar(30),
name varchar(100),
trip varchar(30),
com text,
time varchar(30),
sub varchar(100),
nsfw boolean,
thumb varchar(100),
ext varchar(10),
w int,
h int,
tn_w int,
tn_h int,
md5 varchar(100),
fsize integer,
filename text,
tim varchar(30),
thumb_path text,
reason varchar(200),
op boolean default 0
);
"""
)
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
db.row_factory = dict_factory
return db
def search_db(
board: str = "",
reason: str = "",
name: str = "",
trip: str = "",
com: str = "",
sub: str = "",
time_before: int = sys.maxsize,
time_after: int = 0,
md5: str = "",
limit: int = 100,
page: int = 0,
):
db = get_db()
result = db.execute(
"""
select *
from bans
where
(:board = "" or board = :board)
and (:reason = "" or reason like :reason_like)
and (:name = "" or name like :name_like)
and (:trip = "" or trip like :trip_like)
and (:com = "" or com like :com_like)
and (:sub = "" or sub like :sub_like)
and (time <= :time_before)
and (time >= :time_after)
and (:md5 = "" or md5 like :md5_like)
order by id desc
limit :limit
offset :offset
""",
{
"board": board,
"reason": reason,
"reason_like": f"%{reason}%",
"name": name,
"name_like": f"%{name}%",
"trip": trip,
"trip_like": f"%{trip}%",
"com": com,
"com_like": f"%{com}%",
"sub": sub,
"sub_like": f"%{sub}%",
"time_before": time_before,
"time_after": time_after,
"md5": md5,
"md5_like": f"%{md5}%",
"limit": limit,
"offset": limit * page,
}
)
return list(result.fetchall())