fulvia/modules/tld.py

54 lines
1.3 KiB
Python
Raw Normal View History

2018-03-16 03:13:43 -04:00
#!/usr/bin/env python3
"""
Scrapes and shows information about Top Level Domains.
"""
import bs4
import requests
from module import commands, example
URI = 'https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains'
@commands('tld')
@example('.tld me')
def gettld(bot, trigger):
"""Show information about the given Top Level Domain."""
word = trigger.group(2).strip()
2018-03-21 13:14:43 -04:00
if not word:
return bot.reply("What TLD?")
2018-03-16 03:13:43 -04:00
if " " in word:
return bot.reply("One TLD at a time.")
if not word.startswith("."):
word = "." + word
res = requests.get(URI, verify=True)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, "html.parser")
td = soup.find("td", string=word)
if not td:
return bot.say(f"Unable to find data for TLD: {word}")
table_headers = [th.string for th in td.parent.parent.find_all("th")]
if None in table_headers:
n = table_headers.index(None)
table_headers[n] = "Administrator"
# this should be the only header on the page where th.string fails
table_entries = []
for td in td.parent.find_all("td"):
string = td.text.strip()
if not string:
try:
string = td.a.string
except AttributeError:
string = ""
table_entries.append(string)
msg = "[\x0304TLD\x03] "
for n in range(len(table_headers)):
msg += f"\x0310{table_headers[n]}\x03: "
msg += f"\x0312{table_entries[n]}\x03 | "
msg = msg[:-3]
bot.msg(msg)