Python IRC Bot 链接解析器?

标签 python hyperlink bots irc

因此,我无法找到有关该主题的大量信息,但我正在寻找一个示例,以在将给定链接发布到 IRC channel 时解析该链接。有什么提示/示例吗?

最佳答案

我写了一个简单的示例,它完全符合您的要求。

请随意使用此作为满足您需求的基础!

#!/usr/bin/env python

from re import findall

from circuits import Component
from circuits.ne t.events import connect
from circuits.net.sockets import TCPClient
from circuits.protocols.irc import ERR_NICKNAMEINUSE
from circuits.protocols.irc import RPL_ENDOFMOTD, ERR_NOMOTD
from circuits.protocols.irc import IRC, PRIVMSG, USER, NICK, JOIN

from requests import get
from lxml.html import fromstring


class Bot(Component):

    def init(self, host, port=6667):
        self.host = host
        self.port = port

        TCPClient().register(self)
        IRC().register(self)

    def ready(self, component):
        self.fire(connect(self.host, self.port))

    def connected(self, host, port):
        self.fire(USER("circuits", host, host, "Test circuits IRC Bot"))
        self.fire(NICK("circuits"))

    def numeric(self, source, target, numeric, args, message):
        if numeric == ERR_NICKNAMEINUSE:
            self.fire(NICK("%s_" % args))
        if numeric in (RPL_ENDOFMOTD, ERR_NOMOTD):
            self.fire(JOIN("#circuits"))

    def message(self, source, target, message):
        if target[0] == "#":
            urls = findall("http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+", message)  # noqa
            if urls:
                url = urls[0]
                response = get(url)
                if response.status_code == 200:
                    doc = fromstring(response.text)
                    title = doc.cssselect("title")
                    if title:
                        title = title[0].text.strip()
                        self.fire(
                            PRIVMSG(
                                target,
                                "URL: {0:s} Title: {1:s}".format(
                                    url,
                                    title
                                )
                            )
                        )
        else:
            self.fire(PRIVMSG(source[0], message))


bot = Bot("irc.freenode.net")

bot.run()

您将需要:

您可以通过以下方式安装它们:

pip install circuits cssselect lxml requests

免责声明:我是电路开发人员

更新:经测试可按预期工作。

关于Python IRC Bot 链接解析器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24817517/

相关文章:

python - 使用 ElementTree getpath() 动态获取 Xpath

python flask,使用另一个类将 session 保持在范围内

JSF 链接查看页面

Java jsoup html解析机器人index/bot检测、noindex

javascript - 如何创建一个发送消息的函数

python - 基于 stomp.py 的持久客户端填充 ActiveMQ 中的订阅者列表

python - 在 python 中读取空终止字符串

hyperlink - 从youtube页面上的侧边栏中提取youtube链接

c# - 使用名称单独超链接创建 Gridview

bots - 减少 Bot Connector 的延迟