Can deferred a treatment on each line responded (to save time at first fetch)

This commit is contained in:
nemunaire 2014-09-18 08:22:59 +02:00
parent 4d187f61e3
commit dee6ec28fe
2 changed files with 11 additions and 5 deletions

View File

@ -63,7 +63,7 @@ def parse_wikitext(site, cnt, ssl=False):
return cnt return cnt
def irc_format(cnt): def irc_format(cnt):
cnt, _ = re.subn(r"(?P<title>==+)\s*(.*?)\s*(?P=title)\n+", "\x03\x16" + r"\2" + " :\x03\x16 ", cnt) cnt, _ = re.subn(r"(?P<title>==+)\s*(.*?)\s*(?P=title)\n*", "\x03\x16" + r"\2" + " :\x03\x16 ", cnt)
return cnt.replace("'''", "\x03\x02").replace("''", "\x03\x1f") return cnt.replace("'''", "\x03\x02").replace("''", "\x03\x1f")
def get_page(site, term, ssl=False): def get_page(site, term, ssl=False):
@ -85,7 +85,8 @@ def cmd_wikipedia(msg):
if len(msg.cmds) < 3: if len(msg.cmds) < 3:
raise IRCException("indicate a lang and a term to search") raise IRCException("indicate a lang and a term to search")
return Response(irc_format(parse_wikitext(get_page(msg.cmds[1] + ".wikipedia.org", " ".join(msg.cmds[2:])))), site = msg.cmds[1] + ".wikipedia.org"
# get_page(msg.cmds[1] + ".wikipedia.org", " ".join(msg.cmds[2:])),
# line_treat=lambda line: irc_format(parse_wikitext(site, line, ssl)), return Response(get_page(site, " ".join(msg.cmds[2:])),
line_treat=lambda line: irc_format(parse_wikitext(site, line)),
channel=msg.receivers) channel=msg.receivers)

View File

@ -25,9 +25,11 @@ logger = logging.getLogger("nemubot.response")
class Response: class Response:
def __init__(self, message=None, channel=None, nick=None, server=None, def __init__(self, message=None, channel=None, nick=None, server=None,
nomore="No more message", title=None, more="(suite) ", nomore="No more message", title=None, more="(suite) ",
count=None, ctcp=False, shown_first_count=-1): count=None, ctcp=False, shown_first_count=-1,
line_treat=None):
self.nomore = nomore self.nomore = nomore
self.more = more self.more = more
self.line_treat = line_treat
self.rawtitle = title self.rawtitle = title
self.server = server self.server = server
self.messages = list() self.messages = list()
@ -127,6 +129,9 @@ class Response:
if self.empty: if self.empty:
return self.treat_ctcp(self.nomore) return self.treat_ctcp(self.nomore)
if self.line_treat is not None and self.elt == 0:
self.messages[0] = self.line_treat(self.messages[0])
msg = "" msg = ""
if self.channel is not None and self.nick is not None: if self.channel is not None and self.nick is not None:
msg += self.nick + ": " msg += self.nick + ": "