From b14137e9cc0ed5d68bb65c2cc5ffa2b26e45ee4b Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Mon, 14 Apr 2014 10:19:56 +0200 Subject: [PATCH] =?utf8?q?!from=20:=20quote=20al=C3=A9atoire=20en=20faisan?= =?utf8?q?t=20une=20recherche=20sur=20l'auteur?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- parrot.py | 19 ++++++++++++------- quotes.py | 5 ++++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/parrot.py b/parrot.py index 1f5b70d..26a204a 100755 --- a/parrot.py +++ b/parrot.py @@ -446,18 +446,23 @@ class Parrot(ircbot.SingleServerIRCBot): serv.privmsg(canal, (u"Pas de quote de %s en mémoire." % author).encode("utf-8")) return serv.privmsg(canal, str(q)) - elif message.startswith(u"!author"): + elif message.startswith(u"!author") or message.startswith(u"!from"): words = message.split() cmd = words[0].lstrip("!") - regexp = cmd in ["author" + suffix for suffix in config.regex_suffixes] + regexp = any([cmd.endswith(suffix) for suffix in config.regex_suffixes]) search = u" ".join(words[1:]) authors = self.quotedb.search_authors(search, regexp) - if len(authors) > config.search_max_authors: - authors = authors[:config.search_max_authors+1] + ["+%s" % (len(authors) - config.search_max_authors)] - if authors: - serv.privmsg(canal, "%s: %s" % (auteur, (u", ".join(authors)).encode("utf-8"))) - else: + if not authors: serv.privmsg(canal, "%s: Pas d'auteur correspondant à la recherche." % (auteur,)) + return + if cmd.startswith("author"): + if len(authors) > config.search_max_authors: + authors = authors[:config.search_max_authors+1] + ["+%s" % (len(authors) - config.search_max_authors)] + serv.privmsg(canal, "%s: %s" % (auteur, (u", ".join(authors)).encode("utf-8"))) + elif cmd.startswith("from"): + quotes = sum([self.quotedb.quotesfrom(a) for a in authors], []) + q = random.choice(quotes) + serv.privmsg(canal, str(q)) elif message.startswith(u"!search"): words = message.split() cmd = words[0].lstrip("!") diff --git a/quotes.py b/quotes.py index bd05913..fae0df0 100644 --- a/quotes.py +++ b/quotes.py @@ -101,9 +101,12 @@ class QuoteDB(object): def random(self): """ Sort une quote aléatoire """ return random.choice(self.quotelist) + def quotesfrom(self, author): + """ Sort toutes les quotes de ``author`` """ + return [q for q in self.quotelist if q.author == author] def randomfrom(self, author): """ Sort une quote aléatoire de ``author`` """ - return random.choice([q for q in self.quotelist if q.author == author]) + return random.choice(self.quotesfrom(author)) def search(self, inquote=None, author=None, regexp=False): """Fait une recherche dans les quotes.""" -- 2.39.2