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)