Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,29 @@
|
|
| 1 |
import requests
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
from datetime import datetime
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
# PythonAnywhere API endpoint
|
| 8 |
PYTHONANYWHERE_URL = "https://omarnuwara.pythonanywhere.com/get-response"
|
| 9 |
|
| 10 |
-
# GitHub API and file information
|
| 11 |
-
GITHUB_TOKEN = "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" # Store
|
| 12 |
REPO_OWNER = "omarnuwrar"
|
| 13 |
REPO_NAME = "api"
|
| 14 |
FILE_PATH = "user.json"
|
| 15 |
|
| 16 |
-
|
| 17 |
def fetch_user_data():
|
|
|
|
| 18 |
url = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/contents/{FILE_PATH}"
|
| 19 |
headers = {
|
| 20 |
"Authorization": f"token {GITHUB_TOKEN}",
|
| 21 |
"Accept": "application/vnd.github.v3+json",
|
| 22 |
}
|
|
|
|
| 23 |
response = requests.get(url, headers=headers)
|
| 24 |
if response.status_code == 200:
|
| 25 |
content = response.json()
|
|
@@ -27,49 +31,65 @@ def fetch_user_data():
|
|
| 27 |
file_decoded = base64.b64decode(file_content).decode("utf-8")
|
| 28 |
return json.loads(file_decoded)
|
| 29 |
else:
|
|
|
|
|
|
|
| 30 |
return None
|
| 31 |
|
| 32 |
-
|
| 33 |
-
def
|
|
|
|
|
|
|
|
|
|
| 34 |
user_data = fetch_user_data()
|
| 35 |
if not user_data:
|
| 36 |
return False, "Unable to fetch user data."
|
| 37 |
|
| 38 |
for user in user_data:
|
| 39 |
if user["token"] == token:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
@app.route('/chat', methods=['POST'])
|
| 48 |
def chat():
|
|
|
|
|
|
|
|
|
|
| 49 |
try:
|
| 50 |
-
# Ensure the request contains JSON
|
| 51 |
if not request.is_json:
|
| 52 |
-
return jsonify({"error": "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
token = data.get("token")
|
| 57 |
|
| 58 |
-
if not
|
| 59 |
-
return jsonify({"error": "
|
| 60 |
|
| 61 |
# Validate the token
|
| 62 |
-
is_valid,
|
| 63 |
if not is_valid:
|
| 64 |
-
return jsonify({"error":
|
| 65 |
|
| 66 |
-
# Forward the request to PythonAnywhere
|
| 67 |
-
|
| 68 |
-
response = requests.post(PYTHONANYWHERE_URL, json=
|
| 69 |
|
| 70 |
if response.status_code == 200:
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
else:
|
| 74 |
return jsonify({
|
| 75 |
"error": "Error from PythonAnywhere",
|
|
@@ -77,7 +97,29 @@ def chat():
|
|
| 77 |
}), response.status_code
|
| 78 |
|
| 79 |
except Exception as e:
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
return jsonify({"error": "An error occurred", "details": str(e)}), 500
|
| 82 |
|
| 83 |
|
|
|
|
| 1 |
import requests
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
from datetime import datetime
|
| 4 |
+
import base64
|
| 5 |
+
import json
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
# PythonAnywhere API endpoint
|
| 10 |
PYTHONANYWHERE_URL = "https://omarnuwara.pythonanywhere.com/get-response"
|
| 11 |
|
| 12 |
+
# GitHub API and file information
|
| 13 |
+
GITHUB_TOKEN = "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" # Store this in .env in production
|
| 14 |
REPO_OWNER = "omarnuwrar"
|
| 15 |
REPO_NAME = "api"
|
| 16 |
FILE_PATH = "user.json"
|
| 17 |
|
| 18 |
+
|
| 19 |
def fetch_user_data():
|
| 20 |
+
"""Fetch user data from GitHub repo."""
|
| 21 |
url = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/contents/{FILE_PATH}"
|
| 22 |
headers = {
|
| 23 |
"Authorization": f"token {GITHUB_TOKEN}",
|
| 24 |
"Accept": "application/vnd.github.v3+json",
|
| 25 |
}
|
| 26 |
+
|
| 27 |
response = requests.get(url, headers=headers)
|
| 28 |
if response.status_code == 200:
|
| 29 |
content = response.json()
|
|
|
|
| 31 |
file_decoded = base64.b64decode(file_content).decode("utf-8")
|
| 32 |
return json.loads(file_decoded)
|
| 33 |
else:
|
| 34 |
+
print(f"Failed to fetch file: {response.status_code}")
|
| 35 |
+
print(response.json())
|
| 36 |
return None
|
| 37 |
|
| 38 |
+
|
| 39 |
+
def is_token_valid(token):
|
| 40 |
+
"""
|
| 41 |
+
Check if the provided token is valid and unexpired.
|
| 42 |
+
"""
|
| 43 |
user_data = fetch_user_data()
|
| 44 |
if not user_data:
|
| 45 |
return False, "Unable to fetch user data."
|
| 46 |
|
| 47 |
for user in user_data:
|
| 48 |
if user["token"] == token:
|
| 49 |
+
# Parse token expiration time
|
| 50 |
+
expiration_time = datetime.fromisoformat(user["token_expiration_time"])
|
| 51 |
+
if datetime.now() <= expiration_time:
|
| 52 |
+
return True, None
|
| 53 |
+
|
| 54 |
+
return False, "Your token has expired. Please subscribe to continue."
|
| 55 |
+
|
| 56 |
+
return False, "Invalid token. Please log in or create an account."
|
| 57 |
+
|
| 58 |
|
| 59 |
@app.route('/chat', methods=['POST'])
|
| 60 |
def chat():
|
| 61 |
+
"""
|
| 62 |
+
Chat endpoint. Requires a valid token.
|
| 63 |
+
"""
|
| 64 |
try:
|
| 65 |
+
# Ensure the request contains JSON
|
| 66 |
if not request.is_json:
|
| 67 |
+
return jsonify({"error": "Invalid request format. JSON expected."}), 400
|
| 68 |
+
|
| 69 |
+
# Extract message and token from the request body
|
| 70 |
+
user_input = request.json.get("message", "")
|
| 71 |
+
user_token = request.json.get("token", "")
|
| 72 |
|
| 73 |
+
if not user_input:
|
| 74 |
+
return jsonify({"error": "No message provided."}), 400
|
|
|
|
| 75 |
|
| 76 |
+
if not user_token:
|
| 77 |
+
return jsonify({"error": "No token provided."}), 400
|
| 78 |
|
| 79 |
# Validate the token
|
| 80 |
+
is_valid, error_message = is_token_valid(user_token)
|
| 81 |
if not is_valid:
|
| 82 |
+
return jsonify({"error": error_message}), 403
|
| 83 |
|
| 84 |
+
# Forward the request to PythonAnywhere
|
| 85 |
+
data = {"message": user_input}
|
| 86 |
+
response = requests.post(PYTHONANYWHERE_URL, json=data)
|
| 87 |
|
| 88 |
if response.status_code == 200:
|
| 89 |
+
response_json = response.json()
|
| 90 |
+
ai_response = response_json.get("response", "No 'response' key found in the JSON.")
|
| 91 |
+
ai_response = ai_response.encode('latin1').decode('utf-8', 'ignore')
|
| 92 |
+
return jsonify({"response": ai_response})
|
| 93 |
else:
|
| 94 |
return jsonify({
|
| 95 |
"error": "Error from PythonAnywhere",
|
|
|
|
| 97 |
}), response.status_code
|
| 98 |
|
| 99 |
except Exception as e:
|
| 100 |
+
return jsonify({"error": "An error occurred", "details": str(e)}), 500
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
@app.route('/get-payload', methods=['GET'])
|
| 104 |
+
def get_payload():
|
| 105 |
+
"""
|
| 106 |
+
Get payload from the request body. Requires a valid token.
|
| 107 |
+
"""
|
| 108 |
+
try:
|
| 109 |
+
# Extract token from query parameters
|
| 110 |
+
user_token = request.args.get("token", "")
|
| 111 |
+
|
| 112 |
+
if not user_token:
|
| 113 |
+
return jsonify({"error": "No token provided."}), 400
|
| 114 |
+
|
| 115 |
+
# Validate the token
|
| 116 |
+
is_valid, error_message = is_token_valid(user_token)
|
| 117 |
+
if not is_valid:
|
| 118 |
+
return jsonify({"error": error_message}), 403
|
| 119 |
+
|
| 120 |
+
return jsonify({"payload": "This is a demo payload response."})
|
| 121 |
+
|
| 122 |
+
except Exception as e:
|
| 123 |
return jsonify({"error": "An error occurred", "details": str(e)}), 500
|
| 124 |
|
| 125 |
|