nxy/bot/isup.py

34 lines
1001 B
Python

# -*- coding: utf-8 -*-
from urllib.parse import urlparse
import requests
from docopt import Dict
from irc3.plugins.command import command
from irc3.utils import IrcString
from . import Plugin
class Useless(Plugin):
@command
def isup(self, mask: IrcString, target: IrcString, args: Dict):
"""Checks if an address is up or down
%%isup <address>
"""
address = args['<address>']
if not address.startswith('http://') and not address.startswith('https://'):
address = 'https://{}'.format(address)
try:
res = requests.head(address)
res.raise_for_status()
state = 'up'
except requests.ConnectionError:
state = 'down (timeout)'
except requests.HTTPError as e:
state = f'down ({e.response.status_code} {e.response.reason})'
parsed = urlparse(address)
return '\x02{}://{}\x02 seems to be \x02{}\x02'.format(parsed.scheme, parsed.netloc, state)