File size: 2,124 Bytes
f572332 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
from flask import Flask, render_template, jsonify
import requests
import json
app = Flask(__name__)
API_URL = "https://badimo.nyc3.digitaloceanspaces.com/trade/frequency/snapshot/month/latest.json"
@app.route('/')
def index():
return render_template('index.html')
@app.route('/api/data')
def get_data():
try:
response = requests.get(API_URL)
response.raise_for_status()
data = response.json()
# Add additional derived metrics
for item in data:
item['RarityScore'] = round(item['TimesTraded'] / item['UniqueCirculation'] if item['UniqueCirculation'] > 0 else 0, 2)
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/stats')
def get_stats():
try:
response = requests.get(API_URL)
response.raise_for_status()
data = response.json()
# Generate stats by type
stats_by_type = {}
for item in data:
item_type = item['Type']
if item_type not in stats_by_type:
stats_by_type[item_type] = {
'count': 0,
'totalTraded': 0,
'totalCirculation': 0,
'averageDemand': 0
}
stats_by_type[item_type]['count'] += 1
stats_by_type[item_type]['totalTraded'] += item['TimesTraded']
stats_by_type[item_type]['totalCirculation'] += item['UniqueCirculation']
# Calculate averages
for type_key in stats_by_type:
if stats_by_type[type_key]['totalCirculation'] > 0:
stats_by_type[type_key]['averageDemand'] = round(
stats_by_type[type_key]['totalTraded'] / stats_by_type[type_key]['totalCirculation'], 2
)
return jsonify(stats_by_type)
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=True)
|