Added simple Flask api to kraken_fetch.py.

This commit is contained in:
kalzu 2022-12-28 01:10:26 +02:00
parent d78dfb6933
commit 30b8c4b274
2 changed files with 51 additions and 28 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.db *.db
*.log *.log
*/__pycache__/*

View File

@ -4,8 +4,16 @@ import krakenex
import json, sqlite3 import json, sqlite3
import requests, os, time import requests, os, time
import threading import threading
from flask import Flask, request
database = "btc_ohlc.db" database = "btc_ohlc.db"
app = Flask(__name__)
database_lock = threading.Lock()
# Empty response json
empty_dict = {"exchange": "", "timestamp": 0, "open": 0, "high": 0, "low": 0, "close": 0, "volume_quote": 0, "volume_base": 0, "trades": 0}
empty_json = json.dumps(empty_dict)
def Checkthedatabase(): def Checkthedatabase():
## Some sanity for the database ## Some sanity for the database
@ -29,7 +37,7 @@ def Checkthedatabase():
db.commit() db.commit()
db.close() db.close()
db = sqlite3.connect("database") db = sqlite3.connect(database)
# Check if the table exists # Check if the table exists
table_exists = False table_exists = False
@ -184,30 +192,44 @@ def get_the_data():
write_dict_to_database(json.loads(fetch_bitstamp()), db) write_dict_to_database(json.loads(fetch_bitstamp()), db)
write_dict_to_database(json.loads(fetch_gemini()), db) write_dict_to_database(json.loads(fetch_gemini()), db)
db.close() db.close()
print("fetches done at", time.time(), "sleeping now for 290")
time.sleep(290) time.sleep(290)
@app.route('/')
def get_data():
# Get the time (t) argument from the url"
query_timestamp = request.args.get('t')
query_pretty = request.args.get('pretty')
Checkthedatabase() database_lock.acquire()
db = sqlite3.connect(database)
# Empty response json if query_timestamp:
empty_dict = {"exchange": "", "timestamp": 0, "open": 0, "high": 0, "low": 0, "close": 0, "volume_quote": 0, "volume_base": 0, "trades": 0} rows = db.execute("SELECT exchange, timestamp, open, high, low, close FROM ohlc WHERE timestamp > ?", (query_timestamp,)).fetchall()
empty_json = json.dumps(empty_dict)
database_lock = threading.Lock()
# make this run in thread and allow contium to http api stuff
fetch_thread = threading.Thread(target=get_the_data)
fetch_thread.daemon = True
fetch_thread.start()
db = sqlite3.connect(database)
count = 1
while True:
if count % 10 == 0:
print("------- status ", count, "-------")
top2_rows = db.execute("SELECT * FROM ohlc LIMIT 2 OFFSET (SELECT COUNT(*) FROM ohlc) - 2").fetchall()
print(json.dumps(top2_rows, indent=4))
else: else:
output = str(count) + ".. " rows = db.execute('SELECT exchange, timestamp, open, high, low, close FROM ohlc').fetchall()
print(output, end = '\r') query_timestamp = 0
count = count + 1
time.sleep(30) database_lock.release()
data = {
"timestamp": time.time(),
"rows": rows
}
if query_pretty:
response = json.dumps(data, indent=2, separators=(';\n', ' :'))
else:
response = json.dumps(data)
return response, 200, {'Content-Type': 'application/json'}
if __name__ == '__main__':
# Make sanity checks for the database
Checkthedatabase()
# Start the data fetching backend process
fetch_thread = threading.Thread(target=get_the_data)
fetch_thread.daemon = True
fetch_thread.start()
# Start the Flask app
app.run()