baseddata.io/backend/app.py

81 lines
2.4 KiB
Python
Raw Normal View History

2024-08-01 14:06:16 +01:00
from flask import Flask, request, json, Response
from flask_cors import CORS
import orjson
app = Flask(__name__)
CORS(app)
@app.route('/bitcoin_business_growth_by_country', methods=['GET'])
def business_growth():
# Parse args from request
latest_date = request.args.get('latest_date')
country_names = request.args.get('countries') # change this line
cumulative_period_type = request.args.get('cumulative_period_type')
# Open json locally
with open('../data/final__bitcoin_business_growth_by_country.json', 'rb') as f:
data = orjson.loads(f.read())
# Filter based on args
if latest_date:
latest_date_bool = latest_date == 'true'
filtered_data = [item for item in data if item['latest_date'] == latest_date_bool]
else:
filtered_data = data
if country_names:
countries = [name.strip() for name in country_names.split(",")]
filtered_data = [item for item in filtered_data if item['country_name'] in countries]
if cumulative_period_type:
filtered_data = [item for item in filtered_data if item['cumulative_period_type'] == cumulative_period_type]
# Sort by date
sorted_data = sorted(filtered_data, key=lambda x: x['date'], reverse=True)
# Return json
return Response(json.dumps(sorted_data), mimetype='application/json')
@app.route('/price', methods=['GET'])
def price():
# Open json locally
with open('../data/final__price.json', 'rb') as f:
data = orjson.loads(f.read())
# Return json
return Response(json.dumps(data), mimetype='application/json')
@app.route('/miner_rewards', methods=['GET'])
def miner_rewards():
# Open json locally
with open('../data/final__miner_rewards.json', 'rb') as f:
data = orjson.loads(f.read())
# Return json
return Response(json.dumps(data), mimetype='application/json')
@app.route('/hashrate', methods=['GET'])
def hashrate():
# Open json locally
with open('../data/dev/final__hashrate.json', 'rb') as f:
data = orjson.loads(f.read())
# Return json
return Response(json.dumps(data), mimetype='application/json')
@app.route('/feerates', methods=['GET'])
def feerates():
# Open json locally
with open('../data/final__feerate_percentiles.json', 'rb') as f:
data = orjson.loads(f.read())
# Return json
return Response(json.dumps(data), mimetype='application/json')
if __name__ == '__main__':
app.run()