transcript / app.py
CORVO-AI's picture
Update app.py
20e58bc verified
raw
history blame
10.9 kB
from flask import Flask, request, jsonify
import requests
from bs4 import BeautifulSoup
import json
app = Flask(__name__)
# Step 1: Fetch the authenticity_token and commitOid from the GitHub edit page
def fetch_authenticity_token_and_commit_oid():
url = "https://github.com/omarnuwrar/api/edit/main/user.json"
headers = {
"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",
"if-none-match": 'W/"2ff86bd1792cfee5ed79ee070b3b46de"',
"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",
"x-github-target": "dotcom",
"x-react-router": "json",
"x-requested-with": "XMLHttpRequest",
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
script_tag = soup.find("script", {"type": "application/json", "data-target": "react-app.embeddedData"})
if script_tag:
try:
json_data = json.loads(script_tag.string.strip())
authenticity_token = json_data["payload"]["csrf_tokens"]["/omarnuwrar/api/tree-save/main/user.json"]["post"]
commit_oid = json_data["payload"]["webCommitInfo"]["commitOid"]
return authenticity_token, commit_oid
except (KeyError, json.JSONDecodeError) as e:
print(f"Error: Failed to extract data. Details: {str(e)}")
return None, None
else:
print("Error: Could not find the required <script> tag.")
return None, None
else:
print(f"Error: Failed to fetch the page. Status code: {response.status_code}")
return None, None
# Step 2: Send the POST request to update the user.json file
def update_user_json_file(authenticity_token, commit_oid, new_content):
url = "https://github.com/omarnuwrar/api/tree-save/main/user.json"
headers = {
"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",
"x-requested-with": "XMLHttpRequest",
"github-verified-fetch": "true",
"content-type": "application/x-www-form-urlencoded",
"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=lHUS%2F78pWCs48xqC7p07at9D38qm%2F1Cpk9lWTd2JiSKFlyZGzJlUOq3SBR9I1DhHuprUI9fW1bNU8txfXOwBGdZ1jRUvV4qbsaH0eRMWmuYH6d7qElAM0JUFwVwiOegMMNFmVO0kpI7iOZOGZ%2FTZ3Q95oPiI%2Bx0NDELUemQfP17roEY1AkaeHFrmPGjYtRlFC7Mc5Chd3v3VTBudtUJLn1t%2F%2BNwRTw6gA6YheRwQL1KYBmTv0RiyPp1cNhowIJ7CYMSAi0SzXbjcVPBXpsq1cRlEjgzl%2FrQ9OnXFe1zRJs22DOcYlZ6HBKUs2EMrB32eIM%2Fg4Q7%2FieLI8dI0YuMWWkvTFpjJ4Fg%2Bu4fLpmN%2BZ0HqlL1dcLKuy9MDB7OLAzlUknj%2FDYvZKILBDvEIYCoSPZrcSf9t9lbPYsmn5%2F08tjPXPP%2FacDUViRe0L%2F2YVETK65uORozNK8E2svvFfo1xUjQtNMknRCqOE8DweOFqnSzWYlygTtVS92oS8k0%2BNZK9i2mYMTky6JnaI9nspkTESalWUAVAyv%2Fu%2BRr1w1v3fEiF4wgcmNqo9rHGvlXG7U2d6paNnq5OWFiAou43or0HmCMu3JVAS5XSItDB069Etzlt5wz2OKBDmrjqcQuUXWzvbUnGHxLI0ceBMcOqbcRHGrFlZWjLzsTD%2FeYeCnqpzNDZP53bRbZRomHprx9usLW8OQe%2F477IhOD8qpKqULJW5kEngKofC2hgX8rco3Ner8bU3HNGs6f6vx2BdSrikyefqR7ErzseDaACkgk8uK2RJWNfkyzUS8eaXiPoGiuzrAiFQfetP2Oej5A6po0VKE2ecTziUZZ3P51mL1yA4zk07rReZLdg2AH%2F%2By489AIFyoW24AsV--m4%2Fsyr7G6rcCsQNS--I21rEnxrqaGhOTJI6aeM9w%3D%3D",
}
payload = {
"message": "Update user.json",
"placeholder_message": "Update user.json",
"description": "",
"commit-choice": "direct",
"target_branch": "main",
"quick_pull": "",
"guidance_task": "",
"commit": commit_oid,
"same_repo": "1",
"pr": "",
"content_changed": "true",
"filename": "user.json",
"new_filename": "user.json",
"value": new_content,
"authenticity_token": authenticity_token,
}
response = requests.post(url, headers=headers, data=payload)
if response.status_code == 200:
return {"success": True, "message": "user.json has been updated!"}
else:
return {"success": False, "message": f"Request failed with status code {response.status_code}", "details": response.text}
# Function to fetch and extract the JSON data
def fetch_json_from_github():
# URL of the GitHub page
url = "https://github.com/omarnuwrar/api/blob/main/user.json"
# Custom headers
headers = {
"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",
"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"
}
try:
# Fetch the HTML content of the page
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise an exception for HTTP errors
# Parse the HTML using BeautifulSoup
soup = BeautifulSoup(response.text, 'html.parser')
# Find the <script> tag with type="application/json" and `data-target="react-app.embeddedData"`
script_tag = soup.find('script', {'type': 'application/json', 'data-target': 'react-app.embeddedData'})
if script_tag:
# Load the JSON content from the <script> tag
embedded_data = json.loads(script_tag.string)
# Navigate to the "blob" > "rawLines" key for the JSON in the file
raw_lines = embedded_data.get("payload", {}).get("blob", {}).get("rawLines", [])
if raw_lines:
# The JSON content is in the first element of the rawLines list
json_content = raw_lines[0]
# Parse the JSON content
data = json.loads(json_content)
# Return the extracted JSON data
return {"success": True, "data": data}
else:
return {"success": False, "message": "JSON data not found in the 'rawLines' key."}
else:
return {"success": False, "message": "Could not find the <script> tag with embedded JSON data."}
except requests.exceptions.RequestException as e:
return {"success": False, "message": f"Error fetching data: {e}"}
except json.JSONDecodeError as je:
return {"success": False, "message": f"Error parsing JSON: {je}"}
@app.route('/read_user_json', methods=['GET'])
def fetch_user_json():
result = fetch_json_from_github()
return jsonify(result)
# API Endpoint to update user.json
@app.route('/update_user_json', methods=['POST'])
def update_user_json():
data = request.json
if not data or 'content' not in data:
return jsonify({"success": False, "message": "Invalid request. 'content' field is required."}), 400
# Step 1: Fetch the authenticity_token and commitOid
authenticity_token, commit_oid = fetch_authenticity_token_and_commit_oid()
if authenticity_token and commit_oid:
# Step 2: Perform the file update
result = update_user_json_file(authenticity_token, commit_oid, data['content'])
return jsonify(result)
else:
return jsonify({"success": False, "message": "Failed to fetch required tokens."}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)