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
|
*.db
|
||||||
*.log
|
*.log
|
||||||
|
*/__pycache__/*
|
||||||
|
@ -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')
|
||||||
|
|
||||||
|
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:
|
||||||
|
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()
|
Checkthedatabase()
|
||||||
|
|
||||||
# Empty response json
|
# Start the data fetching backend process
|
||||||
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 = threading.Thread(target=get_the_data)
|
||||||
fetch_thread.daemon = True
|
fetch_thread.daemon = True
|
||||||
fetch_thread.start()
|
fetch_thread.start()
|
||||||
|
|
||||||
db = sqlite3.connect(database)
|
# Start the Flask app
|
||||||
count = 1
|
app.run()
|
||||||
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:
|
|
||||||
output = str(count) + ".. "
|
|
||||||
print(output, end = '\r')
|
|
||||||
count = count + 1
|
|
||||||
time.sleep(30)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user