regex search in FactFile
This commit is contained in:
parent
6e86096f2b
commit
cb70310616
1
TODO
1
TODO
|
@ -19,4 +19,3 @@ dr.botzo --- TODO
|
|||
* obligatory info command
|
||||
* similar to FactFile module, a generic Trigger module
|
||||
* do splitting in DrBotServerConnection.privmsg
|
||||
* regex search in FactFile
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
from ConfigParser import NoOptionError
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
|
||||
from irclib import irclib
|
||||
|
||||
|
@ -47,11 +48,20 @@ class FactFile(Module):
|
|||
if os.path.isfile(filename):
|
||||
# http://www.regexprn.com/2008/11/read-random-line-in-large-file-in.html
|
||||
with open(filename, 'r') as file:
|
||||
facts = file.readlines()
|
||||
to_print = facts[random.randint(1, len(facts))-1]
|
||||
facts = []
|
||||
if len(whats) == 1:
|
||||
facts = file.readlines()
|
||||
else:
|
||||
lines = file.readlines()
|
||||
for line in lines:
|
||||
# check if line matches provided regex
|
||||
if re.search(' '.join(whats[1:]), line, re.IGNORECASE) is not None:
|
||||
facts.append(line)
|
||||
if len(facts) > 0:
|
||||
to_print = facts[random.randint(1, len(facts))-1]
|
||||
|
||||
# return text
|
||||
return self.reply(connection, replypath, to_print.rstrip())
|
||||
# return text
|
||||
return self.reply(connection, replypath, to_print.rstrip())
|
||||
else:
|
||||
print('filename in config file for \'' + whats[0] + '\' is wrong')
|
||||
|
||||
|
|
Loading…
Reference in New Issue