2018-03-16 03:13:43 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
URL parsing.
|
|
|
|
"""
|
|
|
|
import re
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
from html.parser import HTMLParser
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from module import hook
|
|
|
|
|
|
|
|
HEADERS = {"User-Agent": "bix nood gimme the title", "Range": "bytes=0-4096"}
|
|
|
|
|
|
|
|
@hook(True)
|
|
|
|
def title_auto(bot, trigger):
|
|
|
|
"""
|
|
|
|
Automatically show titles for URLs. For shortened URLs/redirects, find
|
|
|
|
where the URL redirects to and show the title for that.
|
|
|
|
"""
|
|
|
|
if "http" not in trigger.group(0):
|
|
|
|
return
|
|
|
|
url_finder = re.compile(r"((?:http|https)(?::\/\/\S+))", re.IGNORECASE)
|
|
|
|
|
|
|
|
urls = re.findall(url_finder, trigger.group(0))
|
|
|
|
if len(urls) == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
for url in urls:
|
|
|
|
broken = False
|
|
|
|
for key in bot.url_callbacks:
|
|
|
|
if key in url:
|
|
|
|
bot.url_callbacks[key](bot, url)
|
|
|
|
broken = True
|
|
|
|
if broken:
|
|
|
|
continue
|
|
|
|
try:
|
2018-06-11 12:49:36 -04:00
|
|
|
res = requests.get(url, headers=HEADERS, verify=True, timeout=10)
|
2018-03-16 03:13:43 -04:00
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
res.raise_for_status()
|
|
|
|
except:
|
|
|
|
continue
|
2018-05-27 14:16:50 -04:00
|
|
|
if not res.headers.get("Content-Type"):
|
|
|
|
continue
|
|
|
|
if not res.headers.get("Content-Type").startswith("text/html"):
|
2018-03-16 03:13:43 -04:00
|
|
|
continue
|
|
|
|
if res.text.find("<title>") == -1:
|
|
|
|
continue
|
|
|
|
title = res.text[res.text.find("<title>")+7:res.text.find("</title>")]
|
|
|
|
title = HTMLParser().unescape(title)
|
|
|
|
title = title.replace("\n","").strip()
|
2018-07-02 12:43:13 -04:00
|
|
|
title = title.encode("latin-1").decode("utf-8")
|
2018-03-16 03:13:43 -04:00
|
|
|
hostname = urlparse(url).hostname
|
2018-05-25 15:21:18 -04:00
|
|
|
bot.msg(f"[ \x0310{title} \x03] - \x0304{hostname}")
|