nxy/bot/isup.py

34 lines
1001 B
Python
Raw Normal View History

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