Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -78,6 +78,61 @@ def update_user_json_file(authenticity_token, commit_oid, new_content):
|
|
| 78 |
return {"success": False, "message": f"Request failed with status code {response.status_code}", "details": response.text}
|
| 79 |
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# API Endpoint to update user.json
|
| 82 |
@app.route('/update_user_json', methods=['POST'])
|
| 83 |
def update_user_json():
|
|
|
|
| 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/user.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.613060662.1706026873; _device_id=a558b3829e717bb44a5756b2c914dad5; saved_user_sessions=169075744%3AYasgfrHcDi5hMOq-6Ry9Zv_VegdDA8KTM1voh_g-TGBKt85u; logged_in=no; cpu_bucket=lg; preferred_color_mode=light; tz=Africa%2FTripoli; _gh_sess=46lFv47ysdZXaDQjVTezeQ2FMXGNQcxjYKkJL2KyZuDGDnHEcIklvDAso6lZdCrKsvKTCh2Kg1OVIurbR2lKoCZHg%2FlZa6iX022mwXRsFjind%2Bwho8gLOT9FBpX%2B9Anu9aRubdkqajNFct4%2BjNOIkMIPPwVjLtdLi3jomLZjj4sVYSzIoJVi1pDooq76bjZRZgndwXocfXHD0Yo8t14l9wTrihU97ahTq2fU7UAetTQ0Xd%2BpqTaBLkcS%2F0SNieGg4MHlBoi2ecgyWQAk2625HiQCKGrVOtNsiihmaiRd%2FtfNxcja--XhXgdENhrRbNN6aw--DtMKGX0uq6kGd8BnbON4zw%3D%3D"
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
try:
|
| 94 |
+
# Fetch the HTML content of the page
|
| 95 |
+
response = requests.get(url, headers=headers)
|
| 96 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
| 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 |
+
# Navigate to the "blob" > "rawLines" key for the JSON in the file
|
| 108 |
+
raw_lines = embedded_data.get("payload", {}).get("blob", {}).get("rawLines", [])
|
| 109 |
+
if raw_lines:
|
| 110 |
+
# The JSON content is in the first element of the rawLines list
|
| 111 |
+
json_content = raw_lines[0]
|
| 112 |
+
|
| 113 |
+
# Parse the JSON content
|
| 114 |
+
data = json.loads(json_content)
|
| 115 |
+
|
| 116 |
+
# Return the extracted JSON data
|
| 117 |
+
return {"success": True, "data": data}
|
| 118 |
+
else:
|
| 119 |
+
return {"success": False, "message": "JSON data not found in the 'rawLines' key."}
|
| 120 |
+
else:
|
| 121 |
+
return {"success": False, "message": "Could not find the <script> tag with embedded JSON data."}
|
| 122 |
+
except requests.exceptions.RequestException as e:
|
| 123 |
+
return {"success": False, "message": f"Error fetching data: {e}"}
|
| 124 |
+
except json.JSONDecodeError as je:
|
| 125 |
+
return {"success": False, "message": f"Error parsing JSON: {je}"}
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
@app.route('/read_user_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_user_json', methods=['POST'])
|
| 138 |
def update_user_json():
|