make urlhistory.py record irc servers as well

This commit is contained in:
Ryan Hitchman 2010-01-18 20:16:40 -07:00
parent 977c62f7ef
commit 8271b23cb6
1 changed files with 69 additions and 110 deletions

View File

@ -1,110 +1,69 @@
import os import os
import time import time
from datetime import datetime
import sqlite3 import sqlite3
import pickle import pickle
from datetime import timedelta
import re import re
from util import hook, timesince from util import hook, urlnorm
from util import urlnorm
#from util import texttime
url_re = re.compile(r'([a-zA-Z]+://|www\.)[^ ]*') url_re = re.compile(r'([a-zA-Z]+://|www\.)[^ ]*')
dbname = "skybot.db" dbname = "skybot.db"
expiration_period = timedelta(days=1) expiration_period = 60 * 60 * 24 # 1 day
ignored_urls = [ urlnorm.normalize("http://google.com") ]
#TODO: Generate expiration_period_text from expiration_period
expiration_period_text = "24 hours" expiration_period_text = "24 hours"
def adapt_datetime(ts): ignored_urls = [urlnorm.normalize("http://google.com")]
return time.mktime(ts.timetuple())
sqlite3.register_adapter(datetime, adapt_datetime)
def insert_history(conn, url, channel, nick):
cursor = conn.cursor()
now = datetime.now()
cursor.execute("insert into urlhistory(url, nick, chan, time) values(?,?,?,?)", (url, nick, channel, now))
conn.commit()
def select_history_for_url_and_channel(conn, url, channel):
cursor = conn.cursor()
results = cursor.execute("select nick, time from urlhistory where url=? and chan=?", (url, channel)).fetchall()
j = 0
now = datetime.now()
nicks = []
for i in xrange(len(results)):
reltime = datetime.fromtimestamp(results[j][1])
if (now - reltime) > expiration_period:
conn.execute("delete from urlhistory where url=? and chan=? and nick=? and time=?", (url, channel, results[j][0], results[j][1]))
results.remove(results[j])
else:
nicks.append(results[j][0])
j += 1
return nicks
def get_nicklist(nicks):
nicks = list(set(nicks))
nicks.sort()
l = len(nicks)
if l == 0:
return ""
elif l == 1:
return nicks[0]
elif l == 2:
return nicks[0] + " and " + nicks[1]
else:
result = ""
for i in xrange(l-1):
result += nicks[i] + ", "
result += "and " + nicks[-1]
return result
def dbconnect(db): def dbconnect(db):
"check to see that our db has the the seen table and return a connection." "check to see that our db has the the seen table and return a connection."
conn = sqlite3.connect(db) conn = sqlite3.connect(db)
conn.execute("create table if not exists urlhistory"
results = conn.execute("select count(*) from sqlite_master where name=?", "(server, chan, url, nick, time)")
("urlhistory",)).fetchone()
if(results[0] == 0):
conn.execute("create table if not exists urlhistory(url text not null, nick text not null, chan text not null, time datetime not null, primary key(url, nick, chan, time));")
conn.commit() conn.commit()
return conn return conn
def normalize_url(url): def insert_history(conn, server, chan, url, nick):
return urlnorm.normalize(url) now = time.time()
conn.execute("insert into urlhistory(server, chan, url, nick, time) "
"values(?,?,?,?,?)", (server, chan, url, nick, time.time()))
conn.commit()
def get_once_twice(count): def get_history(conn, server, chan, url):
if count == 1: conn.execute("delete from urlhistory where time < ?",
return "once" (time.time() - expiration_period,))
elif count == 2: nicks = conn.execute("select nick from urlhistory where server=? "
return "twice" "and chan=? and url=?", (server, chan, url)).fetchall()
return [x[0] for x in nicks]
def get_nicklist(nicks):
nicks = sorted(set(nicks))
if len(nicks) <= 2:
return ' and '.join(nicks)
else: else:
return str(count) + " times" return ', and '.join((', '.join(nicks[:-1]), nicks[-1]))
@hook.command(hook=r'(.*)', prefix=False, ignorebots=True) def ordinal(count):
return ["once", "twice", "%d times" % count][min(count, 3) - 1]
@hook.command(hook=r'(.*)', prefix=False)
def urlinput(bot, input): def urlinput(bot, input):
dbpath = os.path.join(bot.persist_dir, dbname) dbpath = os.path.join(bot.persist_dir, dbname)
m = url_re.search(input.msg) m = url_re.search(input.msg.encode('utf8'))
if m: if not m:
# URL detected return
url = normalize_url(m.group(0))
if url not in ignored_urls:
conn = dbconnect(dbpath)
dupes = select_history_for_url_and_channel(conn, url, input.chan)
num_dupes = len(dupes)
if num_dupes > 0 and input.nick not in dupes:
nicks = get_nicklist(dupes)
reply = "That link has been posted " + get_once_twice(num_dupes)
reply += " in the past " + expiration_period_text + " by " + nicks
input.reply(reply)
insert_history(conn, url, input.chan, input.nick)
conn.close()
# URL detected
conn = dbconnect(dbpath)
try:
url = urlnorm.normalize(m.group(0))
if url not in ignored_urls:
dupes = get_history(conn, input.server, input.chan, url)
insert_history(conn, input.server, input.chan, url, input.nick)
if dupes and input.nick not in dupes:
input.reply("That link has been posted " + ordinal(len(dupes))
+ " in the past " + expiration_period_text + " by " +
get_nicklist(dupes))
finally:
conn.commit()
conn.close()