updated ping.py

This commit is contained in:
Ross Mountjoy 2020-02-07 13:46:44 -05:00
parent 0f72583f31
commit da44d56fa8
2 changed files with 15 additions and 13 deletions

View File

@ -1,4 +1,5 @@
from requests import get import platform
import subprocess
class Platform: class Platform:
@ -8,16 +9,13 @@ class Platform:
self.__dict__[key] = value self.__dict__[key] = value
def process(self): def process(self):
try: param = "-n" if platform.system().lower() == "windows" else "-c"
value = get(self.resource) command = ["ping", param, "1", self.resource]
except Exception: up = subprocess.call(command) == 0
icon_class = "theme-failure-text"
if 599 >= value.status_code >= 400: if up is True:
icon_class = "theme-failure-text"
if 399 >= value.status_code >= 300:
icon_class = "theme-warning-text"
if 299 >= value.status_code >= 100:
icon_class = "theme-success-text" icon_class = "theme-success-text"
else:
icon_class = "theme-failure-text"
return f"<i class='material-icons right {icon_class}'>fiber_manual_record </i>" return f"<i class='material-icons right {icon_class}'>fiber_manual_record </i>"

View File

@ -6,6 +6,7 @@ import transmissionrpc
# from pprint import PrettyPrinter # from pprint import PrettyPrinter
# pp = PrettyPrinter() # pp = PrettyPrinter()
class Platform: class Platform:
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
# parse the user's options from the config entries # parse the user's options from the config entries
@ -15,20 +16,23 @@ class Platform:
if not hasattr(self, "port"): if not hasattr(self, "port"):
self.port = 9091 self.port = 9091
if not hasattr(self, "host"): if not hasattr(self, "host"):
self.host = 'localhost' self.host = "localhost"
self.tc = transmissionrpc.Client(self.host, port=self.port, user=self.user, password=self.password) self.tc = transmissionrpc.Client(
self.host, port=self.port, user=self.user, password=self.password
)
def process(self): def process(self):
torrents = len(self.tc.get_torrents()) torrents = len(self.tc.get_torrents())
data = {} data = {}
for key, field in self.tc.session_stats().__dict__['_fields'].items(): for key, field in self.tc.session_stats().__dict__["_fields"].items():
data[key] = field.value data[key] = field.value
# pp.pprint (data) # pp.pprint (data)
value_template = render_template_string(self.value_template, **data) value_template = render_template_string(self.value_template, **data)
return value_template return value_template
# Testing # Testing
# test = Platform(host='192.168.1.19', user='', password='').process() # test = Platform(host='192.168.1.19', user='', password='').process()