nemubot/modules/syno.py

62 lines
1.9 KiB
Python
Raw Normal View History

2012-06-30 15:51:50 +00:00
# coding=utf-8
import re
2013-03-20 14:08:24 +00:00
import traceback
import sys
2012-06-30 15:51:50 +00:00
from urllib.parse import quote
2012-11-04 15:26:20 +00:00
from tools import web
2012-11-04 14:32:39 +00:00
nemubotversion = 3.3
2012-06-30 15:51:50 +00:00
def help_tiny ():
return "Find french synonyms"
def help_full ():
2012-11-04 14:32:39 +00:00
return "!syno <word>: give a list of synonyms for <word>."
2012-06-30 15:51:50 +00:00
def load(context):
from hooks import Hook
add_hook("cmd_hook", Hook(cmd_syno, "syno"))
add_hook("cmd_hook", Hook(cmd_syno, "synonyme"))
2012-06-30 15:51:50 +00:00
def cmd_syno(msg):
2012-11-04 14:32:39 +00:00
if 1 < len(msg.cmds) < 6:
for word in msg.cmds[1:]:
2013-03-20 14:08:24 +00:00
try:
synos = get_synos(word)
except:
synos = None
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value,
exc_traceback)
if synos is None:
return Response(msg.sender,
"Une erreur s'est produite durant la recherche"
" d'un synonyme de %s" % word, msg.channel)
elif len(synos) > 0:
2012-11-04 14:32:39 +00:00
return Response(msg.sender, synos, msg.channel,
title="Synonymes de %s" % word)
else:
return Response(msg.sender,
"Aucun synonymes de %s n'a été trouvé" % word,
msg.channel)
return False
def get_synos(word):
2013-03-20 14:08:24 +00:00
url = "http://www.crisco.unicaen.fr/des/synonymes/" + quote(word.encode("ISO-8859-1"))
print_debug (url)
page = web.getURLContent(url)
2012-11-04 15:26:20 +00:00
if page is not None:
synos = list()
for line in page.decode().split("\n"):
2012-06-30 15:51:50 +00:00
if re.match("[ \t]*<tr[^>]*>.*</tr>[ \t]*</table>.*", line) is not None:
for elt in re.finditer(">&[^;]+;([^&]*)&[^;]+;<", line):
synos.append(elt.group(1))
return synos
else:
return None