2014-01-07 10:17:05 -08:00
|
|
|
#!/usr/bin/env python
|
2014-10-02 18:14:42 -07:00
|
|
|
"""
|
|
|
|
.. module:: Seen
|
2015-11-01 18:03:11 -08:00
|
|
|
:synopsis: Provides !seen <username>
|
2014-10-02 18:14:42 -07:00
|
|
|
|
|
|
|
.. moduleauthor:: Dave Pedu <dave@davepedu.com>
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2017-11-22 22:09:25 -08:00
|
|
|
from pyircbot.modules.ModInfo import info
|
|
|
|
from pyircbot.modulebase import ModuleBase, command, hook
|
|
|
|
from contextlib import closing
|
2014-01-07 10:17:05 -08:00
|
|
|
import sqlite3
|
|
|
|
import time
|
|
|
|
|
2017-01-01 14:59:01 -08:00
|
|
|
|
2014-01-07 10:17:05 -08:00
|
|
|
class Seen(ModuleBase):
|
2015-11-01 18:03:11 -08:00
|
|
|
def __init__(self, bot, moduleName):
|
|
|
|
ModuleBase.__init__(self, bot, moduleName)
|
|
|
|
# if the database doesnt exist, it will be created
|
|
|
|
sql = self.getSql()
|
2017-01-01 14:59:01 -08:00
|
|
|
c = sql.cursor()
|
2015-11-01 18:03:11 -08:00
|
|
|
# check if our table exists
|
|
|
|
c.execute("SELECT * FROM SQLITE_MASTER WHERE `type`='table' AND `name`='seen'")
|
2017-01-01 14:59:01 -08:00
|
|
|
if len(c.fetchall()) == 0:
|
2015-11-01 18:03:11 -08:00
|
|
|
self.log.info("Seen: Creating database")
|
|
|
|
# if no, create it.
|
2017-01-01 14:59:01 -08:00
|
|
|
c.execute("CREATE TABLE `seen` (`nick` VARCHAR(32), `date` INTEGER, PRIMARY KEY(`nick`))")
|
2015-11-01 18:03:11 -08:00
|
|
|
self.x = "asdf"
|
2017-01-01 14:59:01 -08:00
|
|
|
|
2017-11-22 22:09:25 -08:00
|
|
|
@hook("PRIVMSG")
|
|
|
|
def recordSeen(self, message, command):
|
2015-11-01 18:03:11 -08:00
|
|
|
# using a message to update last seen, also, the .seen query
|
2017-01-01 14:59:01 -08:00
|
|
|
datest = str(time.time() + (int(self.config["add_hours"]) * 60 * 60))
|
2017-11-22 22:09:25 -08:00
|
|
|
sql = self.getSql()
|
|
|
|
with closing(sql.cursor()) as c:
|
|
|
|
# update or add the user's row
|
|
|
|
c.execute("REPLACE INTO `seen` (`nick`, `date`) VALUES (?, ?)", (message.prefix.nick.lower(), datest))
|
|
|
|
# self.log.info("Seen: %s on %s" % (nick.lower(), datest))
|
|
|
|
sql.commit()
|
|
|
|
|
|
|
|
@info("seen <nick> print last time user was seen", cmds=["seen"])
|
|
|
|
@command("seen", require_args=True)
|
|
|
|
def lastSeen(self, message, command):
|
|
|
|
sql = self.getSql()
|
|
|
|
searchnic = command.args[0].lower()
|
|
|
|
with closing(sql.cursor()) as c:
|
2015-11-01 18:03:11 -08:00
|
|
|
# query the DB for the user
|
2017-11-22 22:09:25 -08:00
|
|
|
c.execute("SELECT * FROM `seen` WHERE `nick`= ? ", [searchnic])
|
|
|
|
rows = c.fetchall()
|
|
|
|
if len(rows) == 1:
|
|
|
|
self.bot.act_PRIVMSG(message.args[0], "I last saw %s on %s (%s)." %
|
|
|
|
(command.args[0], time.strftime("%m/%d/%y at %I:%M %p",
|
|
|
|
time.localtime(rows[0]['date'])), self.config["timezone"]))
|
|
|
|
else:
|
|
|
|
self.bot.act_PRIVMSG(message.args[0], "Sorry, I haven't seen %s!" % command.args[0])
|
2017-01-01 14:59:01 -08:00
|
|
|
|
2015-11-01 18:03:11 -08:00
|
|
|
def getSql(self):
|
|
|
|
# return a SQL reference to the database
|
|
|
|
path = self.getFilePath('database.sql3')
|
2017-01-01 14:59:01 -08:00
|
|
|
sql = sqlite3.connect(path)
|
2015-11-01 18:03:11 -08:00
|
|
|
sql.row_factory = self.dict_factory
|
|
|
|
return sql
|
2017-01-01 14:59:01 -08:00
|
|
|
|
2015-11-01 18:03:11 -08:00
|
|
|
def dict_factory(self, cursor, row):
|
|
|
|
# because Lists suck for database results
|
|
|
|
d = {}
|
|
|
|
for idx, col in enumerate(cursor.description):
|
|
|
|
d[col[0]] = row[idx]
|
|
|
|
return d
|