2010-02-06 02:54:28 +00:00
|
|
|
import math
|
2010-01-19 03:16:40 +00:00
|
|
|
import re
|
2010-02-06 02:54:28 +00:00
|
|
|
import time
|
2010-01-19 03:16:40 +00:00
|
|
|
|
2010-02-06 02:54:28 +00:00
|
|
|
from util import hook, urlnorm, timesince
|
2010-01-19 03:16:40 +00:00
|
|
|
|
|
|
|
url_re = re.compile(r'([a-zA-Z]+://|www\.)[^ ]*')
|
|
|
|
|
|
|
|
expiration_period = 60 * 60 * 24 # 1 day
|
|
|
|
|
|
|
|
ignored_urls = [urlnorm.normalize("http://google.com")]
|
|
|
|
|
2010-02-01 07:29:50 +00:00
|
|
|
def db_connect(bot, server):
|
2010-02-02 04:42:34 +00:00
|
|
|
"check to see that our db has the the seen table and return a dbection."
|
|
|
|
db = bot.get_db_connection(server)
|
|
|
|
db.execute("create table if not exists urlhistory"
|
2010-02-01 07:29:50 +00:00
|
|
|
"(chan, url, nick, time)")
|
2010-02-02 04:42:34 +00:00
|
|
|
db.commit()
|
|
|
|
return db
|
2010-01-19 03:16:40 +00:00
|
|
|
|
2010-02-02 04:42:34 +00:00
|
|
|
def insert_history(db, chan, url, nick):
|
2010-01-19 03:16:40 +00:00
|
|
|
now = time.time()
|
2010-02-02 04:42:34 +00:00
|
|
|
db.execute("insert into urlhistory(chan, url, nick, time) "
|
2010-02-01 07:29:50 +00:00
|
|
|
"values(?,?,?,?)", (chan, url, nick, time.time()))
|
2010-02-02 04:42:34 +00:00
|
|
|
db.commit()
|
2010-01-19 03:16:40 +00:00
|
|
|
|
2010-02-02 04:42:34 +00:00
|
|
|
def get_history(db, chan, url):
|
|
|
|
db.execute("delete from urlhistory where time < ?",
|
2010-01-19 03:16:40 +00:00
|
|
|
(time.time() - expiration_period,))
|
2010-02-06 02:54:28 +00:00
|
|
|
return db.execute("select nick, time from urlhistory where "
|
|
|
|
"chan=? and url=? order by time desc", (chan, url)).fetchall()
|
|
|
|
|
|
|
|
def nicklist(nicks):
|
|
|
|
nicks = sorted(dict(nicks), key=unicode.lower)
|
2010-01-19 03:16:40 +00:00
|
|
|
if len(nicks) <= 2:
|
|
|
|
return ' and '.join(nicks)
|
|
|
|
else:
|
|
|
|
return ', and '.join((', '.join(nicks[:-1]), nicks[-1]))
|
|
|
|
|
2010-02-06 02:54:28 +00:00
|
|
|
def format_reply(history):
|
|
|
|
if not history:
|
|
|
|
return
|
|
|
|
|
|
|
|
last_nick, recent_time = history[0]
|
|
|
|
last_time = timesince.timesince(recent_time)
|
|
|
|
|
|
|
|
if len(history) == 1:
|
|
|
|
return "%s linked that %s ago." % (last_nick, last_time)
|
|
|
|
|
|
|
|
hour_span = math.ceil((time.time() - history[-1][1]) / 3600)
|
|
|
|
hour_span = '%.0f hours' % hour_span if hour_span > 1 else 'hour'
|
|
|
|
|
|
|
|
hlen = len(history)
|
|
|
|
ordinal = ["once", "twice", "%d times" % hlen][min(hlen, 3) - 1]
|
|
|
|
|
|
|
|
if len(dict(history)) == 1:
|
|
|
|
last = "last linked %s ago" % last_time
|
|
|
|
else:
|
|
|
|
last = "last linked by %s %s ago" % (last_nick, last_time)
|
|
|
|
|
|
|
|
return "that url has been posted %s in the past %s by %s (%s)." % (ordinal,
|
|
|
|
hour_span, nicklist(history), last)
|
2010-01-19 03:16:40 +00:00
|
|
|
|
|
|
|
@hook.command(hook=r'(.*)', prefix=False)
|
2010-02-02 04:42:34 +00:00
|
|
|
def urlinput(inp, nick='', chan='', server='', reply=None, bot=None):
|
|
|
|
m = url_re.search(inp.encode('utf8'))
|
2010-01-19 03:16:40 +00:00
|
|
|
if not m:
|
|
|
|
return
|
|
|
|
|
|
|
|
# URL detected
|
2010-02-02 04:42:34 +00:00
|
|
|
db = db_connect(bot, server)
|
2010-02-06 02:54:28 +00:00
|
|
|
url = urlnorm.normalize(m.group(0))
|
|
|
|
if url not in ignored_urls:
|
|
|
|
history = get_history(db, chan, url)
|
|
|
|
insert_history(db, chan, url, nick)
|
2010-02-08 00:58:06 +00:00
|
|
|
if nick not in dict(history):
|
2010-02-06 02:54:28 +00:00
|
|
|
return format_reply(history)
|