Seen: convert to use sqlite database
This commit is contained in:
		
							parent
							
								
									67403971df
								
							
						
					
					
						commit
						e020cdb476
					
				| @ -16,7 +16,6 @@ You should have received a copy of the GNU General Public License | ||||
| along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||||
| """ | ||||
| 
 | ||||
| from ConfigParser import NoOptionError | ||||
| import re | ||||
| 
 | ||||
| from datetime import datetime | ||||
| @ -29,23 +28,60 @@ class Seen(Module): | ||||
| 
 | ||||
|     """Track when people say things in public channels, and report on it.""" | ||||
| 
 | ||||
|     def do(self, connection, event, nick, userhost, what, admin_unlocked): | ||||
|         # whatever it is, store it | ||||
|         if not self.config.has_section(self.__class__.__name__): | ||||
|             self.config.add_section(self.__class__.__name__) | ||||
|     def db_init(self): | ||||
|         """Create the table to store seen data.""" | ||||
| 
 | ||||
|         self.config.set(self.__class__.__name__, nick, userhost + '|:|' + datetime.utcnow().isoformat() + '|:|' + what) | ||||
|         version = self.db_module_registered(self.__class__.__name__) | ||||
|         if version == None: | ||||
|             db = self.get_db() | ||||
|             try: | ||||
|                 db.execute(''' | ||||
|                     CREATE TABLE seen_nicks ( | ||||
|                         nick TEXT NOT NULL PRIMARY KEY, | ||||
|                         host TEXT NOT NULL, | ||||
|                         time TEXT DEFAULT CURRENT_TIMESTAMP, | ||||
|                         what TEXT NOT NULL | ||||
|                     )''') | ||||
|                 sql = 'INSERT INTO drbotzo_modules VALUES (?,?)' | ||||
|                 db.execute(sql, (self.__class__.__name__, 1)) | ||||
|                 db.commit() | ||||
|                 version = 1 | ||||
|             except sqlite3.Error as e: | ||||
|                 db.rollback() | ||||
|                 print("sqlite error: " + str(e)) | ||||
|                 raise | ||||
| 
 | ||||
|     def do(self, connection, event, nick, userhost, what, admin_unlocked): | ||||
|         """Track pubmsg/privmsg events, and if asked, report on someone.""" | ||||
| 
 | ||||
|         # whatever it is, store it | ||||
|         try: | ||||
|             db = self.get_db() | ||||
|             cur = db.cursor() | ||||
|             statement = 'REPLACE INTO seen_nicks (nick, host, what) VALUES (?, ?, ?)' | ||||
|             cur.execute(statement, (nick, userhost, what)) | ||||
|             db.commit() | ||||
|         except sqlite3.Error as e: | ||||
|             db.rollback() | ||||
|             print("sqlite error: " + str(e)) | ||||
|             raise | ||||
| 
 | ||||
|         match = re.search('^!seen\s+(\S+)$', what) | ||||
|         if match: | ||||
|             query = match.group(1) | ||||
|             if query != 'debug': | ||||
|                 try: | ||||
|                     seendata = self.config.get(self.__class__.__name__, query).split('|:|') | ||||
|                     converted = datetime.strptime(seendata[1], "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=tzutc()) | ||||
|                     replystr = 'last saw ' + query + ' at ' + converted.astimezone(tzlocal()).strftime("%Y/%m/%d %H:%M:%S %Z") + ' saying \'' + seendata[2] + '\'' | ||||
|             nick = match.group(1) | ||||
|             try: | ||||
|                 db = self.get_db() | ||||
|                 query = 'SELECT * FROM seen_nicks WHERE nick = ?' | ||||
|                 cursor = db.execute(query, (nick,)) | ||||
|                 result = cursor.fetchone() | ||||
|                 if result: | ||||
|                     seentime = datetime.strptime(result['time'], '%Y-%m-%d %H:%M:%S').replace(tzinfo=tzutc()) | ||||
|                     replystr = 'last saw {0:s} at {1:s} saying \'{2:s}\''.format(result['nick'], seentime.astimezone(tzlocal()).strftime('%Y/%m/%d %H:%M:%S %Z'), result['what']) | ||||
|                     return self.reply(connection, event, replystr) | ||||
|                 except NoOptionError: pass | ||||
|             except sqlite3.Error as e: | ||||
|                 db.rollback() | ||||
|                 print("sqlite error: " + str(e)) | ||||
|                 raise | ||||
| 
 | ||||
| # vi:tabstop=4:expandtab:autoindent | ||||
| # kate: indent-mode python;indent-width 4;replace-tabs on; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user