fulvia/modules/tld.py

58 lines
1.4 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."""
2020-01-07 18:58:19 -05:00
if len(trigger.args) < 2:
2018-03-21 13:14:43 -04:00
return bot.reply("What TLD?")
2020-01-07 18:58:19 -05:00
word = trigger.args[1]
2018-03-16 03:13:43 -04:00
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:
2018-05-25 15:21:18 -04:00
return bot.msg(f"Unable to find data for TLD: {word}")
2018-03-16 03:13:43 -04:00
2018-07-12 20:17:34 -04:00
table_headers = []
for th in td.parent.parent.find_all("th"):
header = th.string
if not header:
header = th.a.string
table_headers.append(header)
2018-03-16 03:13:43 -04:00
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 | "
2018-07-12 20:04:45 -04:00
msg = msg[:-3].replace("\n","")
2018-03-16 03:13:43 -04:00
bot.msg(msg)