From dd86cecec27766d18e9f0d44d2ca219c8d295d2e Mon Sep 17 00:00:00 2001 From: kalzu Date: Wed, 28 Dec 2022 20:36:44 +0200 Subject: [PATCH] Add sha256 checksum for api responses. --- btc_tracker/kraken_fetch.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/btc_tracker/kraken_fetch.py b/btc_tracker/kraken_fetch.py index e68114e..df22c1e 100755 --- a/btc_tracker/kraken_fetch.py +++ b/btc_tracker/kraken_fetch.py @@ -4,6 +4,7 @@ import krakenex, math import json, sqlite3 import requests, os, time import threading +from hashlib import sha256 from flask import Flask, request database = "btc_ohlc.db" @@ -172,7 +173,6 @@ def fetch_bybit(): last_minute = math.floor(current_unixtime / 60) last_minute_unixtime = str(last_minute * 60 - 300) query_url = ''.join([base_url, last_minute_unixtime]) - print(query_url) response = requests.get(query_url) if response.status_code == 200: # check if the request was successful @@ -189,7 +189,6 @@ def fetch_bybit(): 'trades': 0 } bybit_json = json.dumps(candle_stick_data, indent=2) - print(bybit_json) return bybit_json else: print(f"Error fetching data from Bybit API: {response.status_code}") @@ -236,17 +235,23 @@ def get_data(): 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() + rows = db.execute("SELECT exchange, timestamp, open, high, low, close FROM ohlc WHERE timestamp > ? ORDER BY timestamp", (query_timestamp,)).fetchall() else: - rows = db.execute('SELECT exchange, timestamp, open, high, low, close FROM ohlc').fetchall() + rows = db.execute('SELECT exchange, timestamp, open, high, low, close FROM ohlc ORDER BY timestamp').fetchall() query_timestamp = 0 - database_lock.release() + data = { "timestamp": time.time(), "rows": rows } + # make sha256 checksum and append it to the data object + data_shasum = sha256(json.dumps(data).encode('utf-8')).hexdigest() + updated_data = {"shasum": data_shasum} + updated_data.update(data) + data = updated_data + if query_pretty: response = json.dumps(data, indent=2, separators=(';\n', ' :')) else: