def handle(self, *args, **options): if len(args) == 0: raise CommandError("Path to log file not specified") elif len(args) > 1: raise CommandError("Can only import one log file at a time") path = args[0] if not os.path.exists(path): raise CommandError("No irc log found at %r" % path) ignored_nicks = options.get("ignored_nicks", []) brain.client.flushdb() with open(path) as fp: start_at = int(options.get('start_at', 0)) lines = fp.readlines()[start_at:] for line in lines: sys.stdout.flush() if line.startswith("---"): continue if line[2] != ":": continue line = line[6:] # If this line doesn't start with < then there isn't a nick, so # there isnt a chat line. if not line.startswith("<"): continue nick, line = line[1:].split(">", 1) nick = nick.strip().lstrip("@") line = line.strip() # Try to find directed messages. Grr. # So treat: pubbot: hello boy # As: hello boy if ":" in line: l, r = line.split(":", 1) if " " not in l: continue # Is this nick blacklisted? (Best to ignore chatbot spam) if nick.lower() in ignored_nicks: continue # Does this line actually have sentences? if " " not in line: continue brain.store_string(line) self.stdout.write('I readed the log file')
def learn(sender, sentence, **kwargs): if kwargs.get('direct', False): return brain.store_string(sentence)