Added simple Flask api to kraken_fetch.py.
This commit is contained in:
parent
d78dfb6933
commit
30b8c4b274
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
*.db
|
||||
*.log
|
||||
*/__pycache__/*
|
||||
|
@ -4,8 +4,16 @@ import krakenex
|
||||
import json, sqlite3
|
||||
import requests, os, time
|
||||
import threading
|
||||
from flask import Flask, request
|
||||
|
||||
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():
|
||||
## Some sanity for the database
|
||||
@ -29,7 +37,7 @@ def Checkthedatabase():
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
db = sqlite3.connect("database")
|
||||
db = sqlite3.connect(database)
|
||||
|
||||
# Check if the table exists
|
||||
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_gemini()), db)
|
||||
db.close()
|
||||
print("fetches done at", time.time(), "sleeping now for 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()
|
||||
|
||||
# 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)
|
||||
|
||||
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))
|
||||
database_lock.acquire()
|
||||
db = sqlite3.connect(database)
|
||||
if query_timestamp:
|
||||
rows = db.execute("SELECT exchange, timestamp, open, high, low, close FROM ohlc WHERE timestamp > ?", (query_timestamp,)).fetchall()
|
||||
else:
|
||||
output = str(count) + ".. "
|
||||
print(output, end = '\r')
|
||||
count = count + 1
|
||||
time.sleep(30)
|
||||
rows = db.execute('SELECT exchange, timestamp, open, high, low, close FROM ohlc').fetchall()
|
||||
query_timestamp = 0
|
||||
|
||||
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()
|
||||
|
Loading…
Reference in New Issue
Block a user