datetime woes

This commit is contained in:
Ryan Hitchman 2009-07-08 11:57:02 -06:00
parent a0131cfaa8
commit fd8ba70c5e
3 changed files with 184 additions and 1 deletions

View File

@ -2,7 +2,7 @@
import os
import time
import datetime
from datetime import datetime
import sqlite3
from util import hook, timesince

76
plugins/seen.py.orig Normal file
View File

@ -0,0 +1,76 @@
" seen.py: written by sklnd in about two beers July 2009"
import os
import time
import datetime
import sqlite3
from util import hook, timesince
dbname = "skybot.db"
def adapt_datetime(ts):
return time.mktime(ts.timetuple())
sqlite3.register_adapter(datetime.datetime, adapt_datetime)
@hook.command(hook=r'(.*)', prefix=False, ignorebots=False)
def seeninput(bot, input):
dbpath = os.path.join(bot.persist_dir, dbname)
conn = dbconnect(dbpath)
cursor = conn.cursor()
cursor.execute("insert or replace into seen(name, date, quote, chan)"
"values(?,?,?,?)", (input.nick, datetime.now(),
input.msg, input.chan))
conn.commit()
conn.close()
@hook.command
def seen(bot, input):
".seen <nick> - Tell when a nickname was last in active in irc"
if len(input.msg) < 6:
return seen.__doc__
query = input.msg[6:].strip()
if query == input.nick:
return "Have you looked in a mirror lately?"
dbpath = os.path.join(bot.persist_dir, dbname)
conn = dbconnect(dbpath)
cursor = conn.cursor()
command = "select date, quote from seen where name = ? and chan = ?"
cursor.execute(command, (query, input.chan))
results = cursor.fetchone()
conn.close()
if(results != None):
reltime = timesince.timesince(datetime.fromtimestamp(results[0]))
return '%s was last seen %s ago saying: <%s> %s' % \
(query, reltime, results[1])
else:
return "I've never seen %s" % query
def dbconnect(db):
"check to see that our db has the the seen table and return a connection."
conn = sqlite3.connect(db)
results = conn.execute("select count(*) from sqlite_master where name=?",
("seen", )).fetchone()
if(results[0] == 0):
conn.execute("create table if not exists "
"seen(name varchar(30) not null, date datetime not null, "
"quote varchar(250) not null, chan varchar(32) not null, "
"primary key(name, chan));")
conn.commit()
return conn

107
plugins/tell.py.orig Normal file
View File

@ -0,0 +1,107 @@
" tell.py: written by sklnd in July 2009"
import os
import time
import datetime
import sqlite3
from util import hook, timesince
dbname = "skybot.db"
def adapt_datetime(ts):
return time.mktime(ts.timetuple())
sqlite3.register_adapter(datetime.datetime, adapt_datetime)
@hook.command(hook=r'(.*)', prefix=False, ignorebots=True)
def tellinput(bot, input):
dbpath = os.path.join(bot.persist_dir, dbname)
conn = dbconnect(dbpath)
cursor = conn.cursor()
command = "select count(name) from tell where name = ? and chan = ?"
results = cursor.execute(command, (input.nick, input.chan)).fetchone()
if results[0] > 0:
command = "select id, user_from, quote, date from tell " \
"where name = ? and chan = ?"
tells = cursor.execute(command, (input.nick, input.chan)).fetchall()
for tell in tells:
reltime = timesince.timesince(datetime.fromtimestamp(tell[3]))
bot.reply('%(teller)s said %(reltime)s ago: %(quote)s' %
{'teller': tell[1], 'quote': tell[2], 'reltime': reltime})
command = "delete from tell where id = ?"
cursor.execute(command, (tell[0], ))
conn.commit()
conn.close()
@hook.command
def tell(bot, input):
".tell <nick> <message> - Relay <message> to <nick> the next time he talks"
if len(input.msg) < 6:
return tell.__doc__
query = input.msg[6:].strip().partition(" ")
if query[0] == input.nick:
return "No."
if query[2] != "":
dbpath = os.path.join(bot.persist_dir, dbname)
conn = dbconnect(dbpath)
command = "select count(*) from tell_probation where name=? and chan=?"
if conn.execute(command, (input.nick, input.chan)).fetchone()[0] > 0:
return "No."
command = "select count(*) from tell where name=? and user_from=?"
if conn.execute(command, (query[0], input.nick)).fetchone()[0] >= 3:
return "You've told that person too many things."
cursor = conn.cursor()
command = "insert into tell(name, user_from, quote, chan, date) " \
"values(?,?,?,?,?)"
cursor.execute(command, (query[0], input.nick, query[2], input.chan,
datetime.now()))
conn.commit()
conn.close()
return "I'll pass that along."
else:
return tell.__doc__
def dbconnect(db):
"check to see that our db has the the seen table and return a connection."
conn = sqlite3.connect(db)
results = conn.execute("select count(*) from sqlite_master where name=?",
("tell", )).fetchone()
if results[0] == 0:
conn.execute("create table if not exists tell(id integer primary key "
"autoincrement, name varchar(30) not null, user_from "
"varchar(30) not null, quote varchar(250) not null, "
"chan varchar(32) not null, date datetime not null);")
conn.commit()
results = conn.execute("select count(*) from sqlite_master where name=?",
("tell_probation", )).fetchone()
if results[0] == 0:
conn.execute("create table if not exists "+ \
"tell_probation(name varchar(30), chan varchar(32),"
"primary key(name, chan));")
conn.commit()
return conn