forked from Aivoapina/imneversorry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
170 lines (141 loc) · 4.84 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import sqlite3 as sq
import datetime as dt
from contextlib import contextmanager
@contextmanager
def cursor():
try:
conn = sq.connect('bot.db')
cur = conn.cursor()
yield cur
conn.commit()
finally:
conn.close()
def addRip(type, rip, channel, creator):
with cursor() as cur:
date = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cur.execute('INSERT INTO Rip values(?, ?, ?, ?, ?)',
(rip, type, date, channel, creator))
def delRip(delrip):
with cursor() as cur:
cur.execute('DELETE FROM Rip WHERE type = ? and rip = ?', (delrip))
def readRips():
with cursor() as cur:
cur.execute('SELECT type, rip, channel from Rip')
rows = cur.fetchall()
data = {}
for row in rows:
type, rip, channel = row
if channel not in data:
data[channel] = set()
data[channel].add((type, rip))
return data
def readViisaudet():
with cursor() as cur:
cur.execute('SELECT viisaus from Viisaus')
rows = cur.fetchall()
return set(rows)
def readSanat():
with cursor() as cur:
cur.execute('SELECT sana from Sana')
rows = cur.fetchall()
return set(rows)
def upsertOppi(keyword, definition, channel, creator):
with cursor() as cur:
date = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cur.execute('INSERT OR REPLACE INTO Oppi values(?, ?, ?, ?, ?)',
(keyword, definition, date, channel, creator))
def findOppi(keyword, channel):
with cursor() as cur:
cur.execute('SELECT definition FROM Oppi WHERE keyword=? and channel=?', (keyword, channel))
return cur.fetchone()
def countOpis(channel):
with cursor() as cur:
cur.execute('SELECT COUNT(*) AS count FROM Oppi WHERE channel=?', (channel,))
count = cur.fetchone()
return count
def randomOppi(channel):
with cursor() as cur:
cur.execute('SELECT keyword, definition FROM Oppi WHERE channel=? ORDER BY RANDOM() LIMIT 1', (channel,))
return cur.fetchone()
def insertQuote(quote, quotee, channel, creator):
with cursor() as cur:
date = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cur.execute('INSERT INTO Quote values(?, ?, ?, ?, ?)',
(quote, quotee, date, channel, creator))
def findQuotes(channel, quotee=None):
with cursor() as cur:
if quotee is not None:
cur.execute('SELECT quote, quotee FROM Quote WHERE channel=? AND upper(quotee) = upper(?)', (channel, quotee))
return cur.fetchall()
else:
cur.execute('SELECT quote, quotee FROM Quote WHERE channel=?', (channel,))
return cur.fetchall()
def countQuotes(channel):
with cursor() as cur:
cur.execute('SELECT count(quote) FROM Quote WHERE channel=?', (channel,))
return cur.fetchone()[0]
def readDiagnoosit():
with cursor() as cur:
cur.execute('SELECT diagnoosi from Diagnoosi')
rows = cur.fetchall()
return set(rows)
def readMaidot():
with cursor() as cur:
cur.execute('SELECT maito from Maito')
rows = cur.fetchall()
return set(rows)
def readNimet():
with cursor() as cur:
cur.execute('SELECT nimi from Nimi')
rows = cur.fetchall()
return set(rows)
def readKalat():
with cursor() as cur:
cur.execute('SELECT kala from Kalat')
rows = cur.fetchall()
return set(rows)
def readVihanneet():
with cursor() as cur:
cur.execute('SELECT nimi from Vihannes')
rows = cur.fetchall()
return set(rows)
def readPlanetoidit():
with cursor() as cur:
cur.execute('SELECT nimi from Planetoidi')
rows = cur.fetchall()
return set(rows)
def readKulkuneuvot():
with cursor() as cur:
cur.execute('SELECT nimi from Kulkuneuvo')
rows = cur.fetchall()
return set(rows)
def readLinnut():
with cursor() as cur:
cur.execute('SELECT nimi from Linnut')
rows = cur.fetchall()
return set(rows)
def readSotilasarvot():
with cursor() as cur:
cur.execute('SELECT nimi from Arvonimet')
rows = cur.fetchall()
return set(rows)
def readSotilasnimet():
with cursor() as cur:
cur.execute('SELECT nimi from Sotilasnimet')
rows = cur.fetchall()
return set(rows)
def readEnnustukset():
with cursor() as cur:
cur.execute('SELECT rivi from Ennustus')
rows = cur.fetchall()
return set(rows)
def readNakutukset():
with cursor() as cur:
cur.execute('SELECT nakutus from Nakutukset')
rows = cur.fetchall()
return set(rows)
def readDefinitions(channel):
with cursor() as cur:
cur.execute('SELECT definition, keyword from Oppi where channel=?', (channel, ))
rows = cur.fetchall()
return rows