Browse Source

Improvements: exit when no time difference, change to os.environ, typo

Foppe Hemminga 6 years ago
parent
commit
cc7cdb2bbd
4 changed files with 16 additions and 6 deletions
  1. 9 0
      data.py
  2. 5 5
      database.py
  3. 1 1
      stocks.py
  4. 1 0
      timestamp.txt

+ 9 - 0
data.py

@@ -3,6 +3,7 @@ import psycopg2.extras
 import stocks
 from dotenv import load_dotenv
 import os
+import sys
 from pprint import pprint
 from datetime import datetime
 from urllib.parse import urlencode
@@ -41,6 +42,14 @@ cursor.execute(query)
 res = cursor.fetchone()
 timestamp_latest = res.timestamp
 
+with open('timestamp.txt', 'w+') as f:
+    timestamp_stored = f.read() or 0
+    if timestamp_latest != timestamp_stored:
+        f.write("{}".format(timestamp_latest))
+    else:
+        # Exit when there is nothing to do
+        sys.exit()
+
 print(timestamp_previous)
 print(timestamp_latest)
 

+ 5 - 5
database.py

@@ -6,9 +6,9 @@ import psycopg2
 # Develop only
 load_dotenv()
 
-host = os.getenv("STOCK_DB_HOST")
-port = os.getenv("STOCK_DB_PORT")
-database = os.getenv("STOCK_DB_DATABASE")
-user = os.getenv("STOCK_DB_USER")
-password = os.getenv("STOCK_DB_PASSWD")
+host = os.environ["STOCK_DB_HOST"]
+port = os.environ["STOCK_DB_PORT"]
+database = os.environ["STOCK_DB_DATABASE"]
+user = os.environ["STOCK_DB_USER"]
+password = os.environ["STOCK_DB_PASSWD"]
 db = psycopg2.connect(host=host, port=port, database=database, user=user, password=password)

+ 1 - 1
stocks.py

@@ -1,5 +1,5 @@
 # stock name, stock number, max price, min(price * drop) in B
-# So this means: stock name, stock # 15 (FHC), min price $335  and a $10B drop
+# So this means: stock name, stock # 15 (FHC), max price $335  and a > $10B drop
 fhc = ("FHC", 15, 335, 10)
 
 # Put all stocks in this set

+ 1 - 0
timestamp.txt

@@ -0,0 +1 @@
+1559608500