From ab2f071c1c66f7e04c66ae42498b669bbca3d5d6 Mon Sep 17 00:00:00 2001
From: Matt <matt@mattcarey.me>
Date: Sat, 21 Jan 2017 12:33:46 -0500
Subject: [PATCH] Cleaned up excess print messages.

---
 clients/deluge.py        |  7 +------
 clients/torrentclient.py |  7 +++++--
 clients/utorrent.py      | 17 +++++++----------
 config.ini               | 15 ++++++++++++++-
 influxdbSeedbox.py       | 30 ++++++++++++++++++++----------
 5 files changed, 47 insertions(+), 29 deletions(-)

diff --git a/clients/deluge.py b/clients/deluge.py
index dc623b0..acf9982 100644
--- a/clients/deluge.py
+++ b/clients/deluge.py
@@ -52,7 +52,6 @@ class DelugeClient(TorrentClient):
         except URLError as e:
             msg = 'Failed To check session state.  HTTP Error'
             self.send_log(msg, 'error')
-            print(msg)
             print(e)
             return None
 
@@ -110,7 +109,6 @@ class DelugeClient(TorrentClient):
         """
         msg = 'Attempting to authenticate against {} API'.format(self.torrent_client)
         self.send_log(msg, 'info')
-        print(msg)
 
         req = self._create_request(method='auth.login', params=[self.password])
 
@@ -119,7 +117,6 @@ class DelugeClient(TorrentClient):
         except URLError as e:
             msg = 'Failed To Authenticate with torrent client.  HTTP Error'
             self.send_log(msg, 'critical')
-            print(msg)
             print(e)
             sys.exit(1)
 
@@ -136,7 +133,7 @@ class DelugeClient(TorrentClient):
 
         msg = 'Successfully Authenticated With {} API'.format(self.torrent_client)
         self.send_log(msg, 'info')
-        print(msg)
+
 
     def _build_torrent_list(self, torrents):
         """
@@ -173,7 +170,6 @@ class DelugeClient(TorrentClient):
         except URLError as e:
             msg = 'Failed to get list of torrents.  HTTP Error'
             self.send_log(msg, 'error')
-            print(msg)
             print(e)
             self.torrent_list = {}
             return
@@ -181,7 +177,6 @@ class DelugeClient(TorrentClient):
         output = self._process_response(res)
         if output['error']:
             msg = 'Problem getting torrent list from {}. Error: {}'.format(self.torrent_client, output['error'])
-            print(msg)
             self.send_log(msg, 'error')
             self.torrent_list = {}
             return
diff --git a/clients/torrentclient.py b/clients/torrentclient.py
index ef3e635..babd109 100644
--- a/clients/torrentclient.py
+++ b/clients/torrentclient.py
@@ -9,7 +9,10 @@ import re
 import gzip
 
 # TODO Deal with slashes in client URL
-# TODO Unify the final torrent list so it can be built into json structure by parent instead of each child
+
+"""
+Base class for torrent clients
+"""
 
 class TorrentClient:
     """
@@ -121,7 +124,7 @@ class TorrentClient:
                 trackers[data['tracker']]['total_size'] = data['total_size']
 
         for k, v in trackers.items():
-            print(v)
+
             total_ratio = round(v['total_uploaded'] / v['total_size'], 3)
             tracker_json = [
                 {
diff --git a/clients/utorrent.py b/clients/utorrent.py
index dad5e76..1d30a23 100644
--- a/clients/utorrent.py
+++ b/clients/utorrent.py
@@ -25,8 +25,9 @@ class UTorrentClient(TorrentClient):
         handler = urllib.request.HTTPBasicAuthHandler(pwd_mgr)
         opener = urllib.request.build_opener(handler)
         token_url = self.url + '/token.html'
-        print('Attempting To Get Token From URL {}'.format(token_url))
+
         self.send_log('Attempting To Get Token From URL {}'.format(token_url), 'info')
+
         opener.open(token_url)
         urllib.request.install_opener(opener)
         req = Request(self.url + '/token.html')
@@ -34,7 +35,7 @@ class UTorrentClient(TorrentClient):
         self.cookie = res.headers['Set-Cookie'].split(';')[0]
         soup = BeautifulSoup(res, 'html.parser')
         token = soup.find("div", {"id": "token"}).text
-        print('Got Token: ' + token)
+        self.send_log('Got Token: {}'.format(token), 'info')
         self.token = token
 
     def _add_common_headers(self, req, headers=None):
@@ -54,7 +55,8 @@ class UTorrentClient(TorrentClient):
     def _create_request(self, method=None, params=None):
         # TODO Validate that we get params
         url = self.url + '/?token={}&{}'.format(self.token, params)
-        print('Creating request with url: ' + url)
+        msg = 'Creating request with url: {}'.format(url)
+        self.send_log(msg, 'debug')
 
         req = self._add_common_headers(Request(url))
 
@@ -91,15 +93,14 @@ class UTorrentClient(TorrentClient):
         """
 
         msg = 'Attempting to get tracker for hash {}'.format(hash)
-        print(msg)
         self.send_log(msg, 'debug')
+
         req = self._create_request(params='action=getprops&hash={}'.format(hash))
 
         try:
             res = urlopen(req).read().decode('utf-8')
         except URLError as e:
             msg = 'Failed to get trackers from URL for hash {}'.format(hash)
-            print(msg)
             self.send_log(msg, 'error')
             return 'N/A'
 
