-
Notifications
You must be signed in to change notification settings - Fork 4
/
db.py
177 lines (155 loc) · 5.02 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
171
172
173
174
175
176
177
from logger import Logger
try:
import MySQLdb as mdb
except ImportError:
import sys
imported = False
if "lite" not in sys.modules:
print("Could not find MySQLdb and lite is not imported! Erroring out!")
from datetime import datetime, timedelta
class DB:
"""
Handles connecting to the database and reading and writing data.
Currently supports only MySQL/mariadb, and that probably needs to change.
"""
age = datetime.now()
def __init__(self, bot=None):
self.bot = bot
self.dry_run = False
def _open(self):
if self.bot is not None:
dbusername = self.bot.conf.getDBUsername(self.bot.network)
password = self.bot.conf.getDBPass(self.bot.network)
dbname = self.bot.conf.getDBName(self.bot.network)
else:
dbusername = "pybot"
password = "1q2w3e4r"
dbname = "pybot"
try:
self.con = mdb.connect("localhost", dbusername, password, dbname)
except mdb.OperationalError as e:
self.dry_run = True
self.bot.logger(Logger.WARNING, e)
print(e)
return
self.cur = self.con.cursor()
def _close(self):
self.con = None
if not self.dry_run:
self.cur.close()
# should prevent mysql has gone away errors.. ideally
def _handle(self):
global cur
global age
now = datetime.now()
if now - self.age > timedelta(minutes=5):
self.cur.close()
self.con = mdb.connect("localhost", "pybot", "1q2w3e4r", "pybot")
self.cur = self.con.cursor()
def select(self, where, what):
try:
self._open()
self.cur.execute("""SELECT %s FROM %s""")
data = self.cur.fetchall()
self._close()
except BaseException:
self._close()
return None
return data
def replace(self, where, which, what):
try:
self._open()
self.cur.execute(
"""REPLACE INTO %s (%s) VALUES (%s)""", (where, which, what))
self._close()
except BaseException:
self._close()
return None
def e(self, sql):
try:
self._open()
self.cur.execute(sql)
if "INSERT" in sql or "REPLACE" in sql:
self.con.commit()
self._close()
elif "SELECT" in sql:
e = self.cur.fetchall()
self._close()
return e
except Exception as e:
print(e)
self.con.rollback()
self._close()
return None
def insert(self, where, which, what):
try:
self._open()
self.cur.execute(
"""INSERT INTO %s (%s) VALUES (%s)""", (where, which, what))
self._close()
except BaseException:
self._close()
return None
def updateSeen(self, who, statement, event):
self._open()
# print "executing REPLACE INTO seen (user_name, statement, event)
# VALUES ( " + str(who) + " " + str(statement) + " " + str(event) + ")"
self.cur.execute(
"REPLACE INTO seen (user_name, statement, event) VALUES (%s, %s, %s)",
(who,
statement,
event))
self._close()
def getSeen(self, who):
self._open()
if who != "":
self.cur.execute(
"SELECT user_name, date, statement, event FROM seen WHERE user_name = %s", who)
data = self.cur.fetchone()
return data
self._close()
else:
self._close()
return None
def insertImg(self, user, url, channel):
self._open()
if user == "" or user is None:
user = "nobody"
try:
self.cur.execute(
"""INSERT INTO img (user, url, channel) VALUES (%s, %s, %s)""",
(user,
url,
channel))
if not self.dry_run:
self.con.commit()
except BaseException:
self.bot.logger(Logger.WARNING, 'failed to insert ' + url)
print("failure")
print(("Unexpected error:", sys.exc_info()[0]))
if not self.dry_run:
self.con.rollback()
self._close()
def getImgs(self):
self._open()
try:
self.cur.execute("""SELECT * FROM img ORDER BY time DESC""")
data = self.cur.fetchall()
self._close()
except BaseException:
self._close()
return None
return data
def isAdmin(self, username):
self._open()
try:
self.cur.execute(
"""SELECT * FROM admins WHERE username = %s""",
[username])
data = self.cur.fetchall()
self._close()
except Exception as e:
print(e)
self._close()
return None
return data