From 15a74e61a12bccacd548060d8034d4df4c63ef1b Mon Sep 17 00:00:00 2001 From: Jeffrey Stone Date: Sat, 9 Mar 2019 21:50:45 -0500 Subject: [PATCH] Added ability to set the interval to 0 to allow the script to exit after one run. --- README.md | 3 ++- alpha_vantage_to_mqtt.py | 11 +++++++++-- env-sample | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a6bc048..dd6581b 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,11 @@ This script was written to provide stock price to a MQTT sensor in the [HomeAssi ## Run -This script is meant to run with something like supervisord, but can be executed manually and will stay running until killed. +This script is meant to run with something like supervisord, and has a default interval of 3600 seconds (1 hour). If you want to have this script to update your stocks continiously set the interval greater than zero. If interval is set to 0 the script will exit after running once. If you want to have cron or some other system mange the script execution then simply set the interval to 0 in the .env There is a brief write up about the specific use case I built this for at [https://slackerlabs.org/2019/03/08/home-assistant-alpha-vantage/] + ## Features * This script publishes stock price to topic `stock//price` * This script utilizes the `get_batch_stock_quotes` method of the Alpha Vantage API to reduce the number of calls to the API to avoid hitting the API more than once a second. diff --git a/alpha_vantage_to_mqtt.py b/alpha_vantage_to_mqtt.py index 805716c..39c29c4 100755 --- a/alpha_vantage_to_mqtt.py +++ b/alpha_vantage_to_mqtt.py @@ -9,6 +9,7 @@ import os import time import json +import sys import paho.mqtt.client as mqtt from alpha_vantage.timeseries import TimeSeries from alpha_vantage.foreignexchange import ForeignExchange @@ -111,8 +112,14 @@ def main(interval): if http_event_collector_key: splunkIt(records,symbols,total_elapsed_time,api_call_time) if app_mode == 'debug': - print("Script Completed...time to sleep for {} seconds\n".format(interval)) - time.sleep(interval) + print("Script Completed...") + if interval > 0: + print("Time to sleep for {} seconds\n".format(interval)) + time.sleep(interval) + else: + if app_mode == 'debug': + print("No Interval set...exiting...\n") + sys.exit() if __name__ == "__main__": main(interval) \ No newline at end of file diff --git a/env-sample b/env-sample index 70cd1ef..06c2507 100644 --- a/env-sample +++ b/env-sample @@ -12,7 +12,7 @@ api_key='' # Comma delimited list of stocks you want to monitor stocks=GOOG,AMZN,APPL,FB -# Interval, in seconds, you want to run the updates. 3600 = 1 hour. +# Interval, in seconds, you want to run the updates. 3600 = 1 hour. If you set inteval to 0 the script will exit after running once. interval=3600 # Splunk Server: