db.py 3.6 KB

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