1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-12-25 01:31:47 +03:00
leon/packages/checker/isitdown.py

40 lines
1.0 KiB
Python
Raw Normal View History

2019-02-10 15:26:50 +03:00
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
import utils
2019-05-03 18:31:09 +03:00
def run(string, entities):
2019-02-10 15:26:50 +03:00
"""Check if a website is down or not"""
domains = []
2019-02-10 15:26:50 +03:00
output = ''
for item in entities:
if item['entity'] == 'url':
domains.append(item['resolution']['value'].lower())
2019-02-10 15:26:50 +03:00
for i, domain in enumerate(domains):
state = 'up'
websitename = domain[:domain.find('.')].title()
utils.output('inter', 'checking', utils.translate('checking', { 'website_name': websitename }))
try:
r = utils.http('GET', 'http://' + domain)
if (r.status_code != requests.codes.ok):
state = 'down'
utils.output('inter', 'up', utils.translate(state, { 'website_name': websitename }))
except requests.exceptions.RequestException as e:
utils.output('inter', 'down', utils.translate('errors', { 'website_name': websitename }))
if len(domains) > 1 and i >= 0 and i + 1 < len(domains):
output += ' '
if len(domains) == 0:
return utils.output('end', 'invalid_domain_name', utils.translate('invalid_domain_name'))
else:
return utils.output('end', 'done')