yusufgundogdu commited on
Commit
15a3e96
·
verified ·
1 Parent(s): 2b01ca4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -52
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
- # 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():
40
  return "stablediffusionapi test"
41
 
42
- # GET Endpoint'leri
43
- @app.route('/users', methods=['GET'])
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__':
 
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__':