<commit_before>import requests

from lib.command import Command


def get_status(host):
    website_url = 'http://isitup.org/{0}.json'.format(host)
    response = requests.get(website_url)
    data = response.json()
    return data.get('status_code', 0), data.get('response_ip', 0), data.get('response_time', 0)


class PingCommand(Command):
    name = 'ping'
    aliases = ['isup']
    description = 'Checks whether a given host is up.'

    def run(self, message, args):
        if not args:
            self.reply(message, 'Please supply a host to check.')
            return

        host = args[0].strip()
        status, ip, response_time = get_status(host)
        if status == 1:
            response = '{0} ({1}) is up ({2} seconds).'.format(host, ip, response_time)
        elif status == 2:
            response = '{0} looks down from here!'.format(host)
        elif status == 3:
            response = '{0} doesn\'t appear to be a valid hostname!'.format(host)
        else:
            response = 'isitup.org api error'
        self.reply(message, response, disable_web_page_preview=True)<commit_msg>ping: rewrite to just use native ping binary<commit_after>import platform
import subprocess

import re

from lib.command import Command


class PingCommand(Command):
    name = 'ping'
    aliases = ['isup']
    description = 'Checks whether a given host is up.'

    def run(self, message, args):
        if not args:
            self.reply(message, 'Please supply a host to check.')
            return

        host = args[0].strip()
        self.bot.telegram.send_chat_action(message.chat.id, 'typing')
        try:
            ping = str(subprocess.check_output(
                ['ping', '-n' if platform.system().lower() == 'windows' else '-c', '1', host], stderr=subprocess.STDOUT,
                timeout=5))
        except subprocess.TimeoutExpired as ex:
            self.reply(message, '{0} looks down from here. (Timed out after {1} seconds)'.format(host, ex.timeout),
                       disable_web_page_preview=True)
            return
        except subprocess.CalledProcessError as ex:
            if 'unknown host' in str(ex.output):
                self.reply(message, '{0} doesn\'t seem to be a resolvable host!'.format(host),
                           disable_web_page_preview=True)
            return

        ip_match = re.match('.*\((.+)\):.*', ping)
        time_match = re.match('.*time=(\d+)', ping)
        time = time_match.group(1).strip() if time_match and time_match.groups() else 0
        ip = ip_match.group(1).strip() if ip_match and ip_match.groups() else None

        self.reply(message, '{0} ({1}) is up ({2} ms).'.format(host, ip or host, time), disable_web_page_preview=True)
{'fraction_non_alphanumeric': 0.13062568605927552, 'fraction_numerical': 0.009147457006952067, 'mean_word_length': 2.4476670870113493, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3057593', 'n_tokens_mistral': 832, 'n_tokens_neox': 801, 'n_words': 232}