baseddata.io/backend/app.py

173 lines
5.3 KiB
Python
Raw Normal View History

2024-08-14 19:48:58 +01:00
from flask import Flask, g, jsonify, request, json, Response, send_from_directory, abort
2024-08-01 14:06:16 +01:00
from flask_cors import CORS
import orjson, os
2024-08-01 14:06:16 +01:00
2024-08-14 19:48:58 +01:00
import datetime
import time
2024-08-01 14:06:16 +01:00
app = Flask(__name__)
CORS(app)
FILES_DIRECTORY = "../data/"
2024-08-14 19:48:58 +01:00
@app.before_request
def start_timer():
g.start = time.time()
2024-08-14 19:48:58 +01:00
@app.after_request
def log(response):
now = time.time()
duration = round(now - g.start, 4)
dt = datetime.datetime.fromtimestamp(now).strftime("%Y-%m-%d %H:%M:%S")
2024-08-14 19:48:58 +01:00
log_entry = {
"timestamp": dt,
"duration": duration,
"method": request.method,
"url": request.url,
"status": response.status_code,
"remote_addr": request.access_route[-1],
"user_agent": request.user_agent.string,
2024-08-14 19:48:58 +01:00
}
log_line = ",".join(f"{key}={value}" for key, value in log_entry.items())
2024-08-14 19:48:58 +01:00
with open("api_logs.txt", "a") as f:
f.write(log_line + "\n")
2024-08-14 19:48:58 +01:00
return response
@app.route("/bitcoin_business_growth_by_country", methods=["GET"])
2024-08-01 14:06:16 +01:00
def business_growth():
today = datetime.datetime.today()
2024-08-01 14:06:16 +01:00
# 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")
2024-08-01 14:06:16 +01:00
# Open json locally
with open("../data/final__bitcoin_business_growth_by_country.json", "rb") as f:
2024-08-01 14:06:16 +01:00
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
]
2024-08-01 14:06:16 +01:00
else:
filtered_data = data
2024-08-01 14:06:16 +01:00
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
]
2024-08-14 19:48:58 +01:00
if cumulative_period_type == "1 day":
delta = today - datetime.timedelta(days=2)
filtered_data = [
item
for item in filtered_data
if item["cumulative_period_type"] == cumulative_period_type
and delta <= datetime.datetime.strptime(item["date"], "%Y-%m-%d")
]
2024-08-14 19:48:58 +01:00
elif cumulative_period_type == "7 day":
2024-08-15 15:00:44 +01:00
delta = today - datetime.timedelta(days=8)
filtered_data = [
item
for item in filtered_data
if item["cumulative_period_type"] == cumulative_period_type
and delta <= datetime.datetime.strptime(item["date"], "%Y-%m-%d")
]
2024-08-14 19:48:58 +01:00
elif cumulative_period_type == "28 day":
2024-08-15 15:00:44 +01:00
delta = today - datetime.timedelta(days=29)
filtered_data = [
item
for item in filtered_data
if item["cumulative_period_type"] == cumulative_period_type
and delta <= datetime.datetime.strptime(item["date"], "%Y-%m-%d")
]
2024-08-14 19:48:58 +01:00
elif cumulative_period_type == "365 day":
2024-08-15 15:00:44 +01:00
delta = today - datetime.timedelta(days=366)
filtered_data = [
item
for item in filtered_data
if item["cumulative_period_type"] == cumulative_period_type
and delta <= datetime.datetime.strptime(item["date"], "%Y-%m-%d")
]
2024-08-01 14:06:16 +01:00
# Sort by date
sorted_data = sorted(filtered_data, key=lambda x: x["date"], reverse=False)
2024-08-01 14:06:16 +01:00
# Return json
return Response(json.dumps(sorted_data), mimetype="application/json")
2024-08-01 14:06:16 +01:00
@app.route("/get_json/<filename>", methods=["GET"])
def get_json(filename):
2024-08-14 19:48:58 +01:00
period = request.args.get("period")
today = datetime.datetime.today()
2024-08-14 19:48:58 +01:00
file_path = os.path.join(FILES_DIRECTORY, filename)
if not os.path.isfile(file_path):
abort(404)
2024-08-01 14:06:16 +01:00
with open(file_path, "r") as f:
2024-08-14 19:48:58 +01:00
data = orjson.loads(f.read())
if period == "last 7 days":
delta = today - datetime.timedelta(days=7)
filtered_data = [
item
for item in data
if delta <= datetime.datetime.strptime(item["date"], "%Y-%m-%d") <= today
]
sorted_data = sorted(filtered_data, key=lambda x: x["date"])
2024-08-14 19:48:58 +01:00
elif period == "last 28 days":
delta = today - datetime.timedelta(days=28)
filtered_data = [
item
for item in data
if delta <= datetime.datetime.strptime(item["date"], "%Y-%m-%d") <= today
]
sorted_data = sorted(filtered_data, key=lambda x: x["date"])
2024-08-14 19:48:58 +01:00
elif period == "last 365 days":
delta = today - datetime.timedelta(days=365)
filtered_data = [
item
for item in data
if delta <= datetime.datetime.strptime(item["date"], "%Y-%m-%d") <= today
]
sorted_data = sorted(filtered_data, key=lambda x: x["date"])
2024-08-14 19:48:58 +01:00
elif period == "last 2 years":
delta = today - datetime.timedelta(days=730)
filtered_data = [
item
for item in data
if delta <= datetime.datetime.strptime(item["date"], "%Y-%m-%d") <= today
]
sorted_data = sorted(filtered_data, key=lambda x: x["date"])
2024-08-14 19:48:58 +01:00
else:
sorted_data = sorted(data, key=lambda x: x["date"])
2024-08-01 14:06:16 +01:00
2024-08-14 19:48:58 +01:00
return jsonify(sorted_data)
2024-08-01 14:06:16 +01:00
@app.route("/download/<filename>", methods=["GET"])
def download_file(filename):
try:
return send_from_directory(FILES_DIRECTORY, filename, as_attachment=True)
except FileNotFoundError:
abort(404)
2024-08-01 14:06:16 +01:00
if __name__ == "__main__":
2024-08-01 14:06:16 +01:00
app.run()