Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,39 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import numpy as np
|
| 3 |
import random
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
-
#
|
| 8 |
shared_data = {
|
| 9 |
"users": [
|
| 10 |
-
|
| 11 |
-
{
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
}
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Ana sayfa
|
| 17 |
@app.route('/')
|
| 18 |
def home():
|
|
@@ -23,40 +44,42 @@ def home():
|
|
| 23 |
def get_users():
|
| 24 |
return jsonify({"users": shared_data["users"]})
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
return jsonify({"error": str(e)}), 500
|
| 44 |
-
|
| 45 |
-
@app.route('/add-user', methods=['POST'])
|
| 46 |
-
def add_user():
|
| 47 |
-
try:
|
| 48 |
-
if not request.json or "name" not in request.json:
|
| 49 |
-
return jsonify({"error": "Name is required"}), 400
|
| 50 |
-
|
| 51 |
new_user = {
|
| 52 |
-
"id":
|
| 53 |
-
"
|
| 54 |
-
"
|
|
|
|
|
|
|
| 55 |
}
|
|
|
|
|
|
|
| 56 |
shared_data["users"].append(new_user)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
if __name__ == '__main__':
|
| 62 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 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 |
+
# Sample database simulation
|
| 9 |
shared_data = {
|
| 10 |
"users": [
|
| 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 shared_data["users"]:
|
| 34 |
+
return 1
|
| 35 |
+
return max(user["id"] for user in shared_data["users"]) + 1
|
| 36 |
+
|
| 37 |
# Ana sayfa
|
| 38 |
@app.route('/')
|
| 39 |
def home():
|
|
|
|
| 44 |
def get_users():
|
| 45 |
return jsonify({"users": shared_data["users"]})
|
| 46 |
|
| 47 |
+
# GET user by UDID
|
| 48 |
+
@app.route('/user/<udid>', methods=['GET'])
|
| 49 |
+
def get_user_by_udid(udid):
|
| 50 |
+
# Search for user in the database
|
| 51 |
+
user = next((u for u in shared_data["users"] if u["udid"] == udid), None)
|
| 52 |
+
|
| 53 |
+
if user:
|
| 54 |
+
# User found, return the user data
|
| 55 |
+
return jsonify({
|
| 56 |
+
"id": user["id"],
|
| 57 |
+
"timestamp": user["timestamp"],
|
| 58 |
+
"udid": user["udid"],
|
| 59 |
+
"platform": user["platform"],
|
| 60 |
+
"credit": user["credit"]
|
| 61 |
+
})
|
| 62 |
+
else:
|
| 63 |
+
# User not found, create new user with 100 credits
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
new_user = {
|
| 65 |
+
"id": generate_new_id(),
|
| 66 |
+
"timestamp": int(time.time()),
|
| 67 |
+
"udid": udid,
|
| 68 |
+
"platform": request.headers.get('User-Agent', 'unknown'), # Get platform from User-Agent
|
| 69 |
+
"credit": 100 # Default credit
|
| 70 |
}
|
| 71 |
+
|
| 72 |
+
# Add to database
|
| 73 |
shared_data["users"].append(new_user)
|
| 74 |
+
|
| 75 |
+
# Return the newly created user
|
| 76 |
+
return jsonify({
|
| 77 |
+
"id": new_user["id"],
|
| 78 |
+
"timestamp": new_user["timestamp"],
|
| 79 |
+
"udid": new_user["udid"],
|
| 80 |
+
"platform": new_user["platform"],
|
| 81 |
+
"credit": new_user["credit"]
|
| 82 |
+
}), 201 # 201 Created status code
|
| 83 |
|
| 84 |
if __name__ == '__main__':
|
| 85 |
+
app.run(host='0.0.0.0', port=7860)
|