Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,71 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
-
import numpy as np
|
| 3 |
-
import random
|
| 4 |
import time
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
"
|
| 11 |
-
# Sample user data structure
|
| 12 |
-
# {
|
| 13 |
-
# "id": 1,
|
| 14 |
-
# "timestamp": int(time.time()),
|
| 15 |
-
# "udid": "sample_udid_123",
|
| 16 |
-
# "platform": "android",
|
| 17 |
-
# "credit": 100
|
| 18 |
-
# }
|
| 19 |
-
]
|
| 20 |
-
}
|
| 21 |
-
|
| 22 |
-
# Account model structure
|
| 23 |
-
account_model = {
|
| 24 |
-
"id": None, # int (auto-incremented)
|
| 25 |
-
"timestamp": int(time.time()), # int (current timestamp)
|
| 26 |
-
"udid": None, # string
|
| 27 |
-
"platform": None, # string
|
| 28 |
-
"credit": 100 # int (default 100)
|
| 29 |
}
|
| 30 |
|
| 31 |
# Helper function to generate a new ID
|
| 32 |
def generate_new_id():
|
| 33 |
-
if not
|
| 34 |
return 1
|
| 35 |
-
return max(
|
| 36 |
|
| 37 |
-
#
|
| 38 |
@app.route('/')
|
| 39 |
def home():
|
| 40 |
return "stablediffusionapi test"
|
| 41 |
|
| 42 |
-
# GET
|
| 43 |
-
@app.route('/
|
| 44 |
-
def
|
| 45 |
-
return jsonify({"
|
| 46 |
|
| 47 |
-
# GET
|
| 48 |
-
@app.route('/
|
| 49 |
-
def
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
if
|
| 54 |
-
#
|
| 55 |
return jsonify({
|
| 56 |
-
"id":
|
| 57 |
-
"timestamp":
|
| 58 |
-
"udid":
|
| 59 |
-
"platform":
|
| 60 |
-
"credit":
|
| 61 |
})
|
| 62 |
else:
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
"id": generate_new_id(),
|
| 66 |
"timestamp": int(time.time()),
|
| 67 |
"udid": udid,
|
| 68 |
-
"platform":
|
| 69 |
"credit": 100 # Default credit
|
| 70 |
}
|
| 71 |
|
| 72 |
# Add to database
|
| 73 |
-
|
| 74 |
|
| 75 |
-
# Return the newly created
|
| 76 |
return jsonify({
|
| 77 |
-
"id":
|
| 78 |
-
"timestamp":
|
| 79 |
-
"udid":
|
| 80 |
-
"platform":
|
| 81 |
-
"credit":
|
| 82 |
}), 201 # 201 Created status code
|
| 83 |
|
| 84 |
if __name__ == '__main__':
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
|
|
|
|
|
|
| 2 |
import time
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
| 6 |
+
# Fake database simulation - more generic name
|
| 7 |
+
fake_db = {
|
| 8 |
+
"accounts": [] # Changed from "users" to "accounts" to better reflect the data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
}
|
| 10 |
|
| 11 |
# Helper function to generate a new ID
|
| 12 |
def generate_new_id():
|
| 13 |
+
if not fake_db["accounts"]:
|
| 14 |
return 1
|
| 15 |
+
return max(account["id"] for account in fake_db["accounts"]) + 1
|
| 16 |
|
| 17 |
+
# Home page
|
| 18 |
@app.route('/')
|
| 19 |
def home():
|
| 20 |
return "stablediffusionapi test"
|
| 21 |
|
| 22 |
+
# GET all accounts
|
| 23 |
+
@app.route('/accounts', methods=['GET'])
|
| 24 |
+
def get_all_accounts():
|
| 25 |
+
return jsonify({"accounts": fake_db["accounts"]})
|
| 26 |
|
| 27 |
+
# GET or CREATE account by platform and UDID
|
| 28 |
+
@app.route('/account/<platform>/<udid>', methods=['GET'])
|
| 29 |
+
def get_or_create_account(platform, udid):
|
| 30 |
+
# Validate platform (should be "IOS" or "AND")
|
| 31 |
+
platform = platform.upper()
|
| 32 |
+
if platform not in ["IOS", "AND"]:
|
| 33 |
+
return jsonify({
|
| 34 |
+
"error": "Invalid platform. Use 'IOS' or 'AND'."
|
| 35 |
+
}), 400
|
| 36 |
+
|
| 37 |
+
# Search for account in the database
|
| 38 |
+
account = next((acc for acc in fake_db["accounts"] if acc["udid"] == udid), None)
|
| 39 |
|
| 40 |
+
if account:
|
| 41 |
+
# Account found, return the account data
|
| 42 |
return jsonify({
|
| 43 |
+
"id": account["id"],
|
| 44 |
+
"timestamp": account["timestamp"],
|
| 45 |
+
"udid": account["udid"],
|
| 46 |
+
"platform": account["platform"],
|
| 47 |
+
"credit": account["credit"]
|
| 48 |
})
|
| 49 |
else:
|
| 50 |
+
# Account not found, create new account with 100 credits
|
| 51 |
+
new_account = {
|
| 52 |
"id": generate_new_id(),
|
| 53 |
"timestamp": int(time.time()),
|
| 54 |
"udid": udid,
|
| 55 |
+
"platform": platform,
|
| 56 |
"credit": 100 # Default credit
|
| 57 |
}
|
| 58 |
|
| 59 |
# Add to database
|
| 60 |
+
fake_db["accounts"].append(new_account)
|
| 61 |
|
| 62 |
+
# Return the newly created account
|
| 63 |
return jsonify({
|
| 64 |
+
"id": new_account["id"],
|
| 65 |
+
"timestamp": new_account["timestamp"],
|
| 66 |
+
"udid": new_account["udid"],
|
| 67 |
+
"platform": new_account["platform"],
|
| 68 |
+
"credit": new_account["credit"]
|
| 69 |
}), 201 # 201 Created status code
|
| 70 |
|
| 71 |
if __name__ == '__main__':
|