From 24ae221f4f60e5c09585cc0a793fae2340d0ebe8 Mon Sep 17 00:00:00 2001
From: Barry Carey <mcarey66@gmail.com>
Date: Sat, 26 Jan 2019 12:17:49 -0500
Subject: [PATCH] Add singlerun flag.  Set server before each run to force ping
 to update

---
 .gitignore           | 67 ++++++++++++++++++++++++++++++++++++++++++++
 InfluxdbSpeedtest.py | 13 +++++----
 README.md            |  5 ++++
 3 files changed, 80 insertions(+), 5 deletions(-)
 create mode 100644 .gitignore

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..2c71f7b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,67 @@
+# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
+# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
+
+# User-specific stuff
+.idea/**/workspace.xml
+.idea/**/tasks.xml
+.idea/**/usage.statistics.xml
+.idea/**/dictionaries
+.idea/**/shelf
+venv
+
+# Generated files
+.idea/**/contentModel.xml
+
+# Sensitive or high-churn files
+.idea/**/dataSources/
+.idea/**/dataSources.ids
+.idea/**/dataSources.local.xml
+.idea/**/sqlDataSources.xml
+.idea/**/dynamic.xml
+.idea/**/uiDesigner.xml
+.idea/**/dbnavigator.xml
+
+# Gradle
+.idea/**/gradle.xml
+.idea/**/libraries
+
+# Gradle and Maven with auto-import
+# When using Gradle or Maven with auto-import, you should exclude module files,
+# since they will be recreated, and may cause churn.  Uncomment if using
+# auto-import.
+# .idea/modules.xml
+# .idea/*.iml
+# .idea/modules
+
+# CMake
+cmake-build-*/
+
+# Mongo Explorer plugin
+.idea/**/mongoSettings.xml
+
+# File-based project format
+*.iws
+
+# IntelliJ
+out/
+
+# mpeltonen/sbt-idea plugin
+.idea_modules/
+
+# JIRA plugin
+atlassian-ide-plugin.xml
+
+# Cursive Clojure plugin
+.idea/replstate.xml
+
+# Crashlytics plugin (for Android Studio and IntelliJ)
+com_crashlytics_export_strings.xml
+crashlytics.properties
+crashlytics-build.properties
+fabric.properties
+
+# Editor-based Rest Client
+.idea/httpRequests
+
+# Android studio 3.1+ serialized cache file
+.idea/caches/build_file_checksums.ser
\ No newline at end of file
diff --git a/InfluxdbSpeedtest.py b/InfluxdbSpeedtest.py
index 6eeda3b..08121ec 100644
--- a/InfluxdbSpeedtest.py
+++ b/InfluxdbSpeedtest.py
@@ -46,10 +46,11 @@ class configManager():
 
 class InfluxdbSpeedtest():
 
-    def __init__(self, config=None):
+    def __init__(self, config=None, single_run=False):
 
         self.config = configManager(config=config)
         self.output = self.config.output
+        self.single_run = single_run
         self.influx_client = InfluxDBClient(
             self.config.influx_address,
             self.config.influx_port,
@@ -62,7 +63,6 @@ class InfluxdbSpeedtest():
 
         self.speedtest = None
         self.results = None
-        self.setup_speedtest()
 
     def setup_speedtest(self):
 
@@ -81,7 +81,7 @@ class InfluxdbSpeedtest():
             print('ERROR: No matched servers: {}'.format(self.config.test_server[0]))
             sys.exit(1)
         except speedtest.ServersRetrievalError:
-            print('ERROR: Cannot retrieve speedtest server list')
+            print('ERROR: Cannot retrieve speedtest.net server list')
             sys.exit(1)
         except speedtest.InvalidServerIDType:
             print('{} is an invalid server type, must be int'.format(self.config.test_server[0]))
@@ -119,11 +119,13 @@ class InfluxdbSpeedtest():
     def run(self):
 
         while True:
-
+            self.setup_speedtest()
             self.speedtest.download()
             self.speedtest.upload()
 
             self.send_results()
+            if self.single_run:
+                return
 
             time.sleep(self.config.delay)
 
@@ -160,8 +162,9 @@ def main():
 
     parser = argparse.ArgumentParser(description="A tool to send Plex statistics to InfluxDB")
     parser.add_argument('--config', default='config.ini', dest='config', help='Specify a custom location for the config file')
+    parser.add_argument('--singlerun', action='store_true', help='Only runs through once, does not keep monitoring')
     args = parser.parse_args()
-    collector = InfluxdbSpeedtest(config=args.config)
+    collector = InfluxdbSpeedtest(config=args.config, single_run=args.singlerun)
     collector.run()
 
 
diff --git a/README.md b/README.md
index d723976..52e9765 100644
--- a/README.md
+++ b/README.md
@@ -5,6 +5,11 @@
 
 This tool is a wrapper for speedtest-cli which allows you to run periodic speedtets and save the results to Influxdb 
 
+## Command Line Args
+--config whatever.ini - Use a different ini file
+
+--singlerun - Run once and exit
+
 ## Configuration within config.ini
 
 #### GENERAL
-- 
GitLab