Phoenixak99 commited on
Commit
8b63294
·
verified ·
1 Parent(s): 0e21a10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -25
app.py CHANGED
@@ -23,55 +23,69 @@ from wordpress_xmlrpc import Client
23
  from wordpress_xmlrpc.compat import xmlrpc_client
24
  from wordpress_xmlrpc.methods import media
25
 
26
- def get_user_id():
27
- url = 'https://songlabai.com/wp-json/custom-api/v1/current-user-id-session'
28
- try:
29
- response = requests.get(url)
30
- if response.status_code == 200:
31
- data = response.json()
32
- if 'user_id' in data:
33
- return data['user_id']
34
- else:
35
- return None
36
- else:
37
- return None
38
- except Exception as e:
39
- print(f"Error getting user ID: {e}")
40
- return None
 
 
 
 
 
 
 
 
41
 
42
- def get_subscription_info(user_id):
43
- url = f'https://songlabai.com/wp-json/custom-api/subscription/{user_id}'
 
44
  try:
45
- response = requests.get(url)
46
  if response.status_code == 200:
47
  data = response.json()
48
  return data
49
  else:
 
50
  return None
51
  except Exception as e:
52
- print(f"Error getting subscription info: {e}")
53
  return None
54
 
55
- def update_user_limit(user_id):
56
- url = f'https://songlabai.com/wp-json/custom-api/update-limit/{user_id}'
 
57
  try:
58
- response = requests.post(url)
59
  if response.status_code == 200:
60
  data = response.json()
61
  if data.get('success') == True:
62
  print("User limit updated successfully.")
63
  return True
64
  else:
65
- print(f"Failed to update user limit: {data}")
66
  return False
67
  else:
68
- print(f"Failed to update user limit, status code: {response.status_code}")
69
  return False
70
  except Exception as e:
71
- print(f"Error updating user limit: {e}")
72
  return False
73
 
74
 
 
 
 
75
  # Try to get API_URL from environment variables, if not found set to a default value
76
  try:
77
  API_URL = os.environ["API_URL"]
@@ -350,6 +364,46 @@ def generate_audio(genre, energy_level, tempo, description, duration):
350
  )
351
  return
352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353
  # After successful response (after retries if necessary), perform API checks
354
 
355
  # Get user ID
 
23
  from wordpress_xmlrpc.compat import xmlrpc_client
24
  from wordpress_xmlrpc.methods import media
25
 
26
+ # Define the same secret key used in WordPress
27
+ SECRET_KEY = '722C3AFB1F60816066AFB6045098E7016D98D1FCD5AFF4D3DE132CC1C6A141C9' # Must match the key used in WordPress
28
+
29
+ def get_user_info_from_jwt():
30
+ # Get the token from the URL parameters
31
+ query_params = st.experimental_get_query_params()
32
+ token = query_params.get('token', [None])[0]
33
+
34
+ if token:
35
+ try:
36
+ # Decode the token
37
+ payload = jwt.decode(token, SECRET_KEY, algorithms=['HS256'])
38
+ user_id = payload['data']['user']['id']
39
+ user_email = payload['data']['user']['email']
40
+ username = payload['data']['user']['username']
41
+ return {'user_id': user_id, 'email': user_email, 'username': username, 'token': token}
42
+ except ExpiredSignatureError:
43
+ st.error("Session expired. Please refresh the page and log in again.")
44
+ except InvalidTokenError:
45
+ st.error("Invalid authentication token. Please log in again.")
46
+ else:
47
+ st.error("No authentication token found. Please log in.")
48
+ return None
49
 
50
+ def get_subscription_info(token):
51
+ url = 'https://songlabai.com/wp-json/custom-api/subscription'
52
+ headers = {'Authorization': f'Bearer {token}'}
53
  try:
54
+ response = requests.get(url, headers=headers)
55
  if response.status_code == 200:
56
  data = response.json()
57
  return data
58
  else:
59
+ st.error(f"Failed to get subscription info. Status code: {response.status_code}")
60
  return None
61
  except Exception as e:
62
+ st.error(f"Error getting subscription info: {e}")
63
  return None
64
 
65
+ def update_user_limit(token):
66
+ url = 'https://songlabai.com/wp-json/custom-api/update-limit'
67
+ headers = {'Authorization': f'Bearer {token}'}
68
  try:
69
+ response = requests.post(url, headers=headers)
70
  if response.status_code == 200:
71
  data = response.json()
72
  if data.get('success') == True:
73
  print("User limit updated successfully.")
74
  return True
75
  else:
76
+ st.error(f"Failed to update user limit: {data}")
77
  return False
78
  else:
79
+ st.error(f"Failed to update user limit, status code: {response.status_code}")
80
  return False
81
  except Exception as e:
82
+ st.error(f"Error updating user limit: {e}")
83
  return False
84
 
85
 
86
+
87
+
88
+
89
  # Try to get API_URL from environment variables, if not found set to a default value
90
  try:
91
  API_URL = os.environ["API_URL"]
 
364
  )
365
  return
366
 
367
+ # After successful response, perform API checks
368
+
369
+ # Get user info from JWT
370
+ user_info = get_user_info_from_jwt()
371
+ if user_info is None:
372
+ st.stop()
373
+ else:
374
+ user_id = user_info['user_id']
375
+ token = user_info['token']
376
+
377
+ # Check subscription via API
378
+ subscription_info = get_subscription_info(token)
379
+ if not subscription_info:
380
+ st.error("Could not retrieve subscription information.")
381
+ return
382
+
383
+ subscription_plan_id = subscription_info['subscription_plan_id']
384
+ has_exceeded_daily_limit = subscription_info['has_exceeded_daily_limit']
385
+
386
+ if subscription_plan_id == '576':
387
+ if has_exceeded_daily_limit == True:
388
+ st.error("You have reached your daily limit for audio generation.")
389
+ return
390
+ else:
391
+ # Allow the user to proceed
392
+ placeholder1.success(f"✔️ Audio Generated, Loading...")
393
+ load_and_play_generated_audio(response)
394
+ placeholder1.empty()
395
+ # After generating audio, update the limit via API
396
+ update_user_limit(token)
397
+ elif subscription_plan_id in ['304', '305', '306']:
398
+ # Unlimited generation
399
+ placeholder1.success(f"✔️ Audio Generated, Loading...")
400
+ load_and_play_generated_audio(response)
401
+ placeholder1.empty()
402
+ else:
403
+ st.error("Your subscription does not allow audio generation.")
404
+ return
405
+
406
+
407
  # After successful response (after retries if necessary), perform API checks
408
 
409
  # Get user ID