-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.py
82 lines (66 loc) · 2.78 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import base64
import os
import sqlite3
BOLUO_DB_PATH = os.path.expanduser('~/.hoshino/boluomi.db')
class BoluoCounter:
def __init__(self):
os.makedirs(os.path.dirname(BOLUO_DB_PATH), exist_ok=True)
self._create_table()
self._create_index()
def _connect(self):
return sqlite3.connect(BOLUO_DB_PATH)
def _create_table(self):
try:
self._connect().execute('''
CREATE TABLE IF NOT EXISTS BOLUO_DATA(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
BOLUO_NAME TEXT NOT NULL,
BOLUO_PIC TEXT NOT NULL,
BOLUO_TYPE TEXT NOT NULL,
BOLUO_TIME TEXT NOT NULL,
BOLUO_TITEL TEXT NOT NULL,
BOLUO_URL TEXT NOT NULL
);''')
except Exception as e:
raise e
def _create_index(self):
try:
self._connect().execute('''
CREATE UNIQUE INDEX IF NOT EXISTS indexname ON
BOLUO_DATA (
BOLUO_NAME,BOLUO_TYPE
);''')
except Exception as e:
raise e
def _add_boluo(self, boluo_name, boluo_pic, boluo_type, boluo_time, boluo_titel, boluo_url): #增加数据
try:
with self._connect() as conn:
conn.execute(
"INSERT OR REPLACE INTO BOLUO_DATA (BOLUO_NAME, BOLUO_PIC, BOLUO_TYPE, BOLUO_TIME, BOLUO_TITEL, BOLUO_URL) \
VALUES (?,?,?,?,?,?)", [boluo_name, boluo_pic, boluo_type, boluo_time, boluo_titel, boluo_url]
)
except Exception as e:
raise e
def _get_boluo_data(self, boluo_name):
try:
r = self._connect().execute("SELECT BOLUO_NAME, BOLUO_PIC, BOLUO_TYPE, BOLUO_TIME, BOLUO_TITEL, BOLUO_URL FROM BOLUO_DATA WHERE BOLUO_NAME=?", [boluo_name]).fetchall()
return [] if r is None else r
except Exception as e:
raise e
def _like_boluo_data(self, boluo_msg):
try:
r = self._connect().execute(f"SELECT BOLUO_NAME, BOLUO_PIC, BOLUO_TYPE, BOLUO_TIME, BOLUO_TITEL, BOLUO_URL FROM BOLUO_DATA WHERE BOLUO_NAME LIKE '%{boluo_msg}%' OR BOLUO_TITEL LIKE '%{boluo_msg}%' COLLATE NOCASE").fetchall()
return [] if r is None else r
except Exception as e:
raise e
def add_boluo(boluo_name, boluo_pic, boluo_type, boluo_time, boluo_titel, boluo_url):
PC = BoluoCounter()
PC._add_boluo(boluo_name, boluo_pic, boluo_type, boluo_time, boluo_titel, boluo_url)
def get_boluo(boluo_name):
PC = BoluoCounter()
R = PC._get_boluo_data(boluo_name)
print(R)
def like_boluo(boluo_msg):
PC = BoluoCounter()
R = PC._like_boluo_data(boluo_msg)
return R