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
|
||||||
@ -176,38 +184,52 @@ def write_dict_to_database(in_dict, connection):
|
|||||||
connection.commit()
|
connection.commit()
|
||||||
|
|
||||||
def get_the_data():
|
def get_the_data():
|
||||||
#cursor = db.cursor()
|
#cursor = db.cursor()
|
||||||
while True:
|
while True:
|
||||||
db = sqlite3.connect(database)
|
db = sqlite3.connect(database)
|
||||||
write_dict_to_database(json.loads(fetch_kraken()), db)
|
write_dict_to_database(json.loads(fetch_kraken()), db)
|
||||||
write_dict_to_database(json.loads(fetch_bitfinex()), db)
|
write_dict_to_database(json.loads(fetch_bitfinex()), db)
|
||||||
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('/')
|
||||||
Checkthedatabase()
|
def get_data():
|
||||||
|
# Get the time (t) argument from the url"
|
||||||
# Empty response json
|
query_timestamp = request.args.get('t')
|
||||||
empty_dict = {"exchange": "", "timestamp": 0, "open": 0, "high": 0, "low": 0, "close": 0, "volume_quote": 0, "volume_base": 0, "trades": 0}
|
query_pretty = request.args.get('pretty')
|
||||||
empty_json = json.dumps(empty_dict)
|
|
||||||
|
database_lock.acquire()
|
||||||
database_lock = threading.Lock()
|
db = sqlite3.connect(database)
|
||||||
# make this run in thread and allow contium to http api stuff
|
if query_timestamp:
|
||||||
fetch_thread = threading.Thread(target=get_the_data)
|
rows = db.execute("SELECT exchange, timestamp, open, high, low, close FROM ohlc WHERE timestamp > ?", (query_timestamp,)).fetchall()
|
||||||
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()
|
||||||
|
Loading…
Reference in New Issue
Block a user