@@ -124,7 +125,6 @@ class UTorrentClient(TorrentClient):
         """
 
         msg = 'Attempting to get file list for hash {}'.format(hash)
-        print(msg)
         self.send_log(msg, 'debug')
 
         req = self._create_request(params='action=getfiles&hash={}'.format(hash))
@@ -133,7 +133,6 @@ class UTorrentClient(TorrentClient):
             res = urlopen(req).read().decode('utf-8')
         except URLError as e:
             msg = 'Failed to get file list from URL for hash {}'.format(hash)
-            print(msg)
             self.send_log(msg, 'error')
             return 'N/A'
 
@@ -148,8 +147,7 @@ class UTorrentClient(TorrentClient):
         """
 
         msg = 'Attempting to get all torrents from {}'.format(self.url)
-        print(msg)
-        self.send_log(msg, 'info')
+        self.send_log(msg, 'debug')
 
         req = self._create_request(params='list=1')
 
@@ -158,7 +156,6 @@ class UTorrentClient(TorrentClient):
             final = res.read().decode('utf-8')
         except URLError as e:
             msg = 'Failed to get list of all torrents'
-            print(msg)
             print(e)
             self.send_log(msg, 'error')
             self.torrent_list = {}
diff --git a/config.ini b/config.ini
index 2588a71..0c12b4e 100644
--- a/config.ini
+++ b/config.ini
@@ -15,6 +15,7 @@ Verify_SSL = False
 
 [TORRENTCLIENT]
 # Leave blank to auto pick server
+# Valid options are deluge, utorrent
 Client = utorrent
 Username = admin
 Password =
@@ -25,8 +26,20 @@ Url =
 
 [LOGGING]
 Enable = True
+
 # Valid Options: critical, error, warning, info, debug
 Level = error
+
 LogFile = output.log
+
 # Removes things such as server names and ip addresses from logs
-CensorLogs = False
\ No newline at end of file
+CensorLogs = False
+
+# Any log messages greater than or equal to this number will also be printed to the console
+# Output must also be true under GENERAL
+# DEBUG: 0
+# INFO: 1
+# WARNING: 2
+# ERROR: 3
+# CRITICAL: 4
+PrintThreshold = 0
\ No newline at end of file
diff --git a/influxdbSeedbox.py b/influxdbSeedbox.py
index 6cde9cc..23ea24e 100644
--- a/influxdbSeedbox.py
+++ b/influxdbSeedbox.py
@@ -12,8 +12,7 @@ from influxdb.exceptions import InfluxDBClientError, InfluxDBServerError
 
 
 # TODO Move urlopen login in each method call to one central method
-# TODO Keep track of tracker overall ratios
-# TODO Add print to _send_log.  Will but down on logging and print code in methods
+# TODO Validate that we get a valid URL from config
 
 __author__ = 'barry'
 class configManager():
@@ -23,6 +22,14 @@ class configManager():
     def __init__(self, config):
 
         self.valid_torrent_clients = ['deluge', 'utorrent']
+        self.valid_log_levels = {
+            'DEBUG': 0,
+            'INFO': 1,
+            'WARNING': 2,
+            'ERROR': 3,
+            'CRITICAL': 4
+        }
+
 
         print('Loading Configuration File {}'.format(config))
         config_file = os.path.join(os.getcwd(), config)
@@ -59,9 +66,10 @@ class configManager():
 
         #Logging
         self.logging = self.config['LOGGING'].getboolean('Enable', fallback=False)
-        self.logging_level = self.config['LOGGING']['Level'].lower()
+        self.logging_level = self.config['LOGGING']['Level'].upper()
         self.logging_file = self.config['LOGGING']['LogFile']
         self.logging_censor = self.config['LOGGING'].getboolean('CensorLogs', fallback=True)
+        self.logging_print_threshold = self.config['LOGGING'].getint('PrintThreshold', fallback=2)
 
         # TorrentClient
         self.tor_client = self.config['TORRENTCLIENT'].get('Client', fallback=None).lower()
@@ -81,14 +89,14 @@ class configManager():
         :return:
         """
 
-        valid_levels = ['critical', 'error', 'warning', 'info', 'debug']
-        if self.logging_level in valid_levels:
+
+        if self.logging_level in self.valid_log_levels:
             self.logging_level = self.logging_level.upper()
             return
         else:
             print('Invalid logging level provided. {}'.format(self.logging_level))
             print('Logging will be disabled')
-            print('Valid options are: {}'.format(', '.join(valid_levels)))
+            print('Valid options are: {}'.format(', '.join(self.valid_log_levels)))
             self.logging = None
 
 
@@ -154,6 +162,9 @@ class influxdbSeedbox():
         if not self.logger:
             return
 
+        if self.output and self.config.valid_log_levels[level.upper()] >= self.config.logging_print_threshold:
+            print(msg)
+
         # Make sure a good level was given
         if not hasattr(self.logger, level):
             self.logger.error('Invalid log level provided to send_log')
@@ -189,8 +200,7 @@ class influxdbSeedbox():
         :param json_data:
         :return:
         """
-        if self.output:
-            print(json_data)
+        self.send_log(json_data, 'debug')
 
         # TODO This bit of fuckery may turn out to not be a good idea.
         """
@@ -209,9 +219,9 @@ class influxdbSeedbox():
             self.influx_client.write_points(json_data)
         except (InfluxDBClientError, ConnectionError, InfluxDBServerError) as e:
             if hasattr(e, 'code') and e.code == 404:
-                print('Database {} Does Not Exist.  Attempting To Create')
 
-                self.send_log('Database {} Does Not Exist.  Attempting To Create', 'error')
+                msg = 'Database {} Does Not Exist.  Attempting To Create'.format(self.config.influx_database)
+                self.send_log(msg, 'error')
 
                 # TODO Grab exception here
                 self.influx_client.create_database(self.config.influx_database)
-- 
GitLab