chatgpt/letters/openGPT-flask-api.py

38 lines
1.1 KiB
Python

from flask import Flask, request
import sqlite3
# Create a Flask app and set up the API endpoint
app = Flask(__name__)
@app.route('/dump_db', methods=['GET'])
def dump_db():
# Connect to the SQLite database
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
# Retrieve the data from the database
cursor.execute('SELECT * FROM price_data')
rows = cursor.fetchall()
# Format the data as a JSON document
data = {
'timestamp': [row[0] for row in rows],
'value': [row[1] for row in rows],
'symbol': [row[2] for row in rows],
'source': [row[3] for row in rows],
}
# Check if a timestamp was provided in the request
timestamp = request.args.get('timestamp')
if timestamp:
# Filter the data to only include rows with a timestamp greater than or equal to the provided value
data = {k: [v for i, v in enumerate(v) if data['timestamp'][i] >= timestamp] for k, v in data.items()}
# Return the data as a JSON response
return data
# Run the app
if __name__ == '__main__':
app.run()