From 3bda8cc3e83892f42e74dd1c1205414b049a07cd Mon Sep 17 00:00:00 2001 From: ShadeAnimator Date: Fri, 7 Feb 2020 18:18:17 +0300 Subject: [PATCH] Transmission platform added --- dashmachine/main/utils.py | 2 +- dashmachine/platform/ping.py | 4 ++-- dashmachine/platform/rest.py | 4 ++-- dashmachine/platform/transmission.py | 34 ++++++++++++++++++++++++++++ requirements.txt | 1 + 5 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 dashmachine/platform/transmission.py diff --git a/dashmachine/main/utils.py b/dashmachine/main/utils.py index 3727022..b4d23ba 100755 --- a/dashmachine/main/utils.py +++ b/dashmachine/main/utils.py @@ -135,5 +135,5 @@ def get_data_source(data_source): module = importlib.import_module( f"dashmachine.platform.{data_source['platform']}", "." ) - platform = module.Platform(data_source, data_source_args) + platform = module.Platform(data_source, **data_source_args) return platform.process() diff --git a/dashmachine/platform/ping.py b/dashmachine/platform/ping.py index 06dd47a..9f7ed1b 100644 --- a/dashmachine/platform/ping.py +++ b/dashmachine/platform/ping.py @@ -2,9 +2,9 @@ from requests import get class Platform: - def __init__(self, data_source, data_source_args): + def __init__(self, *args, **kwargs): # parse the user's options from the config entries - for key, value in data_source_args.items(): + for key, value in kwargs.items(): self.__dict__[key] = value def process(self): diff --git a/dashmachine/platform/rest.py b/dashmachine/platform/rest.py index 455a863..61b5d61 100644 --- a/dashmachine/platform/rest.py +++ b/dashmachine/platform/rest.py @@ -5,9 +5,9 @@ from flask import render_template_string class Platform: - def __init__(self, data_source, data_source_args): + def __init__(self, *args, **kwargs): # parse the user's options from the config entries - for key, value in data_source_args.items(): + for key, value in kwargs.items(): self.__dict__[key] = value # set defaults for omitted options diff --git a/dashmachine/platform/transmission.py b/dashmachine/platform/transmission.py new file mode 100644 index 0000000..ac6e70f --- /dev/null +++ b/dashmachine/platform/transmission.py @@ -0,0 +1,34 @@ +import json +from flask import render_template_string +import transmissionrpc + + +# from pprint import PrettyPrinter +# pp = PrettyPrinter() + +class Platform: + def __init__(self, *args, **kwargs): + # parse the user's options from the config entries + for key, value in kwargs.items(): + self.__dict__[key] = value + + if not hasattr(self, "port"): + self.port = 9091 + if not hasattr(self, "host"): + self.host = 'localhost' + + self.tc = transmissionrpc.Client(self.host, port=self.port, user=self.user, password=self.password) + + def process(self): + + torrents = len(self.tc.get_torrents()) + data = {} + for key, field in self.tc.session_stats().__dict__['_fields'].items(): + data[key] = field.value + # pp.pprint (data) + + value_template = render_template_string(self.value_template, **data) + return value_template + +# Testing +# test = Platform(host='192.168.1.19', user='', password='').process() diff --git a/requirements.txt b/requirements.txt index 2033357..0474a60 100755 --- a/requirements.txt +++ b/requirements.txt @@ -32,3 +32,4 @@ SQLAlchemy==1.3.13 urllib3==1.25.8 Werkzeug==0.16.1 WTForms==2.2.1 +transmissionrpc \ No newline at end of file