Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384from flask import Flask, request, jsonifyimport numpy as npimport randomimport timeapp = Flask(__name__)# Sample database simulationshared_data = {"users": [# Sample user data structure# {# "id": 1,# "timestamp": int(time.time()),# "udid": "sample_udid_123",# "platform": "android",# "credit": 100# }]}# Account model structureaccount_model = {"id": None, # int (auto-incremented)"timestamp": int(time.time()), # int (current timestamp)"udid": None, # string"platform": None, # string"credit": 100 # int (default 100)}# Helper function to generate a new IDdef generate_new_id():if not shared_data["users"]:return 1return max(user["id"] for user in shared_data["users"]) + 1# Ana sayfa@app.route('/')def home():return "stablediffusionapi test"# GET Endpoint'leri@app.route('/users', methods=['GET'])def get_users():return jsonify({"users": shared_data["users"]})# GET user by UDID@app.route('/user/<udid>', methods=['GET'])def get_user_by_udid(udid):# Search for user in the databaseuser = next((u for u in shared_data["users"] if u["udid"] == udid), None)if user:# User found, return the user datareturn jsonify({"id": user["id"],"timestamp": user["timestamp"],"udid": user["udid"],"platform": user["platform"],"credit": user["credit"]})else:# User not found, create new user with 100 creditsnew_user = {"id": generate_new_id(),"timestamp": int(time.time()),"udid": udid,"platform": request.headers.get('User-Agent', 'unknown'), # Get platform from User-Agent"credit": 100 # Default credit}# Add to databaseshared_data["users"].append(new_user)# Return the newly created userreturn jsonify({"id": new_user["id"],"timestamp": new_user["timestamp"],"udid": new_user["udid"],"platform": new_user["platform"],"credit": new_user["credit"]}), 201 # 201 Created status codeif __name__ == '__main__':1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071from flask import Flask, request, jsonifyimport timeapp = Flask(__name__)# Fake database simulation - more generic namefake_db = {"accounts": [] # Changed from "users" to "accounts" to better reflect the data}# Helper function to generate a new IDdef generate_new_id():if not fake_db["accounts"]:return 1return max(account["id"] for account in fake_db["accounts"]) + 1# Home page@app.route('/')def home():return "stablediffusionapi test"# GET all accounts@app.route('/accounts', methods=['GET'])def get_all_accounts():return jsonify({"accounts": fake_db["accounts"]})# GET or CREATE account by platform and UDID@app.route('/account/<platform>/<udid>', methods=['GET'])def get_or_create_account(platform, udid):# Validate platform (should be "IOS" or "AND")platform = platform.upper()if platform not in ["IOS", "AND"]:return jsonify({"error": "Invalid platform. Use 'IOS' or 'AND'."}), 400# Search for account in the databaseaccount = next((acc for acc in fake_db["accounts"] if acc["udid"] == udid), None)if account:# Account found, return the account datareturn jsonify({"id": account["id"],"timestamp": account["timestamp"],"udid": account["udid"],"platform": account["platform"],"credit": account["credit"]})else:# Account not found, create new account with 100 creditsnew_account = {"id": generate_new_id(),"timestamp": int(time.time()),"udid": udid,"platform": platform,"credit": 100 # Default credit}# Add to databasefake_db["accounts"].append(new_account)# Return the newly created accountreturn jsonify({"id": new_account["id"],"timestamp": new_account["timestamp"],"udid": new_account["udid"],"platform": new_account["platform"],"credit": new_account["credit"]}), 201 # 201 Created status codeif __name__ == '__main__':
app.py+39-52