Skip to content

Commit

Permalink
Added ability to set the interval to 0 to allow the script to exit af…
Browse files Browse the repository at this point in the history
…ter one run.
  • Loading branch information
thejeffreystone committed Mar 10, 2019
1 parent 2ffe00d commit 15a74e6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<stock_name>/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.
Expand Down
11 changes: 9 additions & 2 deletions alpha_vantage_to_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion env-sample
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ api_key='<my_alpha_vantag_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:
Expand Down

0 comments on commit 15a74e6

Please sign in to comment.