diff --git a/bot/urlinfo.py b/bot/urlinfo.py new file mode 100644 index 0000000..bf540d0 --- /dev/null +++ b/bot/urlinfo.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +import re +import irc3 +import requests +import io +from lxml import etree + +from . import Plugin + +class URLInfo(Plugin): + BLACKLIST = [ + "^https?:\/\/www\.youtube\.com", + "^https?:\/\/youtu\.be", + "^https?:\/\/w0bm\.com", + "^https?:\/\/f0ck\.me" + ] + + @irc3.event(r'(?i)^:\S+ PRIVMSG (?P\S+) :.*(?Phttps?:\/\/\S+\.\S+).*') + def url_parser(self, target: str, url: str): + for regex in self.BLACKLIST: + if re.match(regex, url): + return + try: + response = requests.get(url, timeout=10) + except requests.exceptions.ConnectionError: + return + tree = etree.parse(io.StringIO(response.text), etree.HTMLParser()).getroot() + title = tree.xpath("/html/head/title") + if len(title) > 0: + self.bot.privmsg(target, '\x02[URLInfo]\x02 ' + title[0].text.strip()) +