From e3426ef2ae5fcd0e3bb8d1c0c2133d666d1e03da Mon Sep 17 00:00:00 2001 From: Swee Date: Mon, 11 Nov 2024 16:46:44 -0800 Subject: [PATCH] Update sweebot.py --- sweebot.py | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/sweebot.py b/sweebot.py index 2450549..e94a56e 100644 --- a/sweebot.py +++ b/sweebot.py @@ -75,7 +75,24 @@ color_map = { '\033[0m': '\x03', # Reset } pattern = re.compile(r'\033\[\d+(;\d+)*m|\033\[\?25[lh]|\033\[\?7[lh]|\033\[\d+C|\033\[\d+A|\033\[\d+D|\033\[\d+B') +def humanbytes(B): + """Return the given bytes as a human friendly KB, MB, GB, or TB string.""" + B = float(B) + KB = float(1024) + MB = float(KB ** 2) # 1,048,576 + GB = float(KB ** 3) # 1,073,741,824 + TB = float(KB ** 4) # 1,099,511,627,776 + if B < KB: + return '{0}{1}'.format(B,'Bytes' if 0 == B > 1 else 'Byte') + elif KB <= B < MB: + return '{0:.2f}KB'.format(B / KB) + elif MB <= B < GB: + return '{0:.2f}MB'.format(B / MB) + elif GB <= B < TB: + return '{0:.2f}GB'.format(B / GB) + elif TB <= B: + return '{0:.2f}TB'.format(B / TB) def replace_color_codes(text): def replacer(match): code = match.group(0) @@ -164,6 +181,7 @@ channel_irc = ["##sweezero"] botnick_irc = environ.get('SBnick') botnickpass_irc =environ.get('SBuser') botpass_irc = environ.get('SBpass') +allowedparse = ["text/html", "application/x-httpd-php"] irc = bot_irc() irc2 = bot_irc() irc3 = bot_irc() @@ -591,27 +609,36 @@ while True: if i[:8] == "https://": try: e = get(i, headers=headers, timeout=10) - if e.ok: - soup = BeautifulSoup(e.text, 'html.parser') - multiline("(" + nick + ") " + (soup.title.string if soup.title != None else "[No title provided]"), channel) + if content_type in allowedparse: + if e.ok: + soup = BeautifulSoup(e.text, 'html.parser') + multiline("(" + nick + ") " + (soup.title.string if soup.title != None else "[No title provided]"), channel) + else: + multiline("(" + nick + ") [HTTP " + str(e.status_code) + "]", channel) else: - multiline("(" + nick + ") [HTTP " + str(e.status_code) + "]", channel) + multiline("(" + nick + ") [" + humanbytes(content_len) + " " + str(content_type) + "]", channel) except rex.SSLError as ex: multiline("(" + nick + ") [SSL Error: " + str(ex.message) + "]", channel) except Exception as ex: multiline("(" + nick + ") [Request error: " + str(ex.message) + "]", channel) elif i[:7] == "http://": e = get(i, headers=headers, timeout=10) - if e.ok: - soup = BeautifulSoup(e.text, 'html.parser') - multiline("(" + nick + ") " + (soup.title.string if soup.title != None else "[No title provided]"), channel) + header = e.headers + content_type = header.get('content-type') + content_len = header.get('Content-length') + if content_type in allowedparse: + if e.ok: + soup = BeautifulSoup(e.text, 'html.parser') + multiline("(" + nick + ") " + (soup.title.string if soup.title != None else "[No title provided]"), channel) + else: + multiline("(" + nick + ") [HTTP " + str(e.status_code) + "]", channel) else: - multiline("(" + nick + ") [HTTP " + str(e.status_code) + "]", channel) + multiline("(" + nick + ") [" + humanbytes(content_len) + " " + str(content_type) + "]", channel) except: print(traceback.format_exc()) elif "JOIN" in text and "#nixsanctuary" in text: nick = text.split(":")[1].split("!")[0] - if not "swe" in nick: + if not "Meow" in nick: irc.send_irc("##hiya", "hiya: " + nick + " has joined #nixsanctuary") pass