1
|
|
|
import logger |
2
|
|
|
try: |
3
|
|
|
import MySQLdb as mdb |
4
|
|
|
except ImportError: |
5
|
|
|
import sys |
6
|
|
|
imported = False |
7
|
|
|
if "lite" not in sys.modules: |
8
|
|
|
print "Could not find MySQLdb and lite is not imported! Erroring out!" |
9
|
|
|
sys.exit(1) |
10
|
|
|
|
11
|
|
|
from datetime import datetime,timedelta |
12
|
|
|
|
13
|
|
|
class DB: |
14
|
|
|
""" |
15
|
|
|
Handles connecting to the database and reading and writing data. |
16
|
|
|
Currently supports only MySQL/mariadb, and that probably needs to change. |
17
|
|
|
""" |
18
|
|
|
age = datetime.now() |
19
|
|
|
|
20
|
|
|
def __init__(self, bot=None): |
21
|
|
|
self.bot = bot |
22
|
|
|
self.dry_run = False |
23
|
|
|
|
24
|
|
|
def _open(self): |
25
|
|
|
if self.bot is not None: |
26
|
|
|
dbusername = self.bot.conf.getDBUsername(self.bot.network) |
27
|
|
|
password = self.bot.conf.getDBPass(self.bot.network) |
28
|
|
|
dbname = self.bot.conf.getDBName(self.bot.network) |
29
|
|
|
else: |
30
|
|
|
dbusername = "pybot" |
31
|
|
|
password = "1q2w3e4r" |
32
|
|
|
dbname = "pybot" |
33
|
|
|
|
34
|
|
|
try: |
35
|
|
|
self.con = mdb.connect("localhost",dbusername,password,dbname) |
36
|
|
|
except mdb.OperationalError as e: |
37
|
|
|
self.dry_run = True |
38
|
|
|
self.bot.logger(logger.Logger.WARNING, e) |
39
|
|
|
return |
40
|
|
|
|
41
|
|
|
self.cur = self.con.cursor() |
42
|
|
|
|
43
|
|
|
def _close(self): |
44
|
|
|
self.con = None |
45
|
|
|
if not self.dry_run: |
46
|
|
|
self.cur.close() |
47
|
|
|
|
48
|
|
|
# should prevent mysql has gone away errors.. ideally |
49
|
|
|
def _handle(self): |
50
|
|
|
global cur |
51
|
|
|
global age |
52
|
|
|
now = datetime.now() |
53
|
|
|
if now - self.age > timedelta(minutes=5): |
54
|
|
|
self.cur.close() |
55
|
|
|
self.con = mdb.connect("localhost","pybot","1q2w3e4r","pybot") |
56
|
|
|
self.cur = self.con.cursor() |
57
|
|
|
|
58
|
|
|
def select(self, where, what): |
59
|
|
|
try: |
60
|
|
|
self._open() |
61
|
|
|
self.cur.execute("""SELECT %s FROM %s""") |
62
|
|
|
data = self.cur.fetchall() |
63
|
|
|
self._close() |
64
|
|
|
except: |
65
|
|
|
self._close() |
66
|
|
|
return None |
67
|
|
|
|
68
|
|
|
return data |
69
|
|
|
|
70
|
|
|
def replace(self, where, which, what): |
71
|
|
|
try: |
72
|
|
|
self._open() |
73
|
|
|
self.cur.execute("""REPLACE INTO %s (%s) VALUES (%s)""",(where, which, what)) |
74
|
|
|
self._close() |
75
|
|
|
except: |
76
|
|
|
self._close() |
77
|
|
|
return None |
78
|
|
|
|
79
|
|
View Code Duplication |
def e(self, sql): |
|
|
|
|
80
|
|
|
try: |
81
|
|
|
self._open() |
82
|
|
|
self.cur.execute(sql) |
83
|
|
|
if "INSERT" in sql or "REPLACE" in sql: |
84
|
|
|
self.con.commit() |
85
|
|
|
self._close() |
86
|
|
|
elif "SELECT" in sql: |
87
|
|
|
e = self.cur.fetchall() |
88
|
|
|
self._close() |
89
|
|
|
return e |
90
|
|
|
except Exception, e: |
91
|
|
|
print e |
92
|
|
|
self.con.rollback() |
93
|
|
|
self._close() |
94
|
|
|
return None |
95
|
|
|
|
96
|
|
|
def insert(self, where, which, what): |
97
|
|
|
try: |
98
|
|
|
self._open() |
99
|
|
|
self.cur.execute("""INSERT INTO %s (%s) VALUES (%s)""",(where, which, what)) |
100
|
|
|
self._close() |
101
|
|
|
except: |
102
|
|
|
self._close() |
103
|
|
|
return None |
104
|
|
|
|
105
|
|
|
def updateSeen(self,who,statement,event): |
106
|
|
|
self._open() |
107
|
|
|
#print "executing REPLACE INTO seen (user_name, statement, event) VALUES ( " + str(who) + " " + str(statement) + " " + str(event) + ")" |
108
|
|
|
self.cur.execute("REPLACE INTO seen (user_name, statement, event) VALUES (%s, %s, %s)", (who, statement, event)) |
109
|
|
|
self._close() |
110
|
|
|
|
111
|
|
|
def getSeen(self, who): |
112
|
|
|
self._open() |
113
|
|
|
if who != "": |
114
|
|
|
self.cur.execute("SELECT user_name, date, statement, event FROM seen WHERE user_name = %s", who) |
115
|
|
|
data = self.cur.fetchone() |
116
|
|
|
return data; |
117
|
|
|
self._close() |
118
|
|
|
else: |
119
|
|
|
self._close() |
120
|
|
|
return None |
121
|
|
|
|
122
|
|
|
def insertImg(self, user, url, channel): |
123
|
|
|
self._open() |
124
|
|
|
if user == "" or user == None: |
125
|
|
|
user = "nobody" |
126
|
|
|
try: |
127
|
|
|
self.cur.execute("""INSERT INTO img (user, url, channel) VALUES (%s, %s, %s)""", (user, url, channel)) |
128
|
|
|
if not self.dry_run: |
129
|
|
|
self.con.commit() |
130
|
|
|
except: |
131
|
|
|
self.bot.logger(logger.Logger.WARNING, 'failed to insert ' + url) |
132
|
|
|
print "failure" |
133
|
|
|
print "Unexpected error:", sys.exc_info()[0] |
134
|
|
|
if not self.dry_run: |
135
|
|
|
self.con.rollback() |
136
|
|
|
|
137
|
|
|
self._close() |
138
|
|
|
|
139
|
|
|
def getImgs(self): |
140
|
|
|
self._open() |
141
|
|
|
try: |
142
|
|
|
self.cur.execute("""SELECT * FROM img ORDER BY time DESC""") |
143
|
|
|
data = self.cur.fetchall() |
144
|
|
|
self._close() |
145
|
|
|
except: |
146
|
|
|
self._close() |
147
|
|
|
return None |
148
|
|
|
|
149
|
|
|
return data |
150
|
|
|
|
151
|
|
|
def isAdmin(self, username): |
152
|
|
|
self._open() |
153
|
|
|
try: |
154
|
|
|
self.cur.execute("""SELECT * FROM admins WHERE username = %s""",[username]) |
155
|
|
|
data = self.cur.fetchall() |
156
|
|
|
self._close() |
157
|
|
|
except Exception, e: |
158
|
|
|
print e |
159
|
|
|
self._close() |
160
|
|
|
return None |
161
|
|
|
|
162
|
|
|
return data |
163
|
|
|
|