2020-02-01 23:48:35 +02:00
|
|
|
from timeit import default_timer as timer
|
|
|
|
from urllib.request import urlopen
|
2020-02-02 23:08:15 +02:00
|
|
|
from modules.common.module import BotModule
|
2020-02-01 23:48:35 +02:00
|
|
|
|
2020-02-02 23:08:15 +02:00
|
|
|
|
|
|
|
class MatrixModule(BotModule):
|
2020-02-01 23:48:35 +02:00
|
|
|
async def matrix_message(self, bot, room, event):
|
|
|
|
args = event.body.split()
|
|
|
|
args.pop(0)
|
|
|
|
|
2020-02-02 23:08:15 +02:00
|
|
|
url = args[0]
|
2020-02-01 23:48:35 +02:00
|
|
|
|
|
|
|
# check url
|
2020-02-02 23:08:15 +02:00
|
|
|
if not (url.startswith('http://') or url.startswith('https://')):
|
2020-02-01 23:48:35 +02:00
|
|
|
# print ("adding trailing https")
|
2020-02-02 23:08:15 +02:00
|
|
|
url = "https://" + url
|
|
|
|
|
2020-02-01 23:48:35 +02:00
|
|
|
print(url)
|
|
|
|
|
|
|
|
start = timer()
|
|
|
|
try:
|
|
|
|
data = urlopen(url)
|
2020-02-02 23:08:15 +02:00
|
|
|
length = format(len(data.read()) / 1024, '.3g') # kB
|
2020-02-01 23:48:35 +02:00
|
|
|
retcode = data.getcode()
|
|
|
|
|
|
|
|
except Exception as e:
|
2020-02-02 23:08:15 +02:00
|
|
|
await bot.send_text(room, "Ping failed: " + str(e))
|
|
|
|
print("Error: " + str(e))
|
|
|
|
return False
|
|
|
|
|
2020-02-01 23:48:35 +02:00
|
|
|
end = timer()
|
|
|
|
|
2020-02-02 23:08:15 +02:00
|
|
|
await bot.send_text(room, url + ": OK (" + str(retcode) + ") / " + "Size: " + str(length) +
|
|
|
|
" kB / Time: " + str(format(end - start, '.3g')) + " sec")
|
2020-02-01 23:48:35 +02:00
|
|
|
|
2020-02-02 23:08:15 +02:00
|
|
|
def help(self):
|
|
|
|
return 'check if IP or URL is accessible'
|