111 lines
3.3 KiB
Python
111 lines
3.3 KiB
Python
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
def set_environment_variable(token, url="https://hub.allspice.io"):
|
|
if sys.platform.startswith('win32'):
|
|
# For Windows
|
|
with open(os.path.expanduser("~\\.bash_profile"), "a") as f:
|
|
f.write(f"\nexport ALLSPICE_ACCESS_TOKEN={token}\n")
|
|
# Alternatively, set it for the current session and future sessions
|
|
subprocess.run(["setx", "ALLSPICE_ACCESS_TOKEN", token], check=True)
|
|
subprocess.run(["setx", "ALLSPICE_URL", url], check=True)
|
|
elif sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
|
|
# For Unix/Linux and macOS
|
|
with open(os.path.expanduser("~/.bashrc"), "a") as f:
|
|
f.write(f"\nexport ALLSPICE_ACCESS_TOKEN={token}\n")
|
|
# For the current session
|
|
os.environ["ALLSPICE_ACCESS_TOKEN"] = token
|
|
else:
|
|
print("Unsupported OS")
|
|
|
|
|
|
import requests
|
|
from bs4 import BeautifulSoup
|
|
def get_tokens():
|
|
csrf_token = ""
|
|
allspice_token = None
|
|
|
|
# Get the page
|
|
# URL of the page containing the form
|
|
form_url = "https://hub.allspice.io/user/settings/applications"
|
|
|
|
# Make a GET request to fetch the page
|
|
response = requests.get(form_url)
|
|
|
|
# Use BeautifulSoup to parse the HTML content
|
|
soup = BeautifulSoup(response.text, 'html.parser')
|
|
|
|
# Find the input element containing the CSRF token
|
|
csrf_input = soup.find('input', {'name': '_csrf'})
|
|
|
|
# Extract the value attribute from the CSRF input element
|
|
csrf_token = csrf_input['value'] if csrf_input else None
|
|
|
|
# Check if CSRF token was found
|
|
if csrf_token:
|
|
print(f"CSRF Token: {csrf_token}")
|
|
else:
|
|
print("CSRF Token not found.")
|
|
exit(1)
|
|
|
|
# Form data to be submitted
|
|
data = {
|
|
"_csrf": csrf_token, # CSRF token obtained dynamically
|
|
"name": "token-name", # Desired token name
|
|
"scope": "", # For "All (public, private, and limited)" access
|
|
"permissions": [ # List of permissions
|
|
"write:activitypub",
|
|
"write:misc",
|
|
"write:notification",
|
|
"write:organization",
|
|
"write:package",
|
|
"write:issue",
|
|
"write:repository",
|
|
"write:user"
|
|
]
|
|
}
|
|
|
|
|
|
# Send a POST request to submit the form
|
|
response = requests.post(form_url, data=data)
|
|
|
|
# Check if the form was submitted successfully
|
|
if response.status_code == 200:
|
|
print("Form submitted successfully.")
|
|
soup = BeautifulSoup(response.text, 'html.parser')
|
|
# write soup to soup_response.html
|
|
with open("soup_response.html", "w") as file:
|
|
file.write(str(soup))
|
|
else:
|
|
print("Form submission failed.")
|
|
exit(1)
|
|
|
|
# Fetch webpage again to get the token
|
|
response = requests.get(form_url)
|
|
|
|
# Use BeautifulSoup to parse the HTML content
|
|
soup = BeautifulSoup(response.text, 'html.parser')
|
|
|
|
# Write soup to file soup.html
|
|
with open("soup.html", "w") as file:
|
|
file.write(str(soup))
|
|
|
|
return csrf_token, allspice_token
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
csrf_token, allspice_token = get_tokens()
|
|
if allspice_token:
|
|
set_environment_variable(allspice_token)
|
|
print(f"Token set: {allspice_token}")
|
|
# refresh the environment variables
|
|
os.system("source ~/.bashrc")
|
|
else:
|
|
print("No token provided.")
|