added --context to seendb

This commit is contained in:
iou1name 2018-05-29 13:58:03 -04:00
parent feb33e2345
commit 447e0be03c

View File

@ -2,6 +2,7 @@
""" """
When was this user last seen. When was this user last seen.
""" """
import os
import time import time
import argparse import argparse
import threading import threading
@ -11,7 +12,7 @@ from sqlite3 import OperationalError
from requests.structures import CaseInsensitiveDict from requests.structures import CaseInsensitiveDict
from tools.time import relativeTime from tools.time import relativeTime
from module import commands, example, hook, require_chanmsg from module import commands, example, hook, require_chanmsg, rate
def load_database(bot): def load_database(bot):
@ -49,7 +50,7 @@ def setup(bot):
bot.memory["seen"] = load_database(bot) bot.memory["seen"] = load_database(bot)
bot.memory["seen_last_dump"] = time.time() bot.memory["seen_last_dump"] = time.time()
@rate(1)
@commands('seen') @commands('seen')
@example(".seen Nigger -l", "Last heard from Nigger at [1997-03-12 16:30:00] "\ @example(".seen Nigger -l", "Last heard from Nigger at [1997-03-12 16:30:00] "\
+"with \"Just going to the store for some smokes babe I'll be right back\"") +"with \"Just going to the store for some smokes babe I'll be right back\"")
@ -60,6 +61,7 @@ def seen(bot, trigger):
--last [-l] reports when the user was last seen. This is the default. --last [-l] reports when the user was last seen. This is the default.
--first [-f] reports when the user was first seen. --first [-f] reports when the user was first seen.
--message [-m] includes the first/last message the user sent. --message [-m] includes the first/last message the user sent.
--context [-c] includes irc logs before and after their last message as context. Implies --message.
""" """
if not trigger.group(2): if not trigger.group(2):
return bot.reply("Seen who?") return bot.reply("Seen who?")
@ -69,6 +71,7 @@ def seen(bot, trigger):
parser.add_argument("-l", "--last", action="store_true", default=True) parser.add_argument("-l", "--last", action="store_true", default=True)
parser.add_argument("-f", "--first", action="store_true") parser.add_argument("-f", "--first", action="store_true")
parser.add_argument("-m", "--message", action="store_true") parser.add_argument("-m", "--message", action="store_true")
parser.add_argument("-c", "--context", action="store_true")
args = parser.parse_args(trigger.group[3:]) args = parser.parse_args(trigger.group[3:])
if args.nick == bot.nick: if args.nick == bot.nick:
@ -100,6 +103,24 @@ def seen(bot, trigger):
bot.msg(msg) bot.msg(msg)
if args.context:
num_con_lines = 2
fname = os.path.join(bot.log_path, bot.hostname,channel,timestamp[1:11])
fname += ".log"
if not os.path.isfile(fname):
return bot.msg("Context not available.")
with open(fname, "r") as file:
data = file.read().splitlines()
matches = [line for line in data if line.startswith(timestamp)]
if not matches:
return bot.msg("Context not available")
index = data.index(matches[0]) # a bit lazy, but w/e
start = max([index - num_con_lines, 0])
end = min([index + num_con_lines+1, len(data) - 1])
bot.msg("Context:")
bot.msg("\n".join(data[start:end]))
del data
def dump_seen_db(bot): def dump_seen_db(bot):
""" """