From ec9481e65bea8bbd9862531d48c687109f00aa66 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Mon, 7 Sep 2015 19:07:43 +0200 Subject: [PATCH] [networking] Add !title command to display the title of a page --- modules/networking/__init__.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/modules/networking/__init__.py b/modules/networking/__init__.py index 670c423..188dbb5 100644 --- a/modules/networking/__init__.py +++ b/modules/networking/__init__.py @@ -3,6 +3,7 @@ """Various network tools (w3m, w3c validator, curl, traceurl, ...)""" import logging +import re from nemubot.exception import IRCException from nemubot.hooks import hook @@ -37,6 +38,20 @@ def help_full(): return "!traceurl /url/: Follow redirections from /url/." +@hook("cmd_hook", "title") +def cmd_title(msg): + if not len(msg.args): + raise IRCException("Indicate the URL to visit.") + + url = " ".join(msg.args) + res = re.search("(.*?)", page.fetch(" ".join(msg.args)), re.DOTALL) + + if res is None: + raise IRCException("The page %s has no title" % url) + else: + return Response("%s: %s" % (url, res.group(1).replace("\n", " ")), channel=msg.channel) + + @hook("cmd_hook", "curly") def cmd_curly(msg): if not len(msg.args):