From 598f626e0ba8edeeac90093eefc66e1de109bd5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9munaire?= Date: Sat, 30 Jun 2012 17:51:50 +0200 Subject: [PATCH] New module: syno which find synonyms --- modules/ddg/WFASearch.py | 2 +- modules/syno.py | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 modules/syno.py diff --git a/modules/ddg/WFASearch.py b/modules/ddg/WFASearch.py index 190d475..d5d0884 100644 --- a/modules/ddg/WFASearch.py +++ b/modules/ddg/WFASearch.py @@ -59,7 +59,7 @@ def getPage(terms): print ("impossible de récupérer la page Wolfram|Alpha.") return (http.client.INTERNAL_SERVER_ERROR, None) except (TypeError, KeyError): - print ("You need an Wolfram|Alpha API key in order to use this module. Add it to the configuration file:\n\nRegister at http://products.wolframalpha.com/api/") + print ("You need a Wolfram|Alpha API key in order to use this module. Add it to the module configuration file:\n\nRegister at http://products.wolframalpha.com/api/") return (http.client.INTERNAL_SERVER_ERROR, None) res = conn.getresponse() diff --git a/modules/syno.py b/modules/syno.py new file mode 100644 index 0000000..9596e01 --- /dev/null +++ b/modules/syno.py @@ -0,0 +1,52 @@ +# coding=utf-8 + +import http.client +import re +import socket +from urllib.parse import quote + +nemubotversion = 3.0 + +import module_states_file as xmlparser + +def help_tiny (): + return "Find french synonyms" + +def help_full (): + return "!syno [ ...]: give a list of synonyms for each (maximum 5 each time)." + + +def parseanswer(msg): + if msg.cmd[0] == "syno" or msg.cmd[0] == "synonyme": + if 1 < len(msg.cmd) < 6: + for syno in msg.cmd[1:]: + (res, page) = getPage(syno) + if res == http.client.OK: + synos = list() + for line in page.decode().split("\n"): + if re.match("[ \t]*]*>.*[ \t]*.*", line) is not None: + for elt in re.finditer(" ([^&]*) ", line): + synos.append(elt.group(1)) + if len(synos) > 0: + msg.send_chn("Synonymes de %s : %s" % (syno, ', '.join(synos))) + else: + msg.send_chn("Aucun synonymes de %s n'a ete trouve") + else: + msg.send_chn("Une erreur s'est produite durant la recherche d'un synonyme de %s" % syno) + return True + return False + + +def getPage(terms): + conn = http.client.HTTPConnection("www.crisco.unicaen.fr") + try: + conn.request("GET", "/des/synonymes/%s" % quote(terms)) + except socket.gaierror: + print ("impossible de récupérer la page Wolfram|Alpha.") + return (http.client.INTERNAL_SERVER_ERROR, None) + + res = conn.getresponse() + data = res.read() + + conn.close() + return (res.status, data)