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