-
Notifications
You must be signed in to change notification settings - Fork 4
/
lite.py
88 lines (74 loc) · 2.83 KB
/
lite.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
# sqlite db functionality
import sqlite3 as lite
class SqliteDB:
def __init__(self, bot=None):
self.bot = bot
self._prepare_database()
self.con.close()
def _prepare_database(self, create_string=None):
self._open()
if create_string:
self.cur.execute(create_string)
self.cur.execute(
'CREATE TABLE IF NOT EXISTS admins (id INTEGER PRIMARY KEY AUTOINCREMENT, username CHAR(32) DEFAULT NULL UNIQUE)')
self.cur.execute('CREATE TABLE IF NOT EXISTS img (id INTEGER PRIMARY KEY, url CHAR(128) DEFAULT NULL, user CHAR(32) DEFAULT NULL, time DATETIME DEFAULT CURRENT_TIMESTAMP, channel CHAR(32) DEFAULT NULL)')
self.cur.execute(
'CREATE TABLE IF NOT EXISTS lastfm (lastfm_username CHAR(64) NOT NULL, nick CHAR(32) NOT NULL)')
self.cur.execute(
'CREATE UNIQUE INDEX IF NOT EXISTS idx_irc_nick on lastfm(nick)')
self.cur.execute(
'CREATE TABLE IF NOT EXISTS qdb (id INTEGER PRIMARY KEY, quote TEXT NOT NULL, date DATETIME DEFAULT CURRENT_TIMESTAMP)')
self.cur.execute(
'CREATE TABLE IF NOT EXISTS qdb_votes (vote_id PRIMARY KEY, quote_id INTEGER NOT NULL, votes INTEGER DEFAULT 0)')
def _open(self):
# perhaps TODO
self.con = lite.connect(
self.bot.conf.getNick(
self.bot.network) + ".db",
check_same_thread=False)
self.cur = self.con.cursor()
def e(self, sql):
try:
self._open()
self.cur.execute(sql)
if "INSERT" in sql or "REPLACE" in sql:
self.con.commit()
self.con.close()
elif "SELECT" in sql:
e = self.cur.fetchall()
self.con.close()
return e
except Exception as e:
print(e)
self.con.rollback()
self.con.close()
return None
def insertImg(self, user, url, channel):
self._open()
if user == "" or user is None:
user = "nobody"
self.cur.execute(
"INSERT INTO img (user, url, channel) VALUES (?, ?, ?)", (user, url, channel))
self.con.commit()
self.con.close()
def getImgs(self):
self._open()
try:
self.cur.execute("SELECT * FROM img ORDER BY time DESC")
data = self.cur.fetchall()
self.con.close()
except BaseException:
self.con.close()
return None
return data
def isAdmin(self, username):
self._open()
try:
self.cur.execute(
"SELECT * FROM admins WHERE username = ?", (username,))
data = self.cur.fetchall()
self.con.close()
except Exception:
self.con.close()
return None
return data