Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,155 +1,80 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
import requests
|
| 3 |
-
from
|
| 4 |
-
import
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
headers = {
|
| 13 |
-
"cookie": "_octo=GH1.1.1509769180.1721774314; _device_id=0038e28d4f7d4f9baf8f76b6b9fb8980; saved_user_sessions=155741452%3Avhq2fotRhM6ixLZuU8plCTagdBxOI9UGuu5XwPf569UWHPOg; user_session=vhq2fotRhM6ixLZuU8plCTagdBxOI9UGuu5XwPf569UWHPOg; __Host-user_session_same_site=vhq2fotRhM6ixLZuU8plCTagdBxOI9UGuu5XwPf569UWHPOg; logged_in=yes; dotcom_user=omarnuwrar; color_mode=%7B%22color_mode%22%3A%22auto%22%2C%22light_theme%22%3A%7B%22name%22%3A%22light%22%2C%22color_mode%22%3A%22light%22%7D%2C%22dark_theme%22%3A%7B%22name%22%3A%22dark%22%2C%22color_mode%22%3A%22dark%22%7D%7D; cpu_bucket=lg; preferred_color_mode=light; tz=Africa%2FTripoli; _gh_sess=cmT9%2BKjq2RkmHF1yGIv0U25vE58ydhCtNe9GK26HxTEojhfvyQXwn0HhD1dCMQkEI5eXOA8kQm8EmM11JtSkNlpW7yECoyPnWUDBAOkHKNovsRrJ%2FpAAGM7zmYrs3kJNQXWIKGPN1jW8Hb3%2FPyMIH74N7k1sGKXq1CTjvi%2BSPofDjCF3%2BNdgTM1G7OPwV%2Fuu%2FB1%2FOro05an8u8%2BWppVww4A0GWiczmOk8g1xlczsgc8LVTb2R5hY%2FVz1exXbVJ6TFGuRpIGs2%2BuD3%2BSWCTHVWyw7%2By1b5f3V8MU5Jmg5UWdnW5B9%2FjW17dAosAYpD1vGuGj8tgi2hLUTzuDdpwVIplBzjUpw0cWL%2BJPpXZ4LWo2H71vPQv8VgQS3WUTxUfkF4UM6Sagdk41EgK4HjS0Ebb8%2BXF3uMNXQXvVcHbGMPGlUX%2BuVmwOf0zO9Ne0y63o09yojQQ74H1oDYwluyJkbX0XqB5RYA6A3AabUTYoL%2B2gPG4maWamZ7DlLShj0bfruQ563GrZwM3tbHpFk8aeJaqWLLwQTXh2o7f5IJhj3qtgwv%2BfxbzrrpXNzhy7iuWlkFKKgQOTG1yrj1ho%2B1wec6hCzFekh%2BVmuHdlOo0opAvTM4aTydsvr0XoW9NXn9z2IGr27K6M7632bJiET2I6sXa8dAKQeWCvXRJt7HDxHayLs3NDJKYAhjOCii%2FLHja7Yksxuxvo3OL1LQw9aNAanjTYOgK5oXT5pC3Q5EKtb1NSrpEo%2ByzNgpFHEp3%2FzvtzEratIK4Jg0jWoasL2hlMpvgQQuLX3NAQhPuXKFfgG2AqkR%2BSlwVw8AVrjVJ6w4AeWVyD1fjq3GkWmKP7yELjLIYGMdNzIVxwmTysLhoFfAv%2BnpM%2B5--iM2WECPbjmZsWX3j--%2FXZDhW0%2FhViqIiMx%2FzhPiQ%3D%3D",
|
| 14 |
-
"if-none-match": 'W/"2ff86bd1792cfee5ed79ee070b3b46de"',
|
| 15 |
-
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
| 16 |
-
"x-github-target": "dotcom",
|
| 17 |
-
"x-react-router": "json",
|
| 18 |
-
"x-requested-with": "XMLHttpRequest",
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
-
response = requests.get(url, headers=headers)
|
| 22 |
-
|
| 23 |
-
if response.status_code == 200:
|
| 24 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
| 25 |
-
script_tag = soup.find("script", {"type": "application/json", "data-target": "react-app.embeddedData"})
|
| 26 |
-
|
| 27 |
-
if script_tag:
|
| 28 |
-
try:
|
| 29 |
-
json_data = json.loads(script_tag.string.strip())
|
| 30 |
-
authenticity_token = json_data["payload"]["csrf_tokens"]["/omarnuwrar/api/tree-save/main/Septimius.json"]["post"]
|
| 31 |
-
commit_oid = json_data["payload"]["webCommitInfo"]["commitOid"]
|
| 32 |
-
return authenticity_token, commit_oid
|
| 33 |
-
except (KeyError, json.JSONDecodeError) as e:
|
| 34 |
-
print(f"Error: Failed to extract data. Details: {str(e)}")
|
| 35 |
-
return None, None
|
| 36 |
-
else:
|
| 37 |
-
print("Error: Could not find the required <script> tag.")
|
| 38 |
-
return None, None
|
| 39 |
-
else:
|
| 40 |
-
print(f"Error: Failed to fetch the page. Status code: {response.status_code}")
|
| 41 |
-
return None, None
|
| 42 |
-
|
| 43 |
-
# Step 2: Send the POST request to update the user.json file
|
| 44 |
-
def update_user_json_file(authenticity_token, commit_oid, new_content):
|
| 45 |
-
url = "https://github.com/omarnuwrar/api/tree-save/main/Septimius.json"
|
| 46 |
-
|
| 47 |
-
headers = {
|
| 48 |
-
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
| 49 |
-
"x-requested-with": "XMLHttpRequest",
|
| 50 |
-
"github-verified-fetch": "true",
|
| 51 |
-
"content-type": "application/x-www-form-urlencoded",
|
| 52 |
-
"cookie": "_octo=GH1.1.1509769180.1721774314; _device_id=0038e28d4f7d4f9baf8f76b6b9fb8980; saved_user_sessions=155741452%3Avhq2fotRhM6ixLZuU8plCTagdBxOI9UGuu5XwPf569UWHPOg; user_session=vhq2fotRhM6ixLZuU8plCTagdBxOI9UGuu5XwPf569UWHPOg; __Host-user_session_same_site=vhq2fotRhM6ixLZuU8plCTagdBxOI9UGuu5XwPf569UWHPOg; logged_in=yes; dotcom_user=omarnuwrar; color_mode=%7B%22color_mode%22%3A%22auto%22%2C%22light_theme%22%3A%7B%22name%22%3A%22light%22%2C%22color_mode%22%3A%22light%22%7D%2C%22dark_theme%22%3A%7B%22name%22%3A%22dark%22%2C%22color_m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| 56 |
-
"message": "Update Septimius.json",
|
| 57 |
-
"placeholder_message": "Update Septimius.json",
|
| 58 |
-
"description": "",
|
| 59 |
-
"commit-choice": "direct",
|
| 60 |
-
"target_branch": "main",
|
| 61 |
-
"quick_pull": "",
|
| 62 |
-
"guidance_task": "",
|
| 63 |
-
"commit": commit_oid,
|
| 64 |
-
"same_repo": "1",
|
| 65 |
-
"pr": "",
|
| 66 |
-
"content_changed": "true",
|
| 67 |
-
"filename": "Septimius.json",
|
| 68 |
-
"new_filename": "Septimius.json",
|
| 69 |
-
"value": new_content,
|
| 70 |
-
"authenticity_token": authenticity_token,
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
response = requests.post(url, headers=headers, data=payload)
|
| 74 |
-
|
| 75 |
-
if response.status_code == 200:
|
| 76 |
-
return {"success": True, "message": "user.json has been updated!"}
|
| 77 |
-
else:
|
| 78 |
-
return {"success": False, "message": f"Request failed with status code {response.status_code}", "details": response.text}
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
# Function to fetch and extract the JSON data
|
| 83 |
-
def fetch_json_from_github():
|
| 84 |
-
# URL of the GitHub page
|
| 85 |
-
url = "https://github.com/omarnuwrar/api/blob/main/Septimius.json"
|
| 86 |
-
|
| 87 |
-
# Custom headers
|
| 88 |
-
headers = {
|
| 89 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
| 90 |
-
"Cookie": "_octo=GH1.1.1509769180.1721774314; _device_id=0038e28d4f7d4f9baf8f76b6b9fb8980; saved_user_sessions=155741452%3Avhq2fotRhM6ixLZuU8plCTagdBxOI9UGuu5XwPf569UWHPOg; user_session=vhq2fotRhM6ixLZuU8plCTagdBxOI9UGuu5XwPf569UWHPOg; __Host-user_session_same_site=vhq2fotRhM6ixLZuU8plCTagdBxOI9UGuu5XwPf569UWHPOg; logged_in=yes; dotcom_user=omarnuwrar; color_mode=%7B%22color_mode%22%3A%22auto%22%2C%22light_theme%22%3A%7B%22name%22%3A%22light%22%2C%22color_mode%22%3A%22light%22%7D%2C%22dark_theme%22%3A%7B%22name%22%3A%22dark%22%2C%22color_mode%22%3A%22dark%22%7D%7D; cpu_bucket=lg; preferred_color_mode=light; tz=Africa%2FTripoli; _gh_sess=nRiYCgrZvINmXVOPcjLS3bUFlDlhGvB80LffLxj9BecoEG%2BNNlQVZSpsCPJM06QDgHcyFNTVKShA7ToXUJv49lcTau2Uz8RhLc2bYqWUSaJMwPfuOvWiDeZq9hYQAPcy57YoGCH%2BPeVnZDPAGBkzYq9HzEYIZH0t7ET%2BiNyuF%2FD1MzsEiMVfH78KMAyO95x8RxlDO8Z3aKvS322hhrh0Y5%2FIvIdlBs8SwDvzCJBxu%2FT1md8bAduhPy7vFMRbaXlNl6ecTYfUpqIRegyuy4tNAbcdN77Uo5y3OqQPuRdcjJHSbH2jj2hcYYcVZ3no%2FtjO9nuY%2Bh%2BEZUzfQiW%2Fi%2BS01%2BnqfRLbo%2BUSl1jmWM96JtjdxIU1H9vuD6kFya3502YEWpExm6%2BQnJQyKVQEjx49EBAqu1K4oX3cDjm8juwskwppQE76Gi66Ov5nQUdXQg9rPeeBmzodA46SzgA3UT7usYXXefEkfBolZTC6JuN6dPUfHWMq9Rxw1pjpxB8BbWEt7J0Ej1heGi%2FQsgI2YiI0VqUqDhm9E7rcnugTU2r1MhzBvGif3unKtjIgBTMl182JyDusEK8AEpAiJrDVlhK51IoyCFzijHVzfwD4TJRQgKhLPyNQKkz7s%2FRqWq9v3pqNFRDggHDzt20hikadYb9Kcc3G3FyK0KFcEAhtuP%2BbmKlHhV%2BqPTZ9cmoHiTrxEmeAAuHJv%2BmYRRkjjkUxlFGQMKaRmgAtWwe%2Fv%2B3cB9dczogccY31VUKXdek49fnAIsKQdmmmQEWr7De3GSBTrLMQBRstIA7E8PWq007tmf1CjZQ1D1OewlafU%2FNrVHBNp5eyNfvwRgxw5lUOzsKGkvGkkHgHXEJXSZAV384vsQqP1Gs%2FotXK--smsPAB7z6o718%2Blu--yC16If8JCvutqRgfws0idA%3D%3D"
|
| 91 |
-
}
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
try:
|
| 94 |
-
# Fetch the
|
| 95 |
-
response = requests.get(
|
| 96 |
-
response.
|
| 97 |
-
|
| 98 |
-
# Parse the HTML using BeautifulSoup
|
| 99 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
| 100 |
-
|
| 101 |
-
# Find the <script> tag with type="application/json" and `data-target="react-app.embeddedData"`
|
| 102 |
-
script_tag = soup.find('script', {'type': 'application/json', 'data-target': 'react-app.embeddedData'})
|
| 103 |
-
if script_tag:
|
| 104 |
-
# Load the JSON content from the <script> tag
|
| 105 |
-
embedded_data = json.loads(script_tag.string)
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
return {"success": False, "message": "JSON data not found in the 'rawLines' key."}
|
| 120 |
else:
|
| 121 |
-
|
|
|
|
| 122 |
except requests.exceptions.RequestException as e:
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
return {"success": False, "message": f"Error parsing JSON: {je}"}
|
| 126 |
-
|
| 127 |
-
|
| 128 |
|
| 129 |
-
@app.route('/read_Septimius_json', methods=['GET'])
|
| 130 |
-
def fetch_user_json():
|
| 131 |
-
result = fetch_json_from_github()
|
| 132 |
-
return jsonify(result)
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
# API Endpoint to update user.json
|
| 137 |
-
@app.route('/update_Septimius_json', methods=['POST'])
|
| 138 |
-
def update_user_json():
|
| 139 |
-
data = request.json
|
| 140 |
-
|
| 141 |
-
if not data or 'content' not in data:
|
| 142 |
-
return jsonify({"success": False, "message": "Invalid request. 'content' field is required."}), 400
|
| 143 |
-
|
| 144 |
-
# Step 1: Fetch the authenticity_token and commitOid
|
| 145 |
-
authenticity_token, commit_oid = fetch_authenticity_token_and_commit_oid()
|
| 146 |
-
|
| 147 |
-
if authenticity_token and commit_oid:
|
| 148 |
-
# Step 2: Perform the file update
|
| 149 |
-
result = update_user_json_file(authenticity_token, commit_oid, data['content'])
|
| 150 |
-
return jsonify(result)
|
| 151 |
-
else:
|
| 152 |
-
return jsonify({"success": False, "message": "Failed to fetch required tokens."}), 500
|
| 153 |
|
| 154 |
if __name__ == "__main__":
|
| 155 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
+
from flask import Flask, request, jsonify
|
| 3 |
+
from datetime import datetime
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
+
# URLs
|
| 8 |
+
SEPTIMIUS_JSON_URL = "https://corvo-ai-septimius.hf.space/read_Septimius_json"
|
| 9 |
+
PYTHONANYWHERE_URL = "https://omarnuwara.pythonanywhere.com/get-response"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
def is_token_valid(token):
|
| 12 |
+
"""
|
| 13 |
+
Check if the token is valid and not expired by reading from Septimius.json.
|
| 14 |
+
"""
|
| 15 |
try:
|
| 16 |
+
# Fetch the current Septimius.json data
|
| 17 |
+
response = requests.get(SEPTIMIUS_JSON_URL)
|
| 18 |
+
if response.status_code == 200:
|
| 19 |
+
json_data = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
if json_data.get("success"):
|
| 22 |
+
# Check if the token exists and is not expired
|
| 23 |
+
for entry in json_data["data"]:
|
| 24 |
+
if entry.get("token") == token:
|
| 25 |
+
# Parse the expiration date and compare it with the current date
|
| 26 |
+
expired_date = entry.get("expired_date")
|
| 27 |
+
if expired_date and datetime.fromisoformat(expired_date) > datetime.now():
|
| 28 |
+
return True # Token is valid
|
| 29 |
+
else:
|
| 30 |
+
return False # Token is expired
|
| 31 |
+
|
| 32 |
+
return False # Token not found
|
|
|
|
| 33 |
else:
|
| 34 |
+
print(f"Error fetching Septimius.json (Status Code: {response.status_code})")
|
| 35 |
+
return False
|
| 36 |
except requests.exceptions.RequestException as e:
|
| 37 |
+
print(f"Error connecting to Septimius API: {e}")
|
| 38 |
+
return False
|
|
|
|
|
|
|
|
|
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
@app.route('/chat', methods=['POST'])
|
| 42 |
+
def chat():
|
| 43 |
+
try:
|
| 44 |
+
# Ensure the request contains JSON and the required keys
|
| 45 |
+
request_data = request.json if request.is_json else None
|
| 46 |
+
token = request_data.get("token") if request_data else None
|
| 47 |
+
message = request_data.get("message") if request_data else None
|
| 48 |
+
|
| 49 |
+
if not token:
|
| 50 |
+
return jsonify({"error": "No token provided in the request."}), 400
|
| 51 |
+
if not message:
|
| 52 |
+
return jsonify({"error": "No message provided in the request."}), 400
|
| 53 |
+
|
| 54 |
+
# Check if the token is valid
|
| 55 |
+
if not is_token_valid(token):
|
| 56 |
+
return jsonify({"error": "You must subscribe to continue."}), 403
|
| 57 |
+
|
| 58 |
+
# Forward the message to PythonAnywhere API
|
| 59 |
+
data = {"message": message}
|
| 60 |
+
response = requests.post(PYTHONANYWHERE_URL, json=data)
|
| 61 |
+
|
| 62 |
+
if response.status_code == 200:
|
| 63 |
+
response_json = response.json()
|
| 64 |
+
ai_response = response_json.get("response", "No 'response' key found in the JSON.")
|
| 65 |
+
ai_response = ai_response.encode('latin1').decode('utf-8', 'ignore') # Handle encoding issues
|
| 66 |
+
return jsonify({"response": ai_response})
|
| 67 |
+
else:
|
| 68 |
+
# Handle errors from PythonAnywhere
|
| 69 |
+
return jsonify({
|
| 70 |
+
"error": "Error from PythonAnywhere",
|
| 71 |
+
"details": response.text
|
| 72 |
+
}), response.status_code
|
| 73 |
|
| 74 |
+
except Exception as e:
|
| 75 |
+
# Catch any unexpected errors
|
| 76 |
+
return jsonify({"error": "An error occurred", "details": str(e)}), 500
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
app.run(host="0.0.0.0", port=7860)
|