因此,我无法找到有关该主题的大量信息,但我正在寻找一个示例,以在将给定链接发布到 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/